#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ScopeObject {
#[serde(skip_serializing_if = "Option::is_none")]
pub product: Option<uuid::Uuid>,
#[serde(skip_serializing_if = "Option::is_none")]
pub policy: Option<uuid::Uuid>,
#[serde(skip_serializing_if = "Option::is_none")]
pub user: Option<uuid::Uuid>,
#[serde(skip_serializing_if = "Option::is_none")]
pub environment: Option<uuid::Uuid>,
#[serde(skip_serializing_if = "Option::is_none")]
pub entitlements: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub fingerprint: Option<String>,
#[serde(skip_serializing)]
#[deprecated(
note = "the server refuses this field with `422 SCOPE_NOT_SUPPORTED`, failing the whole \
validate call, so this SDK never sends it. Setting it is silently discarded and \
constrains nothing — remove it, or move the check to a field the server enforces \
(`entitlements`, `fingerprint`, `product`, `policy`, `user`, `environment`)."
)]
pub version: Option<String>,
#[serde(skip_serializing)]
#[deprecated(
note = "the server refuses this field with `422 SCOPE_NOT_SUPPORTED`, failing the whole \
validate call, so this SDK never sends it. Setting it is silently discarded and \
constrains nothing — remove it, or move the check to a field the server enforces \
(`entitlements`, `fingerprint`, `product`, `policy`, `user`, `environment`)."
)]
pub checksum: Option<String>,
}
#[derive(Debug, Clone)]
pub struct ValidationResult {
pub license: crate::models::license::LicenseResource,
pub meta: ValidationMeta,
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct ValidationMeta {
pub ts: chrono::DateTime<chrono::Utc>,
pub valid: bool,
pub detail: String,
pub code: ValidationCode,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValidationCode {
Valid,
NotFound,
Banned,
Suspended,
Expired,
Overdue,
EntitlementsMissing,
TooManyMachines,
TooManyCores,
TooMuchMemory,
TooMuchDisk,
TooManyProcesses,
TooManyUsers,
HeartbeatDead,
HeartbeatNotStarted,
ProductScopeMismatch,
PolicyScopeMismatch,
UserScopeMismatch,
FingerprintScopeMismatch,
ComponentsScopeMismatch,
ChecksumScopeMismatch,
VersionScopeMismatch,
EnvironmentScopeMismatch,
Unknown(String),
}
impl<'de> serde::Deserialize<'de> for ValidationCode {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Ok(match s.as_str() {
"VALID" => ValidationCode::Valid,
"NOT_FOUND" => ValidationCode::NotFound,
"BANNED" => ValidationCode::Banned,
"SUSPENDED" => ValidationCode::Suspended,
"EXPIRED" => ValidationCode::Expired,
"OVERDUE" => ValidationCode::Overdue,
"ENTITLEMENTS_MISSING" => ValidationCode::EntitlementsMissing,
"TOO_MANY_MACHINES" => ValidationCode::TooManyMachines,
"TOO_MANY_CORES" => ValidationCode::TooManyCores,
"TOO_MUCH_MEMORY" => ValidationCode::TooMuchMemory,
"TOO_MUCH_DISK" => ValidationCode::TooMuchDisk,
"TOO_MANY_PROCESSES" => ValidationCode::TooManyProcesses,
"TOO_MANY_USERS" => ValidationCode::TooManyUsers,
"HEARTBEAT_DEAD" => ValidationCode::HeartbeatDead,
"HEARTBEAT_NOT_STARTED" => ValidationCode::HeartbeatNotStarted,
"PRODUCT_SCOPE_MISMATCH" => ValidationCode::ProductScopeMismatch,
"POLICY_SCOPE_MISMATCH" => ValidationCode::PolicyScopeMismatch,
"USER_SCOPE_MISMATCH" => ValidationCode::UserScopeMismatch,
"FINGERPRINT_SCOPE_MISMATCH" => ValidationCode::FingerprintScopeMismatch,
"COMPONENTS_SCOPE_MISMATCH" => ValidationCode::ComponentsScopeMismatch,
"CHECKSUM_SCOPE_MISMATCH" => ValidationCode::ChecksumScopeMismatch,
"VERSION_SCOPE_MISMATCH" => ValidationCode::VersionScopeMismatch,
"ENVIRONMENT_SCOPE_MISMATCH" => ValidationCode::EnvironmentScopeMismatch,
other => ValidationCode::Unknown(other.to_string()),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
fn all_known_wire_pairs() -> Vec<(&'static str, ValidationCode)> {
vec![
("VALID", ValidationCode::Valid),
("NOT_FOUND", ValidationCode::NotFound),
("BANNED", ValidationCode::Banned),
("SUSPENDED", ValidationCode::Suspended),
("EXPIRED", ValidationCode::Expired),
("OVERDUE", ValidationCode::Overdue),
("ENTITLEMENTS_MISSING", ValidationCode::EntitlementsMissing),
("TOO_MANY_MACHINES", ValidationCode::TooManyMachines),
("TOO_MANY_CORES", ValidationCode::TooManyCores),
("TOO_MUCH_MEMORY", ValidationCode::TooMuchMemory),
("TOO_MUCH_DISK", ValidationCode::TooMuchDisk),
("TOO_MANY_PROCESSES", ValidationCode::TooManyProcesses),
("TOO_MANY_USERS", ValidationCode::TooManyUsers),
("HEARTBEAT_DEAD", ValidationCode::HeartbeatDead),
("HEARTBEAT_NOT_STARTED", ValidationCode::HeartbeatNotStarted),
(
"PRODUCT_SCOPE_MISMATCH",
ValidationCode::ProductScopeMismatch,
),
("POLICY_SCOPE_MISMATCH", ValidationCode::PolicyScopeMismatch),
("USER_SCOPE_MISMATCH", ValidationCode::UserScopeMismatch),
(
"FINGERPRINT_SCOPE_MISMATCH",
ValidationCode::FingerprintScopeMismatch,
),
(
"COMPONENTS_SCOPE_MISMATCH",
ValidationCode::ComponentsScopeMismatch,
),
(
"CHECKSUM_SCOPE_MISMATCH",
ValidationCode::ChecksumScopeMismatch,
),
(
"VERSION_SCOPE_MISMATCH",
ValidationCode::VersionScopeMismatch,
),
(
"ENVIRONMENT_SCOPE_MISMATCH",
ValidationCode::EnvironmentScopeMismatch,
),
]
}
#[test]
fn deserializes_all_23_known_wire_strings() {
let pairs = all_known_wire_pairs();
assert_eq!(pairs.len(), 23, "must cover all 23 server-declared codes");
for (wire, expected) in pairs {
let json = format!("\"{wire}\"");
let parsed: ValidationCode = serde_json::from_str(&json).unwrap();
assert_eq!(parsed, expected, "wire value {wire}");
}
}
#[test]
fn deserializes_unknown_value_to_unknown_variant() {
let parsed: ValidationCode = serde_json::from_str("\"SOME_FUTURE_CODE\"").unwrap();
assert_eq!(
parsed,
ValidationCode::Unknown("SOME_FUTURE_CODE".to_string())
);
}
#[test]
fn too_many_uses_is_retired_and_falls_back_to_unknown() {
let parsed: ValidationCode = serde_json::from_str("\"TOO_MANY_USES\"").unwrap();
assert_eq!(parsed, ValidationCode::Unknown("TOO_MANY_USES".to_string()));
}
#[test]
fn scope_object_serializes_only_set_fields() {
let scope = ScopeObject {
product: Some(uuid::Uuid::nil()),
..Default::default()
};
let value = serde_json::to_value(&scope).unwrap();
let obj = value.as_object().unwrap();
assert_eq!(obj.len(), 1, "only `product` was set: {obj:?}");
assert!(obj.contains_key("product"));
}
#[test]
#[allow(deprecated)]
fn scope_object_never_serializes_the_two_refused_fields() {
let scope = ScopeObject {
product: Some(uuid::Uuid::nil()),
version: Some("1.2.3".to_string()),
checksum: Some("deadbeef".to_string()),
..Default::default()
};
let value = serde_json::to_value(&scope).unwrap();
assert_eq!(value, serde_json::json!({ "product": uuid::Uuid::nil() }));
}
#[test]
#[allow(deprecated)]
fn scope_object_still_deserializes_the_two_refused_fields() {
let scope: ScopeObject =
serde_json::from_value(serde_json::json!({ "version": "1.2.3" })).unwrap();
assert_eq!(scope.version.as_deref(), Some("1.2.3"));
}
#[test]
fn scope_object_serializes_the_enforced_entitlements_and_fingerprint() {
let scope = ScopeObject {
entitlements: Some(vec!["pro".to_string()]),
fingerprint: Some("fp-abc123".to_string()),
..Default::default()
};
let value = serde_json::to_value(&scope).unwrap();
assert_eq!(
value,
serde_json::json!({ "entitlements": ["pro"], "fingerprint": "fp-abc123" })
);
}
#[test]
fn scope_object_empty_serializes_to_empty_object() {
let scope = ScopeObject::default();
let value = serde_json::to_value(&scope).unwrap();
assert_eq!(value, serde_json::json!({}));
}
#[test]
fn validation_meta_deserializes_from_representative_response() {
let json = serde_json::json!({
"ts": "2026-01-01T00:00:00Z",
"valid": true,
"detail": "is valid",
"code": "VALID",
});
let meta: ValidationMeta = serde_json::from_value(json).unwrap();
assert!(meta.valid);
assert_eq!(meta.detail, "is valid");
assert_eq!(meta.code, ValidationCode::Valid);
}
}