summaryrefslogtreecommitdiff
path: root/src/generation.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-08-01 20:50:58 +0300
committerLars Wirzenius <liw@liw.fi>2021-08-01 20:55:04 +0300
commit1962cdaebc71bec28861635784e0b41deac98060 (patch)
treeb25ed37fcfb2f8805a5b298b5ba09b80922a3c11 /src/generation.rs
parent2bc07b176bcf63f94bf6f1cca0ade2505958a15d (diff)
downloadobnam2-1962cdaebc71bec28861635784e0b41deac98060.tar.gz
refactor: use a struct for GenId
This means a ChunkId can't be used instead. Sponsored-by: author
Diffstat (limited to 'src/generation.rs')
-rw-r--r--src/generation.rs33
1 files changed, 28 insertions, 5 deletions
diff --git a/src/generation.rs b/src/generation.rs
index 66b4647..5412ae7 100644
--- a/src/generation.rs
+++ b/src/generation.rs
@@ -4,13 +4,36 @@ use crate::chunkid::ChunkId;
use crate::fsentry::FilesystemEntry;
use log::debug;
use rusqlite::Connection;
+use std::fmt;
use std::path::{Path, PathBuf};
/// An identifier for a file in a generation.
type FileId = i64;
/// An identifier for a generation.
-pub type GenId = ChunkId;
+#[derive(Debug, Clone)]
+pub struct GenId {
+ id: ChunkId,
+}
+
+impl GenId {
+ pub fn from_chunk_id(id: ChunkId) -> Self {
+ Self { id }
+ }
+
+ pub fn as_chunk_id(&self) -> &ChunkId {
+ &self.id
+ }
+}
+
+impl fmt::Display for GenId {
+ /// Format an identifier for display.
+ ///
+ /// The output can be parsed to re-created an identical identifier.
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{}", self.id)
+ }
+}
/// A nascent backup generation.
///
@@ -100,21 +123,21 @@ impl NascentGeneration {
/// A generation is finished when it's on the server. It can be restored.
#[derive(Debug, Clone)]
pub struct FinishedGeneration {
- id: ChunkId,
+ id: GenId,
ended: String,
}
impl FinishedGeneration {
pub fn new(id: &str, ended: &str) -> Self {
- let id = id.parse().unwrap(); // this never fails
+ let id = GenId::from_chunk_id(id.parse().unwrap()); // this never fails
Self {
id,
ended: ended.to_string(),
}
}
- pub fn id(&self) -> ChunkId {
- self.id.clone()
+ pub fn id(&self) -> &GenId {
+ &self.id
}
pub fn ended(&self) -> &str {