summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-12-30 13:34:42 +0000
committerLars Wirzenius <liw@liw.fi>2020-12-30 13:34:42 +0000
commit34a881d96d4899c1c29b22b1774968d1bd0e92ed (patch)
tree48a3200af495193531ee3e0353e4f72821d97263
parentd590b44ed1481702188f9e540adf85192690333c (diff)
parent0e7a851fb6578eee5812e69bc11772142bc9fb54 (diff)
downloadobnam2-34a881d96d4899c1c29b22b1774968d1bd0e92ed.tar.gz
Merge branch 'fix' into 'main'
fix: add missing source files See merge request larswirzenius/obnam!49
-rw-r--r--src/error.rs8
-rw-r--r--src/genlist.rs43
2 files changed, 51 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs
new file mode 100644
index 0000000..1e750e5
--- /dev/null
+++ b/src/error.rs
@@ -0,0 +1,8 @@
+use thiserror::Error;
+
+/// Define all the kinds of errors any part of this crate can return.
+#[derive(Debug, Error)]
+pub enum ObnamError {
+ #[error("Can't find backup '{0}'")]
+ UnknownGeneration(String),
+}
diff --git a/src/genlist.rs b/src/genlist.rs
new file mode 100644
index 0000000..10c614e
--- /dev/null
+++ b/src/genlist.rs
@@ -0,0 +1,43 @@
+use crate::chunkid::ChunkId;
+use crate::generation::FinishedGeneration;
+
+pub struct GenerationList {
+ list: Vec<FinishedGeneration>,
+}
+
+impl GenerationList {
+ pub fn new(gens: Vec<FinishedGeneration>) -> Self {
+ let mut list = gens.clone();
+ list.sort_by_cached_key(|gen| gen.ended().to_string());
+ Self { list }
+ }
+
+ pub fn iter(&self) -> impl Iterator<Item = &FinishedGeneration> {
+ self.list.iter()
+ }
+
+ pub fn resolve(&self, genref: &str) -> Option<String> {
+ let gen = if self.list.is_empty() {
+ None
+ } else if genref == "latest" {
+ let i = self.list.len() - 1;
+ Some(self.list[i].clone())
+ } else {
+ let genref: ChunkId = genref.parse().unwrap();
+ let hits: Vec<FinishedGeneration> = self
+ .iter()
+ .filter(|gen| gen.id() == genref)
+ .map(|gen| gen.clone())
+ .collect();
+ if hits.len() == 1 {
+ Some(hits[0].clone())
+ } else {
+ None
+ }
+ };
+ match gen {
+ None => None,
+ Some(gen) => Some(gen.id().to_string()),
+ }
+ }
+}