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
//! The data version (`abi.md`, `trace.md`, *The data version*), and bytes
//! written in one.
use std::fmt;
use crate::wire;
use crate::wire::Rest;
wire! {
/// A data version: one version for the value encoding, the Tool protocol
/// and the history format together, written `"major.minor"`.
///
/// Minors only add, so a reader of one minor reads every earlier minor of
/// the same major; a change of meaning takes a new major.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct DataVersion {
/// What a change of meaning moves.
pub major: u64,
/// What an addition moves.
pub minor: u64,
}
}
wire! {
/// Bytes written in a data version: the version, then what follows,
/// which only a reader of that version reads.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Versioned {
/// The version the rest is written in.
pub version: DataVersion,
/// What follows it.
pub rest: Rest,
}
}
impl DataVersion {
/// The version `text` writes as `"major.minor"`, each a number in its
/// one decimal spelling: no sign, no leading zero, nothing else. `None`
/// for any other text.
pub fn parse(text: &str) -> Option<DataVersion> {
let (major, minor) = text.split_once('.')?;
let number = |part: &str| {
part.parse::<u64>()
.ok()
.filter(|value| value.to_string() == part)
};
Some(DataVersion {
major: number(major)?,
minor: number(minor)?,
})
}
/// Whether a reader of this version reads data written in `other`: the
/// same major, and a minor no newer than this one's.
pub fn reads(&self, other: &DataVersion) -> bool {
self.major == other.major && other.minor <= self.minor
}
/// Whether a peer of this version and one of `other` speak the Tool
/// process protocol to each other: the same major, whatever their
/// minors (`abi.md`, *The Tool process protocol*).
pub fn speaks_with(&self, other: &DataVersion) -> bool {
self.major == other.major
}
}
impl fmt::Display for DataVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}.{}", self.major, self.minor)
}
}
/// This implementation's data version. No data version is stable before the
/// first public release.
pub const DATA_VERSION: DataVersion = DataVersion { major: 6, minor: 0 };