From 52db8b9109355dba108bfd07affd6c3a08564e92 Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Tue, 5 Dec 2023 12:36:28 +0100 Subject: [PATCH] Refactor --- src/day1/mod.rs | 4 +--- src/day2/mod.rs | 2 +- src/day3/mod.rs | 2 +- src/day4/mod.rs | 2 +- src/error/mod.rs | 28 ++++++++++++++++++++++++++++ src/input/mod.rs | 29 ++--------------------------- src/main.rs | 5 +++-- 7 files changed, 37 insertions(+), 35 deletions(-) create mode 100644 src/error/mod.rs diff --git a/src/day1/mod.rs b/src/day1/mod.rs index d72dc02..3ff4af5 100644 --- a/src/day1/mod.rs +++ b/src/day1/mod.rs @@ -1,9 +1,7 @@ use std::{path::Path, vec}; -use crate::input::AdventError; - -use super::input::read_into_vec; +use crate::{input::read_into_vec, error::AdventError}; fn process(lines: Vec::) -> Result { let calibrations: Vec = lines.iter().map(|v| { diff --git a/src/day2/mod.rs b/src/day2/mod.rs index 6ba748d..720a7eb 100644 --- a/src/day2/mod.rs +++ b/src/day2/mod.rs @@ -1,6 +1,6 @@ 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 { type Err = AdventError; diff --git a/src/day3/mod.rs b/src/day3/mod.rs index 73675e4..7f6bf45 100644 --- a/src/day3/mod.rs +++ b/src/day3/mod.rs @@ -2,7 +2,7 @@ use std::{str::FromStr, path::Path, option::Option}; use itertools::Itertools; -use crate::input::{AdventError, read_into_vec}; +use crate::{input::read_into_vec, error::AdventError}; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Number { diff --git a/src/day4/mod.rs b/src/day4/mod.rs index 7a83dc3..388644d 100644 --- a/src/day4/mod.rs +++ b/src/day4/mod.rs @@ -2,7 +2,7 @@ use std::{collections::HashSet, path::Path, str::FromStr}; use itertools::Itertools; -use crate::input::{AdventError, read_into_vec}; +use crate::{input::read_into_vec, error::AdventError}; impl FromStr for ScratchGame { type Err = AdventError; diff --git a/src/error/mod.rs b/src/error/mod.rs new file mode 100644 index 0000000..fb703e6 --- /dev/null +++ b/src/error/mod.rs @@ -0,0 +1,28 @@ +use std::{num::{ParseIntError, TryFromIntError}, convert::Infallible, io}; + +#[derive(Debug)] +pub struct AdventError(pub String); + +impl From for AdventError { + fn from(e: ParseIntError) -> Self { + AdventError(e.to_string()) + } +} + +impl From for AdventError { + fn from(e: io::Error) -> Self { + AdventError(e.to_string()) + } +} + +impl From for AdventError { + fn from(_: Infallible) -> Self { + unreachable!() + } +} + +impl From for AdventError { + fn from(e: TryFromIntError) -> Self { + AdventError(e.to_string()) + } +} \ No newline at end of file diff --git a/src/input/mod.rs b/src/input/mod.rs index 31a9a07..0f51191 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -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)] -pub struct AdventError(pub String); - -impl From for AdventError { - fn from(e: ParseIntError) -> Self { - AdventError(e.to_string()) - } -} - -impl From for AdventError { - fn from(e: io::Error) -> Self { - AdventError(e.to_string()) - } -} - -impl From for AdventError { - fn from(_: Infallible) -> Self { - unreachable!() - } -} - -impl From for AdventError { - fn from(e: TryFromIntError) -> Self { - AdventError(e.to_string()) - } -} +use crate::error::AdventError; pub fn read_into_vec(file_path : &Path) -> Result, AdventError> where AdventError: From<::Err> { let file = File::open(file_path).expect("no such file"); diff --git a/src/main.rs b/src/main.rs index 55a5477..ea43aac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,14 @@ pub mod input; +pub mod error; pub mod day1; pub mod day2; pub mod day3; pub mod day4; -use std::{env}; +use std::env; -use input::AdventError; +use crate::error::AdventError; fn main() -> Result<(), AdventError> { let days = vec![