Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions rustlings-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ struct InfoFile<'a> {

#[proc_macro]
pub fn include_files(_: TokenStream) -> TokenStream {
let info_file = include_str!("../info.toml");
let exercises = toml::de::from_str::<InfoFile>(info_file)
// Remove `\r` on Windows
let info_file = String::from_utf8(
include_bytes!("../info.toml")
.iter()
.copied()
.filter(|c| *c != b'\r')
.collect(),
)
.expect("Failed to parse `info.toml` as UTF8");
let exercises = toml::de::from_str::<InfoFile>(&info_file)
.expect("Failed to parse `info.toml`")
.exercises;

Expand Down
15 changes: 11 additions & 4 deletions src/info_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,17 @@ impl InfoFile {
/// Community exercises: Parse the `info.toml` file in the current directory.
pub fn parse() -> Result<Self> {
// Read a local `info.toml` if it exists.
let slf = match fs::read_to_string("info.toml") {
// Leaking is fine since the info file is used until the end of the program.
Ok(file_content) => toml::de::from_str::<Self>(file_content.leak())
.context("Failed to parse the `info.toml` file")?,
let slf = match fs::read("info.toml") {
Ok(file_content) => {
// Remove `\r` on Windows.
// Leaking is fine since the info file is used until the end of the program.
let file_content =
String::from_utf8(file_content.into_iter().filter(|c| *c != b'\r').collect())
.context("Failed to parse `info.toml` as UTF8")?
.leak();
toml::de::from_str::<Self>(file_content)
.context("Failed to parse the `info.toml` file")?
}
Err(e) => {
if e.kind() == ErrorKind::NotFound {
return toml::de::from_str(EMBEDDED_FILES.info_file)
Expand Down
6 changes: 1 addition & 5 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ impl<'a> Cmd<'a> {
cmd.args(self.args).stdin(Stdio::null());

let status = match self.output {
None => cmd
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.unwrap(),
None => cmd.status().unwrap(),
Some(FullStdout(stdout)) => {
let output = cmd.stderr(Stdio::null()).output().unwrap();
assert_eq!(from_utf8(&output.stdout).unwrap(), stdout);
Expand Down