Files
rust-discovery/crates/domain/src/lib.rs
T
2026-11-13 17:43:38 +01:00

80 lines
2.3 KiB
Rust

use serde::{Deserialize, Serialize};
pub mod apt_transaction;
pub mod checkpoint_repair;
pub mod inventory_document;
pub mod jobs;
pub mod reconciliation;
pub const TENANT_A: &str = "11111111-1111-4111-8111-111111111111";
pub const TENANT_B: &str = "22222222-2222-4222-8222-222222222222";
pub const MAX_MESSAGE: usize = 1024 * 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ServerRole {
Api,
Gateway,
All,
}
impl ServerRole {
pub fn parse(value: &str) -> Result<Self, &'static str> {
match value {
"api" => Ok(Self::Api),
"gateway" => Ok(Self::Gateway),
"all" => Ok(Self::All),
_ => Err("unknown server role"),
}
}
pub fn api(self) -> bool {
matches!(self, Self::Api | Self::All)
}
pub fn gateway(self) -> bool {
matches!(self, Self::Gateway | Self::All)
}
}
/// No production authentication adapter is shipped in this development slice.
pub fn validate_profile(profile: &str, dev_auth: bool) -> Result<(), &'static str> {
match (profile, dev_auth) {
("development", true) => Ok(()),
_ => Err("explicit development profile and dev auth required; production disabled"),
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Inventory {
pub os: String,
pub hostname: String,
pub architecture: String,
pub root_total_bytes: u64,
pub root_available_bytes: u64,
pub root_available_inodes: u64,
}
impl Inventory {
pub fn validate(&self) -> bool {
!self.os.is_empty()
&& self.os.len() <= 4096
&& !self.hostname.is_empty()
&& self.hostname.len() <= 255
&& self.architecture.len() <= 64
&& self.root_total_bytes > 0
&& self.root_available_bytes <= self.root_total_bytes
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn production_never_accepts_development_auth() {
assert!(validate_profile("production", true).is_err());
assert!(validate_profile("production", false).is_err());
assert!(validate_profile("development", false).is_err());
assert!(validate_profile("development", true).is_ok());
assert!(ServerRole::parse("worker").is_err());
assert!(!ServerRole::Api.gateway());
assert!(!ServerRole::Gateway.api());
}
}