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 BACKUP_ARTIFACT_V1: &'static str = "greentic.backup-artifact.v1";
33    pub const MESSAGING_ENDPOINT_V1: &'static str = "greentic.messaging-endpoint.v1";
34    pub const UPDATE_CHANNEL_V1: &'static str = "greentic.update-channel.v1";
35
36    pub fn new(s: impl Into<String>) -> Self {
37        Self(s.into())
38    }
39
40    pub fn as_str(&self) -> &str {
41        &self.0
42    }
43}
44
45impl fmt::Display for SchemaVersion {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        f.write_str(&self.0)
48    }
49}
50
51impl From<&str> for SchemaVersion {
52    fn from(s: &str) -> Self {
53        Self(s.to_string())
54    }
55}
56
57/// Semantic version wrapper (re-export with serde glue).
58#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
59#[serde(transparent)]
60pub struct SemVer(pub semver::Version);
61
62impl SemVer {
63    pub fn new(major: u64, minor: u64, patch: u64) -> Self {
64        Self(semver::Version::new(major, minor, patch))
65    }
66}
67
68impl fmt::Display for SemVer {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        write!(f, "{}", self.0)
71    }
72}
73
74impl std::str::FromStr for SemVer {
75    type Err = semver::Error;
76
77    fn from_str(s: &str) -> Result<Self, Self::Err> {
78        Ok(Self(s.parse()?))
79    }
80}