Skip to main content

flow_abi/
version.rs

1//! The data version (`abi.md`, `trace.md`, *The data version*), and bytes
2//! written in one.
3
4use std::fmt;
5
6use crate::wire;
7use crate::wire::Rest;
8
9wire! {
10    /// A data version: one version for the value encoding, the Tool protocol
11    /// and the history format together, written `"major.minor"`.
12    ///
13    /// Minors only add, so a reader of one minor reads every earlier minor of
14    /// the same major; a change of meaning takes a new major.
15    #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
16    pub struct DataVersion {
17        /// What a change of meaning moves.
18        pub major: u64,
19        /// What an addition moves.
20        pub minor: u64,
21    }
22}
23
24wire! {
25    /// Bytes written in a data version: the version, then what follows,
26    /// which only a reader of that version reads.
27    #[derive(Clone, PartialEq, Eq, Debug)]
28    pub struct Versioned {
29        /// The version the rest is written in.
30        pub version: DataVersion,
31        /// What follows it.
32        pub rest: Rest,
33    }
34}
35
36impl DataVersion {
37    /// The version `text` writes as `"major.minor"`, each a number in its
38    /// one decimal spelling: no sign, no leading zero, nothing else. `None`
39    /// for any other text.
40    pub fn parse(text: &str) -> Option<DataVersion> {
41        let (major, minor) = text.split_once('.')?;
42        let number = |part: &str| {
43            part.parse::<u64>()
44                .ok()
45                .filter(|value| value.to_string() == part)
46        };
47        Some(DataVersion {
48            major: number(major)?,
49            minor: number(minor)?,
50        })
51    }
52
53    /// Whether a reader of this version reads data written in `other`: the
54    /// same major, and a minor no newer than this one's.
55    pub fn reads(&self, other: &DataVersion) -> bool {
56        self.major == other.major && other.minor <= self.minor
57    }
58
59    /// Whether a peer of this version and one of `other` speak the Tool
60    /// process protocol to each other: the same major, whatever their
61    /// minors (`abi.md`, *The Tool process protocol*).
62    pub fn speaks_with(&self, other: &DataVersion) -> bool {
63        self.major == other.major
64    }
65}
66
67impl fmt::Display for DataVersion {
68    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69        write!(f, "{}.{}", self.major, self.minor)
70    }
71}
72
73/// This implementation's data version. No data version is stable before the
74/// first public release.
75pub const DATA_VERSION: DataVersion = DataVersion { major: 6, minor: 0 };