use std::{env, fs, num::ParseIntError, str::FromStr}; #[derive(Debug)] struct BatteryBank(Vec); struct ParseBatteryBankError; impl From for ParseBatteryBankError { fn from(_: ParseIntError) -> Self { Self } } impl FromStr for BatteryBank { type Err = ParseBatteryBankError; fn from_str(s: &str) -> Result { Ok(Self( s.chars() .map(|ch| ch.to_string().parse()) .collect::>()?, )) } } #[derive(Debug)] struct Input(Vec); #[derive(Debug)] enum ParseInputError { BadBank(String), } impl FromStr for Input { type Err = ParseInputError; fn from_str(s: &str) -> Result { Ok(Self( s.split_whitespace() .map(|bank| bank.parse().map_err(|_| ParseInputError::BadBank(s.into()))) .collect::>()?, )) } } #[derive(Debug)] enum Error { BadArgument, BadInput(ParseInputError), } impl From for Error { fn from(value: std::io::Error) -> Self { Self::BadArgument } } impl From for Error { fn from(value: ParseInputError) -> Self { Self::BadInput(value) } } fn main() -> Result<(), Error> { let mut args = env::args(); let input_path = args.nth(1).ok_or(Error::BadArgument)?; let input: Input = fs::read_to_string(input_path)?.parse()?; dbg!(input); Ok(()) }