diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-01-05 14:31:23 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-01-05 14:31:23 +0200 |
commit | c2f7b5af1b9a237c740c71b3b65a155a8440cf3a (patch) | |
tree | f2db3d57cbd6db1e68779907f1477dca315220c5 /src | |
parent | fee70c974049560164d8950dabc63554f55671b1 (diff) | |
download | obnam2-c2f7b5af1b9a237c740c71b3b65a155a8440cf3a.tar.gz |
refactor: add BackedUpFile to avoid using a tuple
The struct is easier to use right.
Diffstat (limited to 'src')
-rw-r--r-- | src/backup_reason.rs | 14 | ||||
-rw-r--r-- | src/cmd/list_files.rs | 7 | ||||
-rw-r--r-- | src/cmd/restore.rs | 10 | ||||
-rw-r--r-- | src/generation.rs | 38 |
4 files changed, 56 insertions, 13 deletions
diff --git a/src/backup_reason.rs b/src/backup_reason.rs index cc0d49a..218857c 100644 --- a/src/backup_reason.rs +++ b/src/backup_reason.rs @@ -2,7 +2,7 @@ use rusqlite::types::ToSqlOutput; use rusqlite::ToSql; use std::fmt; -#[derive(Debug)] +#[derive(Debug, Copy, Clone)] pub enum Reason { Skipped, IsNew, @@ -11,6 +11,18 @@ pub enum Reason { Error, } +impl Reason { + pub fn from_str(text: &str) -> Reason { + match text { + "skipped" => Reason::Skipped, + "new" => Reason::IsNew, + "changed" => Reason::Changed, + "unchanged" => Reason::Unchanged, + _ => Reason::Error, + } + } +} + impl ToSql for Reason { fn to_sql(&self) -> rusqlite::Result<ToSqlOutput> { Ok(ToSqlOutput::Owned(rusqlite::types::Value::Text(format!( diff --git a/src/cmd/list_files.rs b/src/cmd/list_files.rs index aa4bed0..a69c3df 100644 --- a/src/cmd/list_files.rs +++ b/src/cmd/list_files.rs @@ -1,3 +1,4 @@ +use crate::backup_reason::Reason; use crate::client::BackupClient; use crate::client::ClientConfig; use crate::error::ObnamError; @@ -22,8 +23,8 @@ pub fn list_files(config: &ClientConfig, gen_ref: &str) -> anyhow::Result<()> { }; let gen = client.fetch_generation(&gen_id, &dbname)?; - for (_, entry, reason) in gen.files()? { - println!("{}", format_entry(&entry, &reason)); + for file in gen.files()? { + println!("{}", format_entry(&file.entry(), file.reason())); } // Delete the temporary file. @@ -32,7 +33,7 @@ pub fn list_files(config: &ClientConfig, gen_ref: &str) -> anyhow::Result<()> { Ok(()) } -fn format_entry(e: &FilesystemEntry, reason: &str) -> String { +fn format_entry(e: &FilesystemEntry, reason: Reason) -> String { let kind = match e.kind() { FilesystemKind::Regular => "-", FilesystemKind::Directory => "d", diff --git a/src/cmd/restore.rs b/src/cmd/restore.rs index 0efdffb..c882e21 100644 --- a/src/cmd/restore.rs +++ b/src/cmd/restore.rs @@ -35,12 +35,12 @@ pub fn restore(config: &ClientConfig, gen_ref: &str, to: &Path) -> anyhow::Resul let gen = client.fetch_generation(&gen_id, &dbname)?; info!("restore file count: {}", gen.file_count()?); let progress = create_progress_bar(gen.file_count()?, true); - for (fileid, entry, _) in gen.files()? { - restore_generation(&client, &gen, fileid, &entry, &to, &progress)?; + for file in gen.files()? { + restore_generation(&client, &gen, file.fileno(), file.entry(), &to, &progress)?; } - for (_, entry, _) in gen.files()? { - if entry.is_dir() { - restore_directory_metadata(&entry, &to)?; + for file in gen.files()? { + if file.entry().is_dir() { + restore_directory_metadata(file.entry(), &to)?; } } progress.finish(); diff --git a/src/generation.rs b/src/generation.rs index 5bf749f..8a15363 100644 --- a/src/generation.rs +++ b/src/generation.rs @@ -110,6 +110,35 @@ pub struct LocalGeneration { conn: Connection, } +pub struct BackedUpFile { + fileno: FileId, + entry: FilesystemEntry, + reason: Reason, +} + +impl BackedUpFile { + pub fn new(fileno: FileId, entry: FilesystemEntry, reason: &str) -> Self { + let reason = Reason::from_str(reason); + Self { + fileno, + entry, + reason, + } + } + + pub fn fileno(&self) -> FileId { + self.fileno + } + + pub fn entry(&self) -> &FilesystemEntry { + &self.entry + } + + pub fn reason(&self) -> Reason { + self.reason + } +} + impl LocalGeneration { pub fn open<P>(filename: P) -> anyhow::Result<Self> where @@ -123,7 +152,7 @@ impl LocalGeneration { Ok(sql::file_count(&self.conn)?) } - pub fn files(&self) -> anyhow::Result<Vec<(FileId, FilesystemEntry, String)>> { + pub fn files(&self) -> anyhow::Result<Vec<BackedUpFile>> { Ok(sql::files(&self.conn)?) } @@ -141,6 +170,7 @@ impl LocalGeneration { } mod sql { + use super::BackedUpFile; use super::FileId; use crate::backup_reason::Reason; use crate::chunkid::ChunkId; @@ -214,14 +244,14 @@ mod sql { Ok(count) } - pub fn files(conn: &Connection) -> anyhow::Result<Vec<(FileId, FilesystemEntry, String)>> { + pub fn files(conn: &Connection) -> anyhow::Result<Vec<BackedUpFile>> { let mut stmt = conn.prepare("SELECT * FROM files")?; let iter = stmt.query_map(params![], |row| row_to_entry(row))?; - let mut files: Vec<(FileId, FilesystemEntry, String)> = vec![]; + let mut files = vec![]; for x in iter { let (fileno, json, reason) = x?; let entry = serde_json::from_str(&json)?; - files.push((fileno, entry, reason)); + files.push(BackedUpFile::new(fileno, entry, &reason)); } Ok(files) } |