From 5959955f38ea9fda63e51c96a18facd390904e54 Mon Sep 17 00:00:00 2001 From: Alexander Weiss Date: Fri, 16 Sep 2022 14:37:50 +0200 Subject: [PATCH] impl Display for SnapshotGroup --- src/commands/forget.rs | 2 +- src/commands/snapshots.rs | 2 +- src/repo/snapshotfile.rs | 29 ++++++++++++++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/commands/forget.rs b/src/commands/forget.rs index 4f150ee..2b24cf5 100644 --- a/src/commands/forget.rs +++ b/src/commands/forget.rs @@ -84,7 +84,7 @@ pub(super) async fn execute( for (group, mut snapshots) in groups { if !group.is_empty() { - println!("snapshots for {:?}", group); + println!("snapshots for {group}"); } snapshots.sort_unstable_by(|sn1, sn2| sn1.cmp(sn2).reverse()); let latest_time = snapshots[0].time; diff --git a/src/commands/snapshots.rs b/src/commands/snapshots.rs index 0f0374b..fdcd70d 100644 --- a/src/commands/snapshots.rs +++ b/src/commands/snapshots.rs @@ -79,7 +79,7 @@ pub(super) async fn execute( for (group, mut snapshots) in groups { if !group.is_empty() { - println!("\nsnapshots for {:?}", group); + println!("\nsnapshots for {group}"); } snapshots.sort_unstable(); let count = snapshots.len(); diff --git a/src/repo/snapshotfile.rs b/src/repo/snapshotfile.rs index 1a9df24..0e30bcc 100644 --- a/src/repo/snapshotfile.rs +++ b/src/repo/snapshotfile.rs @@ -1,5 +1,6 @@ -use std::cmp::Ordering; +use std::fmt; use std::str::FromStr; +use std::{cmp::Ordering, fmt::Display}; use anyhow::{anyhow, bail, Result}; use chrono::{DateTime, Local}; @@ -366,6 +367,25 @@ pub struct SnapshotGroup { tags: Option, } +impl Display for SnapshotGroup { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let mut out = Vec::new(); + + if let Some(host) = &self.hostname { + out.push(format!("host [{host}]")); + } + if let Some(paths) = &self.paths { + out.push(format!("paths [{paths}]")); + } + if let Some(tags) = &self.tags { + out.push(format!("tags [{tags}]")); + } + + write!(f, "({})", out.join(", "))?; + Ok(()) + } +} + impl SnapshotGroup { pub fn from_sn(sn: &SnapshotFile, crit: &SnapshotGroupCriterion) -> Self { Self { @@ -390,6 +410,13 @@ impl FromStr for StringList { } } +impl Display for StringList { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.0.join(","))?; + Ok(()) + } +} + impl StringList { pub fn contains(&self, s: &String) -> bool { self.0.contains(s)