#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LicenseScheme {
Ed25519Sign,
Rsa2048Pkcs1Sign,
Rsa2048Pkcs1PssSign,
EcdsaP256Sign,
Rsa2048JwtRs256,
}
impl LicenseScheme {
pub fn parse(s: &str) -> Option<Self> {
match s {
"ED25519_SIGN" => Some(LicenseScheme::Ed25519Sign),
"RSA_2048_PKCS1_SIGN" => Some(LicenseScheme::Rsa2048Pkcs1Sign),
"RSA_2048_PKCS1_PSS_SIGN" => Some(LicenseScheme::Rsa2048Pkcs1PssSign),
"ECDSA_P256_SIGN" => Some(LicenseScheme::EcdsaP256Sign),
"RSA_2048_JWT_RS256" => Some(LicenseScheme::Rsa2048JwtRs256),
_ => None,
}
}
}
#[cfg(test)]
mod license_scheme_tests {
use super::*;
#[test]
fn parses_all_5_known_wire_strings() {
assert_eq!(
LicenseScheme::parse("ED25519_SIGN"),
Some(LicenseScheme::Ed25519Sign)
);
assert_eq!(
LicenseScheme::parse("RSA_2048_PKCS1_SIGN"),
Some(LicenseScheme::Rsa2048Pkcs1Sign)
);
assert_eq!(
LicenseScheme::parse("RSA_2048_PKCS1_PSS_SIGN"),
Some(LicenseScheme::Rsa2048Pkcs1PssSign)
);
assert_eq!(
LicenseScheme::parse("ECDSA_P256_SIGN"),
Some(LicenseScheme::EcdsaP256Sign)
);
assert_eq!(
LicenseScheme::parse("RSA_2048_JWT_RS256"),
Some(LicenseScheme::Rsa2048JwtRs256)
);
}
#[test]
fn rejects_unrecognized_scheme_string() {
assert_eq!(LicenseScheme::parse("BOGUS"), None);
assert_eq!(LicenseScheme::parse(""), None);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OverageStrategy {
NoOverage,
Allow1_25xOverage,
Allow1_5xOverage,
Allow2xOverage,
AlwaysAllowOverage,
}
impl OverageStrategy {
pub fn allows(self, count: i64, max: i64) -> bool {
match self {
OverageStrategy::NoOverage => count <= max,
OverageStrategy::Allow1_25xOverage => (count as f64) <= (max as f64) * 1.25,
OverageStrategy::Allow1_5xOverage => (count as f64) <= (max as f64) * 1.5,
OverageStrategy::Allow2xOverage => (count as f64) <= (max as f64) * 2.0,
OverageStrategy::AlwaysAllowOverage => true,
}
}
}
impl<'de> serde::Deserialize<'de> for OverageStrategy {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Ok(match s.as_str() {
"NO_OVERAGE" => OverageStrategy::NoOverage,
"ALLOW_1_25X_OVERAGE" => OverageStrategy::Allow1_25xOverage,
"ALLOW_1_5X_OVERAGE" => OverageStrategy::Allow1_5xOverage,
"ALLOW_2X_OVERAGE" => OverageStrategy::Allow2xOverage,
"ALWAYS_ALLOW_OVERAGE" => OverageStrategy::AlwaysAllowOverage,
_ => OverageStrategy::NoOverage,
})
}
}
#[cfg(test)]
mod overage_strategy_tests {
use super::*;
#[test]
fn deserializes_all_5_known_wire_strings() {
let cases = [
("\"NO_OVERAGE\"", OverageStrategy::NoOverage),
(
"\"ALLOW_1_25X_OVERAGE\"",
OverageStrategy::Allow1_25xOverage,
),
("\"ALLOW_1_5X_OVERAGE\"", OverageStrategy::Allow1_5xOverage),
("\"ALLOW_2X_OVERAGE\"", OverageStrategy::Allow2xOverage),
(
"\"ALWAYS_ALLOW_OVERAGE\"",
OverageStrategy::AlwaysAllowOverage,
),
];
for (wire, expected) in cases {
let parsed: OverageStrategy = serde_json::from_str(wire).unwrap();
assert_eq!(parsed, expected, "wire value {wire}");
}
}
#[test]
fn falls_back_to_no_overage_for_unrecognized_value() {
let parsed: OverageStrategy = serde_json::from_str("\"DENY_ACCESS\"").unwrap();
assert_eq!(parsed, OverageStrategy::NoOverage);
}
#[test]
fn no_overage_blocks_at_max_plus_one() {
assert!(!OverageStrategy::NoOverage.allows(11, 10));
assert!(OverageStrategy::NoOverage.allows(10, 10));
}
#[test]
fn allow_1_25x_permits_within_allowance_and_blocks_excess() {
assert!(OverageStrategy::Allow1_25xOverage.allows(12, 10));
assert!(!OverageStrategy::Allow1_25xOverage.allows(13, 10));
}
#[test]
fn allow_1_5x_permits_within_allowance() {
assert!(OverageStrategy::Allow1_5xOverage.allows(15, 10));
}
#[test]
fn allow_2x_permits_within_allowance() {
assert!(OverageStrategy::Allow2xOverage.allows(20, 10));
}
#[test]
fn always_allow_permits_any_count() {
assert!(OverageStrategy::AlwaysAllowOverage.allows(9999, 1));
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum HeartbeatCullStrategy {
DeactivateDead,
KeepDead,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HeartbeatResurrectionStrategy {
NoRevive,
OneMinuteRevive,
TwoMinuteRevive,
FiveMinuteRevive,
TenMinuteRevive,
FifteenMinuteRevive,
AlwaysRevive,
}
impl<'de> serde::Deserialize<'de> for HeartbeatResurrectionStrategy {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Ok(match s.as_str() {
"NO_REVIVE" => HeartbeatResurrectionStrategy::NoRevive,
"1_MINUTE_REVIVE" => HeartbeatResurrectionStrategy::OneMinuteRevive,
"2_MINUTE_REVIVE" => HeartbeatResurrectionStrategy::TwoMinuteRevive,
"5_MINUTE_REVIVE" => HeartbeatResurrectionStrategy::FiveMinuteRevive,
"10_MINUTE_REVIVE" => HeartbeatResurrectionStrategy::TenMinuteRevive,
"15_MINUTE_REVIVE" => HeartbeatResurrectionStrategy::FifteenMinuteRevive,
"ALWAYS_REVIVE" => HeartbeatResurrectionStrategy::AlwaysRevive,
_ => HeartbeatResurrectionStrategy::NoRevive,
})
}
}
#[cfg(test)]
mod heartbeat_strategy_tests {
use super::*;
#[test]
fn cull_strategy_deserializes_both_known_wire_strings() {
let deactivate: HeartbeatCullStrategy =
serde_json::from_str("\"DEACTIVATE_DEAD\"").unwrap();
assert_eq!(deactivate, HeartbeatCullStrategy::DeactivateDead);
let keep: HeartbeatCullStrategy = serde_json::from_str("\"KEEP_DEAD\"").unwrap();
assert_eq!(keep, HeartbeatCullStrategy::KeepDead);
}
#[test]
fn resurrection_strategy_deserializes_all_7_known_wire_strings() {
let cases = [
("\"NO_REVIVE\"", HeartbeatResurrectionStrategy::NoRevive),
(
"\"1_MINUTE_REVIVE\"",
HeartbeatResurrectionStrategy::OneMinuteRevive,
),
(
"\"2_MINUTE_REVIVE\"",
HeartbeatResurrectionStrategy::TwoMinuteRevive,
),
(
"\"5_MINUTE_REVIVE\"",
HeartbeatResurrectionStrategy::FiveMinuteRevive,
),
(
"\"10_MINUTE_REVIVE\"",
HeartbeatResurrectionStrategy::TenMinuteRevive,
),
(
"\"15_MINUTE_REVIVE\"",
HeartbeatResurrectionStrategy::FifteenMinuteRevive,
),
(
"\"ALWAYS_REVIVE\"",
HeartbeatResurrectionStrategy::AlwaysRevive,
),
];
for (wire, expected) in cases {
let parsed: HeartbeatResurrectionStrategy = serde_json::from_str(wire).unwrap();
assert_eq!(parsed, expected, "wire value {wire}");
}
}
#[test]
fn resurrection_strategy_falls_back_to_no_revive_for_unrecognized_value() {
let parsed: HeartbeatResurrectionStrategy =
serde_json::from_str("\"NO_RESURRECTION\"").unwrap();
assert_eq!(parsed, HeartbeatResurrectionStrategy::NoRevive);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CheckInInterval {
Day,
Week,
Month,
Year,
}
macro_rules! free_text_policy_field {
($name:ident { $($const_name:ident = $wire:literal),+ $(,)? }) => {
#[doc = concat!("Open string newtype for the `", stringify!($name), "` policy field — see the macro's own doc comment above for the general rationale.")]
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(transparent)]
pub struct $name(
/// The raw wire string — may be any value, not just the named
pub String,
);
impl $name {
$(
#[doc = concat!("`\"", $wire, "\"`.")]
pub const $const_name: &'static str = $wire;
)+
pub fn as_str(&self) -> &str {
&self.0
}
}
};
}
free_text_policy_field!(ExpirationStrategy {
RESTRICT_ACCESS = "RESTRICT_ACCESS",
MAINTAIN_ACCESS = "MAINTAIN_ACCESS",
ALLOW_ACCESS = "ALLOW_ACCESS",
});
free_text_policy_field!(RenewalBasis {
FROM_EXPIRY = "FROM_EXPIRY",
FROM_NOW = "FROM_NOW",
});
free_text_policy_field!(AuthenticationStrategy {
TOKEN = "TOKEN",
LICENSE = "LICENSE",
MIXED = "MIXED",
});
#[derive(Debug, Clone, serde::Deserialize)]
pub struct Policy {
pub id: uuid::Uuid,
#[serde(rename = "type")]
pub resource_type: String,
pub attributes: PolicyAttributes,
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct PolicyAttributes {
pub product_id: String,
pub name: String,
pub duration: Option<i64>,
pub strict: bool,
pub floating: bool,
pub scheme: Option<String>,
pub encrypted: bool,
pub use_pool: bool,
pub protected: bool,
pub require_check_in: bool,
pub check_in_interval: Option<CheckInInterval>,
pub check_in_interval_count: Option<i32>,
pub require_heartbeat: bool,
pub heartbeat_duration: Option<i32>,
pub heartbeat_cull_strategy: String,
pub heartbeat_resurrection_strategy: String,
pub machine_uniqueness_strategy: String,
pub expiration_strategy: ExpirationStrategy,
pub expiration_basis: String,
pub renewal_basis: RenewalBasis,
pub authentication_strategy: AuthenticationStrategy,
pub overage_strategy: String,
pub max_machines: Option<i32>,
pub max_cores: Option<i32>,
pub max_uses: Option<i32>,
pub max_processes: Option<i32>,
pub max_users: Option<i32>,
pub metadata: serde_json::Value,
pub created: chrono::DateTime<chrono::Utc>,
pub updated: chrono::DateTime<chrono::Utc>,
}
#[cfg(test)]
mod policy_tests {
use super::*;
#[test]
fn deserializes_a_representative_policy_with_the_real_bogus_defaults() {
let json = serde_json::json!({
"type": "policies",
"id": "01926b3e-0000-7000-8000-000000000000",
"attributes": {
"product_id": "01926b3e-1111-7000-8000-000000000000",
"name": "Default", "duration": null, "strict": false, "floating": false,
"scheme": null, "encrypted": false, "use_pool": false, "protected": false,
"require_check_in": false, "check_in_interval": null,
"check_in_interval_count": null, "require_heartbeat": false,
"heartbeat_duration": null,
"heartbeat_cull_strategy": "DEACTIVATE_DEAD",
"heartbeat_resurrection_strategy": "NO_RESURRECTION",
"machine_uniqueness_strategy": "UNIQUE_PER_LICENSE",
"expiration_strategy": "RESTRICT_ACCESS", "expiration_basis": "FROM_CREATION",
"renewal_basis": "FROM_EXPIRY", "authentication_strategy": "TOKEN",
"overage_strategy": "DENY_ACCESS",
"max_machines": null, "max_cores": null, "max_uses": null,
"max_processes": null, "max_users": null, "metadata": {},
"created": "2026-01-01T00:00:00Z", "updated": "2026-01-01T00:00:00Z",
}
});
let policy: Policy = serde_json::from_value(json).unwrap();
assert_eq!(policy.attributes.overage_strategy, "DENY_ACCESS");
assert_eq!(
policy.attributes.heartbeat_resurrection_strategy,
"NO_RESURRECTION"
);
let overage: OverageStrategy =
serde_json::from_str(&format!("\"{}\"", policy.attributes.overage_strategy)).unwrap();
assert_eq!(overage, OverageStrategy::NoOverage);
}
#[test]
fn check_in_interval_deserializes_lowercase_wire_values() {
for (wire, expected) in [
("\"day\"", CheckInInterval::Day),
("\"week\"", CheckInInterval::Week),
("\"month\"", CheckInInterval::Month),
("\"year\"", CheckInInterval::Year),
] {
let parsed: CheckInInterval = serde_json::from_str(wire).unwrap();
assert_eq!(parsed, expected, "wire value {wire}");
}
}
#[test]
fn free_text_fields_accept_unrecognized_values_without_erroring() {
let strategy: ExpirationStrategy = serde_json::from_str("\"SOME_FUTURE_VALUE\"").unwrap();
assert_eq!(strategy.as_str(), "SOME_FUTURE_VALUE");
}
#[test]
fn free_text_field_constants_match_documented_values() {
assert_eq!(ExpirationStrategy::RESTRICT_ACCESS, "RESTRICT_ACCESS");
assert_eq!(ExpirationStrategy::MAINTAIN_ACCESS, "MAINTAIN_ACCESS");
assert_eq!(ExpirationStrategy::ALLOW_ACCESS, "ALLOW_ACCESS");
assert_eq!(RenewalBasis::FROM_EXPIRY, "FROM_EXPIRY");
assert_eq!(RenewalBasis::FROM_NOW, "FROM_NOW");
assert_eq!(AuthenticationStrategy::TOKEN, "TOKEN");
assert_eq!(AuthenticationStrategy::LICENSE, "LICENSE");
assert_eq!(AuthenticationStrategy::MIXED, "MIXED");
}
}