advent_of_utils_cli/error/
loading.rs

1use std::path::PathBuf;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5pub enum LoadingError {
6    #[error("No solution library found for year {year}")]
7    LibraryNotFound {
8        year: i32,
9        search_path: PathBuf,
10        #[source]
11        source: Option<std::io::Error>,
12    },
13
14    #[error("Multiple solution libraries found: {}", .paths.iter().map(|p| p.display().to_string()).collect::<Vec<_>>().join(", "))]
15    AmbiguousLibraries { paths: Vec<PathBuf> },
16
17    #[error("Failed to load solution library: {reason}")]
18    LibraryLoadFailed {
19        reason: String,
20        #[source]
21        source: Option<libloading::Error>,
22    },
23
24    #[error("No solutions found in library for year {year}")]
25    NoSolutions { year: i32 },
26
27    #[error("Invalid solution library: {reason}")]
28    InvalidLibrary { reason: String },
29}
30
31impl LoadingError {
32    pub fn library_load_failed(e: libloading::Error) -> Self {
33        LoadingError::LibraryLoadFailed {
34            reason: e.to_string(),
35            source: Some(e),
36        }
37    }
38
39    pub fn invalid_library(reason: impl Into<String>) -> Self {
40        LoadingError::InvalidLibrary {
41            reason: reason.into(),
42        }
43    }
44
45    pub fn no_solutions(year: i32) -> Self {
46        LoadingError::NoSolutions { year }
47    }
48
49    pub fn library_not_found(
50        year: i32,
51        search_path: PathBuf,
52        source: Option<std::io::Error>,
53    ) -> Self {
54        LoadingError::LibraryNotFound {
55            year,
56            search_path,
57            source,
58        }
59    }
60
61    pub fn ambiguous_libraries(paths: Vec<PathBuf>) -> Self {
62        LoadingError::AmbiguousLibraries { paths }
63    }
64}