Skip to main content

greentic_deploy_spec/
version.rs

1//! Schema versioning primitives.
2//!
3//! Each top-level spec type carries a `schema` field holding a [`SchemaVersion`]
4//! discriminator (e.g. `greentic.environment.v1`). The constants below name the
5//! canonical schema string for every type in this crate; consumers should match
6//! against `SchemaVersion::ENVIRONMENT_V1` rather than the raw string.
7//!
8//! [`SemVer`] wraps [`semver::Version`] for places where finer-grained version
9//! tracking is wanted (revision pack lists, descriptors, etc.).
10
11use serde::{Deserialize, Serialize};
12use std::fmt;
13
14/// Top-level schema discriminator string (e.g. `greentic.environment.v1`).
15#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
16#[serde(transparent)]
17pub struct SchemaVersion(pub String);
18
19impl SchemaVersion {
20    pub const ENVIRONMENT_V1: &'static str = "greentic.environment.v1";
21    pub const ENVIRONMENT_RUNTIME_V1: &'static str = "greentic.environment-runtime.v1";
22    pub const REVISION_V1: &'static str = "greentic.revision.v1";
23    pub const TRAFFIC_SPLIT_V1: &'static str = "greentic.traffic-split.v1";
24    pub const BUNDLE_DEPLOYMENT_V1: &'static str = "greentic.bundle-deployment.v1";
25    pub const REVENUE_POLICY_V1: &'static str = "greentic.revenue-policy.v1";
26    pub const CREDENTIALS_V1: &'static str = "greentic.credentials.v1";
27    pub const PACK_CONFIG_V1: &'static str = "greentic.pack-config.v1";
28    pub const RUNTIME_CONFIG_V1: &'static str = "greentic.runtime-config.v1";
29    pub const PACK_LIST_LOCK_V1: &'static str = "greentic.pack-list-lock.v1";
30    pub const AUDIT_EVENT_V1: &'static str = "greentic.audit.event.v1";
31    pub const BACKUP_MANIFEST_V1: &'static str = "greentic.backup-manifest.v1";
32    pub const MESSAGING_ENDPOINT_V1: &'static str = "greentic.messaging-endpoint.v1";
33
34    pub fn new(s: impl Into<String>) -> Self {
35        Self(s.into())
36    }
37
38    pub fn as_str(&self) -> &str {
39        &self.0
40    }
41}
42
43impl fmt::Display for SchemaVersion {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        f.write_str(&self.0)
46    }
47}
48
49impl From<&str> for SchemaVersion {
50    fn from(s: &str) -> Self {
51        Self(s.to_string())
52    }
53}
54
55/// Semantic version wrapper (re-export with serde glue).
56#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
57#[serde(transparent)]
58pub struct SemVer(pub semver::Version);
59
60impl SemVer {
61    pub fn new(major: u64, minor: u64, patch: u64) -> Self {
62        Self(semver::Version::new(major, minor, patch))
63    }
64}
65
66impl fmt::Display for SemVer {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68        write!(f, "{}", self.0)
69    }
70}
71
72impl std::str::FromStr for SemVer {
73    type Err = semver::Error;
74
75    fn from_str(s: &str) -> Result<Self, Self::Err> {
76        Ok(Self(s.parse()?))
77    }
78}