aoc2025/bram/rust/src/utils/mod.rs

54 lines
1.2 KiB
Rust

use std::fs;
pub mod diagnostics;
pub fn char_to_u8(c : char) -> Option<u8> {
char_to_u32(c).and_then(|n| u8::try_from(n).ok())
}
pub fn char_to_u32(c : char) -> Option<u32> {
c.to_digit(10)
}
pub fn char_to_u64(c : char) -> Option<u64> {
char_to_u32(c).map(u64::from)
}
pub fn read_from_file(name : &str) -> String {
match fs::read_to_string(name) {
Err(error) => {
println!("Failed to read file {name}: {error}");
String::new()
},
Ok(s) => s,
}
}
pub fn str_to_i16(s : &str) -> Option<i16> {
s.trim().to_string().parse::<i16>().ok()
}
// pub fn str_to_i32(s : &str) -> Option<i32> {
// s.trim().to_string().parse::<i32>().ok()
// }
// pub fn str_to_u8(s : &str) -> Option<u8> {
// s.trim().to_string().parse::<u8>().ok()
// }
// pub fn str_to_u16(s : &str) -> Option<u16> {
// s.trim().to_string().parse::<u16>().ok()
// }
// pub fn str_to_u32(s : &str) -> Option<u32> {
// s.trim().to_string().parse::<u32>().ok()
// }
pub fn str_to_u64(s : &str) -> Option<u64> {
s.trim().to_string().parse::<u64>().ok()
}
pub fn str_to_u128(s : &str) -> Option<u128> {
s.trim().to_string().parse::<u128>().ok()
}