Skip to main content

compose_lens/model/
cpu_rt_runtime.rs

1//! Raw-preserving service real-time CPU-runtime values.
2
3use super::lifecycle::valid_stop_grace_period;
4
5/// A service `cpu_rt_runtime` scalar category with exact authored spelling.
6#[derive(Debug, Clone, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum CpuRtRuntime {
9    /// An integer microsecond spelling retained without conversion.
10    Microseconds(String),
11    /// A raw Compose duration retained without conversion.
12    Duration(String),
13    /// A dollar-bearing string retained as a deferred expression.
14    Expression(String),
15    /// An invalid or unsupported spelling retained with a dedicated diagnostic.
16    Other(String),
17}
18
19impl CpuRtRuntime {
20    pub(crate) fn parse_number(value: String, is_integer: bool) -> Self {
21        if is_integer && value.bytes().all(|byte| byte.is_ascii_digit()) {
22            Self::Microseconds(value)
23        } else {
24            Self::Other(value)
25        }
26    }
27    pub(crate) fn parse_string(value: String) -> Self {
28        if value.contains('$') {
29            Self::Expression(value)
30        } else if valid_stop_grace_period(value.trim_end_matches(['\r', '\n'])) {
31            Self::Duration(value)
32        } else {
33            Self::Other(value)
34        }
35    }
36    /// Reports whether the category is an accepted authored form.
37    #[must_use]
38    pub const fn is_valid(&self) -> bool {
39        !matches!(self, Self::Other(_))
40    }
41}