This commit is contained in:
Dennis Brentjes 2023-12-05 12:36:28 +01:00
parent f0370944d3
commit 52db8b9109
7 changed files with 37 additions and 35 deletions

View File

@ -1,9 +1,7 @@
use std::{path::Path, vec}; use std::{path::Path, vec};
use crate::input::AdventError; use crate::{input::read_into_vec, error::AdventError};
use super::input::read_into_vec;
fn process(lines: Vec::<String>) -> Result<u32, AdventError> { fn process(lines: Vec::<String>) -> Result<u32, AdventError> {
let calibrations: Vec<u32> = lines.iter().map(|v| { let calibrations: Vec<u32> = lines.iter().map(|v| {

View File

@ -1,6 +1,6 @@
use std::{path::Path, str::FromStr, cmp::max}; use std::{path::Path, str::FromStr, cmp::max};
use crate::input::{AdventError, read_into_vec}; use crate::{input::read_into_vec, error::AdventError};
impl FromStr for Color { impl FromStr for Color {
type Err = AdventError; type Err = AdventError;

View File

@ -2,7 +2,7 @@ use std::{str::FromStr, path::Path, option::Option};
use itertools::Itertools; use itertools::Itertools;
use crate::input::{AdventError, read_into_vec}; use crate::{input::read_into_vec, error::AdventError};
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Number { pub struct Number {

View File

@ -2,7 +2,7 @@ use std::{collections::HashSet, path::Path, str::FromStr};
use itertools::Itertools; use itertools::Itertools;
use crate::input::{AdventError, read_into_vec}; use crate::{input::read_into_vec, error::AdventError};
impl FromStr for ScratchGame { impl FromStr for ScratchGame {
type Err = AdventError; type Err = AdventError;

28
src/error/mod.rs Normal file
View File

@ -0,0 +1,28 @@
use std::{num::{ParseIntError, TryFromIntError}, convert::Infallible, io};
#[derive(Debug)]
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 {
AdventError(e.to_string())
}
}
impl From<Infallible> for AdventError {
fn from(_: Infallible) -> Self {
unreachable!()
}
}
impl From<TryFromIntError> for AdventError {
fn from(e: TryFromIntError) -> Self {
AdventError(e.to_string())
}
}

View File

@ -1,31 +1,6 @@
use std::{io::{BufReader, BufRead, self}, fs::File, str::FromStr, path::Path, convert::Infallible, num::{ParseIntError, TryFromIntError}}; use std::{io::{BufReader, BufRead}, fs::File, str::FromStr, path::Path};
#[derive(Debug)] use crate::error::AdventError;
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 {
AdventError(e.to_string())
}
}
impl From<Infallible> for AdventError {
fn from(_: Infallible) -> Self {
unreachable!()
}
}
impl From<TryFromIntError> for AdventError {
fn from(e: TryFromIntError) -> Self {
AdventError(e.to_string())
}
}
pub fn read_into_vec<T:FromStr>(file_path : &Path) -> Result<Vec<T>, AdventError> where AdventError: From<<T as FromStr>::Err> { 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 file = File::open(file_path).expect("no such file");

View File

@ -1,13 +1,14 @@
pub mod input; pub mod input;
pub mod error;
pub mod day1; pub mod day1;
pub mod day2; pub mod day2;
pub mod day3; pub mod day3;
pub mod day4; pub mod day4;
use std::{env}; use std::env;
use input::AdventError; use crate::error::AdventError;
fn main() -> Result<(), AdventError> { fn main() -> Result<(), AdventError> {
let days = vec![ let days = vec![