Remove \r on Windows

This commit is contained in:
mo8it 2026-02-26 17:39:00 +01:00
parent e91647b023
commit 7e5793b642
2 changed files with 21 additions and 6 deletions

View File

@ -16,8 +16,16 @@ struct InfoFile<'a> {
#[proc_macro] #[proc_macro]
pub fn include_files(_: TokenStream) -> TokenStream { pub fn include_files(_: TokenStream) -> TokenStream {
let info_file = include_str!("../info.toml"); // Remove `\r` on Windows
let exercises = toml::de::from_str::<InfoFile>(info_file) 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`") .expect("Failed to parse `info.toml`")
.exercises; .exercises;

View File

@ -94,10 +94,17 @@ impl InfoFile {
/// Community exercises: Parse the `info.toml` file in the current directory. /// Community exercises: Parse the `info.toml` file in the current directory.
pub fn parse() -> Result<Self> { pub fn parse() -> Result<Self> {
// Read a local `info.toml` if it exists. // Read a local `info.toml` if it exists.
let slf = match fs::read_to_string("info.toml") { let slf = match fs::read("info.toml") {
// Leaking is fine since the info file is used until the end of the program. Ok(file_content) => {
Ok(file_content) => toml::de::from_str::<Self>(file_content.leak()) // Remove `\r` on Windows.
.context("Failed to parse the `info.toml` file")?, // 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) => { Err(e) => {
if e.kind() == ErrorKind::NotFound { if e.kind() == ErrorKind::NotFound {
return toml::de::from_str(EMBEDDED_FILES.info_file) return toml::de::from_str(EMBEDDED_FILES.info_file)