Skip to main content

phoxal_model/component/
mod.rs

1//! Canonical component facts used after authored documents are loaded.
2//!
3//! Runtime consumers use these unversioned values through [`crate::Robot`].
4
5pub mod capability;
6
7use std::collections::BTreeMap;
8
9use crate::identity::CapabilityId;
10use crate::structure::Structure;
11use capability::Capability;
12
13/// One component *type*: the capabilities and structure every instance of it
14/// has.
15#[derive(phoxal_macros::DescribeWire, serde::Serialize, serde::Deserialize, Debug, Clone)]
16#[serde(deny_unknown_fields)]
17pub struct Component {
18    capabilities: BTreeMap<CapabilityId, Capability>,
19    structure: Structure,
20}
21
22impl Component {
23    pub(crate) fn new(
24        capabilities: BTreeMap<CapabilityId, Capability>,
25        structure: Structure,
26    ) -> Self {
27        Self {
28            capabilities,
29            structure,
30        }
31    }
32
33    /// The named capability, if this type declares it.
34    #[must_use]
35    pub fn capability(&self, capability_id: &str) -> Option<&Capability> {
36        self.capabilities.get(capability_id)
37    }
38
39    /// Every declared capability, ordered by capability id.
40    pub fn capabilities(&self) -> impl ExactSizeIterator<Item = (&CapabilityId, &Capability)> {
41        self.capabilities.iter()
42    }
43
44    /// The component's own structure, in component-local identities.
45    #[must_use]
46    pub fn structure(&self) -> &Structure {
47        &self.structure
48    }
49}