agentguard_policy/
version.rs

1//! Policy version identifiers.
2
3use serde::{Deserialize, Serialize};
4use std::fmt;
5
6/// A monotonic policy version for a single tenant.
7///
8/// Versions are simple `u64` counters. Monotonicity is the caller's
9/// responsibility — [`super::BundleRegistry::register`] does not check
10/// that a new version is strictly greater than the previous one.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
12#[serde(transparent)]
13pub struct PolicyVersion(u64);
14
15impl PolicyVersion {
16    pub fn new(v: u64) -> Self {
17        Self(v)
18    }
19
20    pub fn as_u64(&self) -> u64 {
21        self.0
22    }
23
24    /// Returns the next version (v + 1). Saturates at `u64::MAX`.
25    pub fn next(self) -> Self {
26        Self(self.0.saturating_add(1))
27    }
28}
29
30impl fmt::Display for PolicyVersion {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        write!(f, "v{}", self.0)
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn ordering() {
42        assert!(PolicyVersion::new(2) > PolicyVersion::new(1));
43        assert_eq!(PolicyVersion::new(1).next(), PolicyVersion::new(2));
44    }
45
46    #[test]
47    fn display() {
48        assert_eq!(PolicyVersion::new(42).to_string(), "v42");
49    }
50}