diff options
Diffstat (limited to 'src/chunkid.rs')
-rw-r--r-- | src/chunkid.rs | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/src/chunkid.rs b/src/chunkid.rs index 3933d4b..50fc3d3 100644 --- a/src/chunkid.rs +++ b/src/chunkid.rs @@ -1,4 +1,9 @@ -use crate::checksummer::sha256; +//! The identifier for a chunk. +//! +//! Chunk identifiers are chosen by the server. Each chunk has a +//! unique identifier, which isn't based on the contents of the chunk. + +use crate::label::Label; use rusqlite::types::ToSqlOutput; use rusqlite::ToSql; use serde::{Deserialize, Serialize}; @@ -37,20 +42,24 @@ impl ChunkId { } } - pub fn from_str(s: &str) -> Self { + /// Re-construct an identifier from a previous value. + pub fn recreate(s: &str) -> Self { ChunkId { id: s.to_string() } } + /// Return the identifier as a slice of bytes. pub fn as_bytes(&self) -> &[u8] { self.id.as_bytes() } - pub fn sha256(&self) -> String { - sha256(self.id.as_bytes()) + /// Return the SHA256 checksum of the identifier. + pub fn sha256(&self) -> Label { + Label::sha256(self.id.as_bytes()) } } impl ToSql for ChunkId { + /// Format identifier for SQL. fn to_sql(&self) -> rusqlite::Result<ToSqlOutput> { Ok(ToSqlOutput::Owned(rusqlite::types::Value::Text( self.id.clone(), @@ -68,12 +77,14 @@ impl fmt::Display for ChunkId { } impl From<&String> for ChunkId { + /// Create a chunk identifier from a string. fn from(s: &String) -> Self { ChunkId { id: s.to_string() } } } impl From<&OsStr> for ChunkId { + /// Create a chunk identifier from an operating system string. fn from(s: &OsStr) -> Self { ChunkId { id: s.to_string_lossy().to_string(), @@ -84,8 +95,9 @@ impl From<&OsStr> for ChunkId { impl FromStr for ChunkId { type Err = (); + /// Create a chunk from a string. fn from_str(s: &str) -> Result<Self, Self::Err> { - Ok(ChunkId::from_str(s)) + Ok(ChunkId::recreate(s)) } } @@ -117,6 +129,6 @@ mod test { fn survives_round_trip() { let id = ChunkId::new(); let id_str = id.to_string(); - assert_eq!(id, ChunkId::from_str(&id_str)) + assert_eq!(id, ChunkId::recreate(&id_str)) } } |