//! A tenant's projects, owned by the platform registry. //! //! A project groups targets organisationally: a customer's product or //! machine, or a topic such as the CRA or the Data Act. It never changes what //! gets checked. Targets refer to projects by the registry's id. use serde::{Deserialize, Serialize}; /// What a project stands for. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum ProjectKind { /// A customer's product or machine. Product, /// A topic such as the CRA or the Data Act. Topic, } /// Where a product project sits in the registry's hierarchy /// (family, then product, then component; tenant-registry 0020). #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum ProjectLevel { /// A product family: no project above it. Family, /// A product or machine, below a family or at the top. Product, /// An assembly of a product, below a product or another component. Component, } /// One of a tenant's projects, as the registry lists it /// (`GET /v1/tenants/{id}/projects`). #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Project { /// The registry's id (a UUID). pub id: String, pub name: String, pub kind: ProjectKind, #[serde(default)] pub description: String, /// A product project's place in the hierarchy; `None` for a topic, and /// for a product from before the hierarchy (read as a product). #[serde(default, skip_serializing_if = "Option::is_none")] pub level: Option, /// The project above it; `None` at the top. #[serde( default, skip_serializing_if = "Option::is_none", deserialize_with = "non_empty" )] pub parent_id: Option, } /// An optional string, with an empty one read as none (the registry writes /// `""` for a detached project). fn non_empty<'de, D: serde::Deserializer<'de>>(d: D) -> Result, D::Error> { Ok(Option::::deserialize(d)?.filter(|s| !s.trim().is_empty())) } #[cfg(test)] mod tests { use super::*; #[test] fn a_registry_project_reads() { let p: Project = serde_json::from_value(serde_json::json!({ "id": "0b7d51aa-9f1c-4e33-8a1f-2d8c1e0f9a77", "name": "Rangierlok 3000", "kind": "product" })) .expect("deserialize"); assert_eq!(p.kind, ProjectKind::Product); assert_eq!(p.description, ""); assert_eq!((p.level, p.parent_id), (None, None)); } #[test] fn a_registry_project_reads_its_place_in_the_hierarchy() { let read = |parent: serde_json::Value| -> Project { serde_json::from_value(serde_json::json!({ "id": "c1", "name": "Antriebseinheit", "kind": "product", "level": "component", "parent_id": parent })) .expect("deserialize") }; let p = read(serde_json::json!("p1")); assert_eq!(p.level, Some(ProjectLevel::Component)); assert_eq!(p.parent_id.as_deref(), Some("p1")); assert_eq!(read(serde_json::json!("")).parent_id, None, "detached"); assert_eq!(read(serde_json::Value::Null).parent_id, None); } }