From 312dcc7ead1f64bdbbae53a63a4dde4fbb4277f3 Mon Sep 17 00:00:00 2001 From: Sebastian Kuzminsky Date: Sun, 9 Feb 2025 14:54:48 -0700 Subject: [PATCH] repo: better error handling fixup repo err handling --- tools/Cargo.lock | 21 +++++++++++++++++++++ tools/Cargo.toml | 1 + tools/src/bin/parser/main.rs | 2 +- tools/src/lib.rs | 1 + tools/src/repo.rs | 16 +++++++++++----- 5 files changed, 35 insertions(+), 6 deletions(-) diff --git a/tools/Cargo.lock b/tools/Cargo.lock index 4925a65..2ff245f 100644 --- a/tools/Cargo.lock +++ b/tools/Cargo.lock @@ -230,6 +230,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "2.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d452f284b73e6d76dd36758a0c8684b1d5be31f92b89d07fd5822175732206fc" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26afc1baea8a989337eeb52b6e72a039780ce45c3edfcc9c5b9d112feeb173c2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "toml" version = "0.8.19" @@ -271,6 +291,7 @@ dependencies = [ "anyhow", "clap", "serde", + "thiserror", "toml", "uuid", ] diff --git a/tools/Cargo.toml b/tools/Cargo.toml index 8a2048a..4cff3c3 100644 --- a/tools/Cargo.toml +++ b/tools/Cargo.toml @@ -7,5 +7,6 @@ edition = "2021" anyhow = "1.0.95" clap = { version = "4.5.26", features = ["derive"] } serde = { version = "1.0.217", features = ["derive"] } +thiserror = "2.0.11" toml = "0.8.19" uuid = { version = "1.12.1", features = ["std", "v4" ] } diff --git a/tools/src/bin/parser/main.rs b/tools/src/bin/parser/main.rs index 0b8e779..7cdff80 100644 --- a/tools/src/bin/parser/main.rs +++ b/tools/src/bin/parser/main.rs @@ -15,7 +15,7 @@ struct Args { fn main() -> anyhow::Result<()> { let args = Args::parse(); - let repo = Repo::new(&args.repo); + let repo = Repo::new(&args.repo).unwrap(); repo.compile(&args.target)?; Ok(()) } diff --git a/tools/src/lib.rs b/tools/src/lib.rs index fbe3eac..74b3f6a 100644 --- a/tools/src/lib.rs +++ b/tools/src/lib.rs @@ -3,3 +3,4 @@ pub use recipe::Recipe; pub mod repo; pub use repo::Repo; +pub use repo::RepoLoadError; diff --git a/tools/src/repo.rs b/tools/src/repo.rs index 456d8fb..ce23d06 100644 --- a/tools/src/repo.rs +++ b/tools/src/repo.rs @@ -9,14 +9,20 @@ pub struct Repo { recipes: std::collections::HashMap, } +#[derive(Debug, thiserror::Error)] +pub enum RepoLoadError { + #[error("io error: {0}")] + StdIoError(#[from] std::io::Error), +} + impl Repo { - pub fn new(path: &str) -> Self { + pub fn new(path: &str) -> Result { let mut repo = Self { path: std::string::String::from(path), recipes: std::collections::HashMap::::new(), }; - repo.add_dir(path).unwrap(); - repo + repo.add_dir(path)?; + Ok(repo) } pub fn get_recipe(self: &Self, recipe_name: &str) -> Option<&Recipe> { @@ -51,9 +57,9 @@ impl Repo { } impl Repo { - fn add_dir(self: &mut Self, path: &str) -> anyhow::Result<()> { + fn add_dir(self: &mut Self, path: &str) -> Result<(), RepoLoadError> { // println!("reading Recipes from {path}"); - let dir_entries = std::fs::read_dir(path).unwrap(); + let dir_entries = std::fs::read_dir(path)?; for dir_entry in dir_entries { let dir_entry = dir_entry.unwrap(); let file_type = dir_entry.file_type().unwrap();