Skip to content

Commit

Permalink
feat(backup): add impl Display for BackupStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
graelo committed Aug 18, 2022
1 parent eca142d commit e9baef7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
17 changes: 14 additions & 3 deletions src/management/backup.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
//! High-level representation of a backup for catalog operations and reporting.

use std::fmt;
use std::path::PathBuf;

use chrono::NaiveDateTime;
use clap::ValueEnum;

/// Quick access, high-level representation of a backup.
///
/// `Backup` provides only information which can be derived from the file name, avoiding to open
/// the file, deal with the format, parse the metadata, etc.
///
/// This is sufficient for the [`Catalog`](crate::management::catalog::Catalog) to list backups
/// and decide whether or not a backup should be deleted or kept.
///
/// `Backup` provides only information which can be derived from the file name. This avoids to open
/// the file, deal with the format, parse the metadata, etc.
pub struct Backup {
/// Path to the backup file.
pub filepath: PathBuf,
Expand All @@ -25,6 +26,16 @@ pub struct Backup {
pub enum BackupStatus {
/// Retainable backups only.
Retainable,

/// Disposable backups only.
Disposable,
}

impl fmt::Display for BackupStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BackupStatus::Retainable => write!(f, "retainable"),
BackupStatus::Disposable => write!(f, "disposable"),
}
}
}
14 changes: 3 additions & 11 deletions src/management/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,13 @@ impl Catalog {
BackupStatus::Disposable => yellow,
BackupStatus::Retainable => green,
};
let status_str = match status {
BackupStatus::Disposable => "disposable",
BackupStatus::Retainable => "retainable",
};
let time_ago = Self::time_ago(now, backup.creation_date);

let overview = metadata.overview();
let version = &metadata.version;

println!(
"{index:3}. {color}{filename:32}{reset} {time_ago:17} {color}{status_str:12}{reset} {version:8} {overview:8}"
"{index:3}. {color}{filename:32}{reset} {time_ago:17} {color}{status:12}{reset} {version:8} {overview:8}"
);
}
} else {
Expand All @@ -323,15 +319,11 @@ impl Catalog {
BackupStatus::Disposable => yellow,
BackupStatus::Retainable => green,
};
let status_str = match status {
BackupStatus::Disposable => "disposable",
BackupStatus::Retainable => "retainable",
};
let time_ago = Self::time_ago(now, backup.creation_date);

println!(
"{index:3}. {color}{filename:32}{reset} {time_ago:17} {color}{status_str:6}{reset}"
);
"{index:3}. {color}{filename:32}{reset} {time_ago:17} {color}{status:6}{reset}"
);
}
}

Expand Down

0 comments on commit e9baef7

Please sign in to comment.