Skip to main content

compose_lens/model/
cgroup.rs

1//! Raw-preserving service cgroup namespace values.
2
3use super::Located;
4
5/// A service `cgroup` namespace value with a non-destructive classification.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct CgroupNamespace {
8    raw: Located<String>,
9    kind: CgroupNamespaceKind,
10}
11
12impl CgroupNamespace {
13    pub(crate) fn parse(raw: Located<String>) -> Self {
14        let kind = match raw.value().as_str() {
15            "host" => CgroupNamespaceKind::Host,
16            "private" => CgroupNamespaceKind::Private,
17            value if value.contains('$') => CgroupNamespaceKind::Expression(value.to_owned()),
18            value => CgroupNamespaceKind::Other(value.to_owned()),
19        };
20        Self { raw, kind }
21    }
22
23    /// Returns the exact authored cgroup namespace scalar and source span.
24    #[must_use]
25    pub const fn raw(&self) -> &Located<String> {
26        &self.raw
27    }
28
29    /// Returns the non-destructive namespace classification.
30    #[must_use]
31    pub const fn kind(&self) -> &CgroupNamespaceKind {
32        &self.kind
33    }
34
35    /// Reports whether the value is a documented literal or deferred expression.
36    #[must_use]
37    pub const fn is_valid(&self) -> bool {
38        matches!(
39            self.kind,
40            CgroupNamespaceKind::Host | CgroupNamespaceKind::Private | CgroupNamespaceKind::Expression(_)
41        )
42    }
43}
44
45/// The recognized family of a service `cgroup` namespace value.
46#[derive(Debug, Clone, PartialEq, Eq)]
47#[non_exhaustive]
48pub enum CgroupNamespaceKind {
49    /// Share the host cgroup namespace.
50    Host,
51    /// Request a private cgroup namespace.
52    Private,
53    /// A dollar-bearing value deferred to interpolation.
54    Expression(String),
55    /// An empty, provider-specific, or otherwise unsupported strict YAML string.
56    Other(String),
57}