Skip to content

Commit 7e5793b

Browse files
committed
Remove \r on Windows
1 parent e91647b commit 7e5793b

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

rustlings-macros/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ struct InfoFile<'a> {
1616

1717
#[proc_macro]
1818
pub fn include_files(_: TokenStream) -> TokenStream {
19-
let info_file = include_str!("../info.toml");
20-
let exercises = toml::de::from_str::<InfoFile>(info_file)
19+
// Remove `\r` on Windows
20+
let info_file = String::from_utf8(
21+
include_bytes!("../info.toml")
22+
.iter()
23+
.copied()
24+
.filter(|c| *c != b'\r')
25+
.collect(),
26+
)
27+
.expect("Failed to parse `info.toml` as UTF8");
28+
let exercises = toml::de::from_str::<InfoFile>(&info_file)
2129
.expect("Failed to parse `info.toml`")
2230
.exercises;
2331

src/info_file.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,17 @@ impl InfoFile {
9494
/// Community exercises: Parse the `info.toml` file in the current directory.
9595
pub fn parse() -> Result<Self> {
9696
// Read a local `info.toml` if it exists.
97-
let slf = match fs::read_to_string("info.toml") {
98-
// Leaking is fine since the info file is used until the end of the program.
99-
Ok(file_content) => toml::de::from_str::<Self>(file_content.leak())
100-
.context("Failed to parse the `info.toml` file")?,
97+
let slf = match fs::read("info.toml") {
98+
Ok(file_content) => {
99+
// Remove `\r` on Windows.
100+
// Leaking is fine since the info file is used until the end of the program.
101+
let file_content =
102+
String::from_utf8(file_content.into_iter().filter(|c| *c != b'\r').collect())
103+
.context("Failed to parse `info.toml` as UTF8")?
104+
.leak();
105+
toml::de::from_str::<Self>(file_content)
106+
.context("Failed to parse the `info.toml` file")?
107+
}
101108
Err(e) => {
102109
if e.kind() == ErrorKind::NotFound {
103110
return toml::de::from_str(EMBEDDED_FILES.info_file)

0 commit comments

Comments
 (0)