Implemented day 2.
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
use std::{path::Path, io::{BufReader, BufRead, self}, fs::File};
|
||||
use std::{io::{BufReader, BufRead, self}, fs::File, str::FromStr, path::Path, convert::Infallible, num::ParseIntError};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct AdventError(String);
|
||||
pub struct AdventError(pub String);
|
||||
|
||||
impl From<ParseIntError> for AdventError {
|
||||
fn from(e: ParseIntError) -> Self {
|
||||
AdventError(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<io::Error> for AdventError {
|
||||
fn from(e: io::Error) -> Self {
|
||||
@@ -9,8 +15,14 @@ impl From<io::Error> for AdventError {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_into_vec(path: &Path) -> Result<Vec<String>, AdventError> {
|
||||
let file = File::open(path).expect("no such file");
|
||||
impl From<Infallible> for AdventError {
|
||||
fn from(_: Infallible) -> Self {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_into_vec<T:FromStr>(file_path : &Path) -> Result<Vec<T>, AdventError> where AdventError: From<<T as FromStr>::Err> {
|
||||
let file = File::open(file_path).expect("no such file");
|
||||
let buf = BufReader::new(file);
|
||||
buf.lines().map(|line| line.map_err(Into::into)).collect::<Result<Vec<String>, AdventError>>()
|
||||
buf.lines().map(|line| T::from_str(line?.as_str()).map_err(Into::into)).collect::<Result<Vec<T>, AdventError>>()
|
||||
}
|
||||
Reference in New Issue
Block a user