1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
use std::fmt;
use std::str::FromStr;
use uuid::Uuid;
/// An identifier for a chunk.
///
/// An identifier is chosen randomly in such a way that even in
/// extremely large numbers of identifiers the likelihood of duplicate
/// identifiers is so small it can be ignored. The current
/// implementation uses UUID4 and provides a 122-bit random number.
/// For a discussion on collision likelihood, see
/// <https://en.wikipedia.org/wiki/Universally_unique_identifier#Collisions>.
///
/// We also need to be able to re-create identifiers from stored
/// values. When an identifier is formatted as a string and parsed
/// back, the result is the same value.
///
/// Because every identifier is meant to be different, there is no
/// default value, since default values should be identical.
#[derive(Debug, PartialEq, Clone)]
pub struct ChunkId {
id: String,
}
#[allow(clippy::new_without_default)]
impl ChunkId {
/// Construct a new, random identifier.
pub fn new() -> Self {
ChunkId {
id: Uuid::new_v4().to_string(),
}
}
}
impl fmt::Display for ChunkId {
/// 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)
}
}
impl FromStr for ChunkId {
type Err = ();
/// Parse a string representation of an identifier.
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(ChunkId { id: s.to_string() })
}
}
#[cfg(test)]
mod test {
use super::ChunkId;
#[test]
fn displayable() {
let id = ChunkId::new();
assert_ne!(format!("{}", id), "")
}
#[test]
fn never_the_same() {
let id1 = ChunkId::new();
let id2 = ChunkId::new();
assert_ne!(id1, id2);
}
#[test]
fn recreatable() {
let id_str = "xyzzy"; // it doesn't matter what the string representation is
let id: ChunkId = id_str.parse().unwrap();
assert_eq!(format!("{}", id), id_str);
}
#[test]
fn survives_round_trip() {
let id = ChunkId::new();
let id_str = format!("{}", id);
assert_eq!(id, id_str.parse().unwrap());
}
}
|