Skip to main content

derec_library/
protocol_version.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 DeRec Alliance. All rights reserved.
3
4use std::fmt;
5
6/// DeRec protocol version supported by this SDK.
7///
8/// This type represents the protocol-level version carried in the
9/// [`DeRecMessage`](derec_proto::DeRecMessage) envelope:
10///
11/// - `protocolVersionMajor`
12/// - `protocolVersionMinor`
13///
14/// This is **not** the same as the Rust crate version, npm package version,
15/// or NuGet package version. Package versions identify SDK releases, while
16/// `ProtocolVersion` identifies the DeRec wire protocol version expected by
17/// protocol messages.
18///
19/// # Fields
20///
21/// * `major` - Protocol major version.
22/// * `minor` - Protocol minor version.
23///
24/// # Compatibility
25///
26/// The compatibility policy associated with protocol major/minor versions is
27/// defined by the DeRec protocol specification, not by this helper type.
28///
29/// # Example
30///
31/// ```rust
32/// use derec_library::protocol_version::ProtocolVersion;
33///
34/// let version = ProtocolVersion::current();
35///
36/// assert!(version.major >= 0);
37/// assert!(version.minor >= 0);
38/// ```
39#[derive(Debug, Clone, Copy, PartialEq, Eq)]
40pub struct ProtocolVersion {
41    pub major: u32,
42    pub minor: u32,
43}
44
45impl ProtocolVersion {
46    /// Returns the current DeRec protocol version supported by this SDK.
47    ///
48    /// This value corresponds to the protocol version that should be written
49    /// into the `protocolVersionMajor` and `protocolVersionMinor` fields of
50    /// the `DeRecMessage` envelope.
51    ///
52    /// This function reports the supported **protocol** version only. It does
53    /// not expose the crate or package release version.
54    ///
55    /// # Returns
56    ///
57    /// Returns the current [`ProtocolVersion`] supported by this SDK.
58    ///
59    /// # Example
60    ///
61    /// ```rust
62    /// use derec_library::protocol_version::ProtocolVersion;
63    ///
64    /// let version = ProtocolVersion::current();
65    ///
66    /// println!("DeRec protocol version: {}.{}", version.major, version.minor);
67    /// ```
68    pub const fn current() -> Self {
69        CURRENT
70    }
71}
72
73impl fmt::Display for ProtocolVersion {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        write!(f, "{}.{}", self.major, self.minor)
76    }
77}
78
79/// Current DeRec protocol version supported by this SDK.
80///
81/// This constant is the source of truth for the protocol version reported by
82/// [`ProtocolVersion::current`].
83///
84/// # Example
85///
86/// ```rust
87/// use derec_library::protocol_version::{CURRENT, ProtocolVersion};
88///
89/// assert_eq!(CURRENT, ProtocolVersion::current());
90/// ```
91pub const CURRENT: ProtocolVersion = ProtocolVersion { major: 0, minor: 0 };