diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-02-03 09:11:49 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-02-04 09:14:01 +0200 |
commit | a2adcb5a90c15b473a2fcf114555443fba8a20ce (patch) | |
tree | 7ec36f244daa105b0da774d6705ef736f9135f64 /src/genlist.rs | |
parent | bf08ea67ca035fc0e78364450599cefff7cd9bc6 (diff) | |
download | obnam2-a2adcb5a90c15b473a2fcf114555443fba8a20ce.tar.gz |
refactor: have per-module error enums
This means that a function that parses step bindings can't return an
error that the document is missing a title. Such an error return would
be nonsensical, and we use the Rust type system to prevent it, at a
small cost of being a bit verbose.
Additional benefit is that the library portion of Obnam doesn't return
anyhow::Result values anymore.
Diffstat (limited to 'src/genlist.rs')
-rw-r--r-- | src/genlist.rs | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/genlist.rs b/src/genlist.rs index 10c614e..5eec248 100644 --- a/src/genlist.rs +++ b/src/genlist.rs @@ -5,6 +5,12 @@ pub struct GenerationList { list: Vec<FinishedGeneration>, } +#[derive(Debug, thiserror::Error)] +pub enum GenerationListError { + #[error("Unknown generation: {0}")] + UnknownGeneration(String), +} + impl GenerationList { pub fn new(gens: Vec<FinishedGeneration>) -> Self { let mut list = gens.clone(); @@ -16,7 +22,7 @@ impl GenerationList { self.list.iter() } - pub fn resolve(&self, genref: &str) -> Option<String> { + pub fn resolve(&self, genref: &str) -> Result<String, GenerationListError> { let gen = if self.list.is_empty() { None } else if genref == "latest" { @@ -36,8 +42,8 @@ impl GenerationList { } }; match gen { - None => None, - Some(gen) => Some(gen.id().to_string()), + None => Err(GenerationListError::UnknownGeneration(genref.to_string())), + Some(gen) => Ok(gen.id().to_string()), } } } |