Simplify error-handling in seed generation

This commit is contained in:
Bruno BELANYI 2024-04-06 19:22:16 +01:00
parent a667e6b7f2
commit 7dd0da6628

View file

@ -212,25 +212,29 @@ mod test {
Some((prefix, mid, suffix)) Some((prefix, mid, suffix))
} }
fn array_string(piece_type: &str, values: &[Magic]) -> Result<String, std::fmt::Error> { fn array_string(piece_type: &str, values: &[Magic]) -> String {
let mut res = String::new(); let inner = || -> Result<String, std::fmt::Error> {
let mut res = String::new();
writeln!( writeln!(
&mut res, &mut res,
"/// A set of magic numbers for {} move generation.", "/// A set of magic numbers for {} move generation.",
piece_type piece_type
)?; )?;
writeln!( writeln!(
&mut res, &mut res,
"pub(crate) const {}_SEED: [u64; Square::NUM_VARIANTS] = [", "pub(crate) const {}_SEED: [u64; Square::NUM_VARIANTS] = [",
piece_type.to_uppercase() piece_type.to_uppercase()
)?; )?;
for magic in values { for magic in values {
writeln!(&mut res, " {},", magic.magic)?; writeln!(&mut res, " {},", magic.magic)?;
} }
writeln!(&mut res, "];")?; writeln!(&mut res, "];")?;
Ok(res) Ok(res)
};
inner().unwrap()
} }
#[test] #[test]
@ -243,8 +247,8 @@ mod test {
let original_text = std::fs::read_to_string(file!()).unwrap(); let original_text = std::fs::read_to_string(file!()).unwrap();
let bishop_array = array_string("bishop", &bishop_magics[..]).unwrap(); let bishop_array = array_string("bishop", &bishop_magics[..]);
let rook_array = array_string("rook", &rook_magics[..]).unwrap(); let rook_array = array_string("rook", &rook_magics[..]);
let new_text = { let new_text = {
let start_marker = "// region:sourcegen\n"; let start_marker = "// region:sourcegen\n";