buswatch_types/
version.rs

1//! Schema versioning for forward compatibility.
2
3use crate::SCHEMA_VERSION;
4
5/// Schema version information embedded in snapshots.
6///
7/// This allows consumers to detect and handle format changes gracefully.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10#[cfg_attr(feature = "minicbor", derive(minicbor::Encode, minicbor::Decode))]
11pub struct SchemaVersion {
12    /// Major version - breaking changes increment this.
13    #[cfg_attr(feature = "minicbor", n(0))]
14    pub major: u32,
15
16    /// Minor version - backwards-compatible additions increment this.
17    #[cfg_attr(feature = "minicbor", n(1))]
18    pub minor: u32,
19}
20
21impl SchemaVersion {
22    /// Create a new schema version.
23    pub const fn new(major: u32, minor: u32) -> Self {
24        Self { major, minor }
25    }
26
27    /// The current schema version used by this library.
28    pub const fn current() -> Self {
29        Self {
30            major: SCHEMA_VERSION,
31            minor: 0,
32        }
33    }
34
35    /// Check if this version is compatible with the current library version.
36    ///
37    /// Returns true if the major version matches (minor differences are OK).
38    pub fn is_compatible(&self) -> bool {
39        self.major == SCHEMA_VERSION
40    }
41}
42
43impl Default for SchemaVersion {
44    fn default() -> Self {
45        Self::current()
46    }
47}