starnix_features/
lib.rs

1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use strum_macros::EnumString;
6use thiserror::Error;
7
8use std::fmt::Display;
9use std::str::FromStr;
10
11/// A feature that can be specified in a starnix kernel or container's configuration.
12#[derive(Debug, Clone, Copy, PartialEq, EnumString, strum_macros::Display)]
13#[strum(serialize_all = "snake_case")]
14pub enum Feature {
15    AndroidFdr,
16    AndroidSerialno,
17    AndroidBootreason,
18    AspectRatio,
19    Container,
20    CustomArtifacts,
21    Ashmem,
22    Framebuffer,
23    Gralloc,
24    Kgsl,
25    Magma,
26    MagmaSupportedVendors,
27    Nanohub,
28    Fastrpc,
29    NetstackMark,
30    NetworkManager,
31    Gfxstream,
32    Bpf,
33    EnableSuid,
34    IoUring,
35    ErrorOnFailedReboot,
36    Perfetto,
37    PerfettoProducer,
38    RootfsRw,
39    RtnetlinkAssumeIfb0Existence,
40    SelfProfile,
41    Selinux,
42    SelinuxTestSuite,
43    TestData,
44    Thermal,
45    HvdcpOpti,
46}
47
48/// Error returned when a feature is not recognized.
49#[derive(Debug, Error)]
50#[error("unsupported feature: {0}")]
51pub struct UnsupportedFeatureError(String);
52
53impl Feature {
54    /// Parses the name of a feature from a string.
55    pub fn try_parse(s: &str) -> Result<Feature, UnsupportedFeatureError> {
56        Feature::from_str(s).map_err(|_| UnsupportedFeatureError(s.to_string()))
57    }
58
59    /// Parses a feature and args from a string.
60    pub fn try_parse_feature_and_args(
61        s: &str,
62    ) -> Result<(Feature, Option<String>), UnsupportedFeatureError> {
63        let (raw_flag, raw_args) =
64            s.split_once(':').map(|(f, a)| (f, Some(a.to_string()))).unwrap_or((s, None));
65        Self::try_parse(raw_flag).map(|feature| (feature, raw_args))
66    }
67}
68
69/// A feature together with any arguments that go along with it, if specified.
70#[derive(Debug, Clone, PartialEq)]
71pub struct FeatureAndArgs {
72    /// The feature.
73    pub feature: Feature,
74    /// If specified, the (unparsed) arguments for the feature.
75    pub raw_args: Option<String>,
76}
77
78impl FeatureAndArgs {
79    /// Parses a feature and args from a string that separates them with `:`, e.g. "bpf:v2".
80    ///
81    /// If there is no `:` then the whole string is interpreted as the feature name.
82    pub fn try_parse(s: &str) -> Result<FeatureAndArgs, UnsupportedFeatureError> {
83        let (raw_flag, raw_args) =
84            s.split_once(':').map(|(f, a)| (f, Some(a.to_string()))).unwrap_or((s, None));
85        let feature = Feature::try_parse(raw_flag)?;
86        Ok(FeatureAndArgs { feature, raw_args })
87    }
88}
89
90impl Display for FeatureAndArgs {
91    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
92        let FeatureAndArgs { feature, raw_args } = self;
93        match raw_args {
94            None => feature.fmt(f),
95            Some(raw_args) => format_args!("{feature}:{raw_args}").fmt(f),
96        }
97    }
98}
99
100#[cfg(test)]
101mod test {
102    use super::*;
103
104    #[test]
105    fn feature_serde() {
106        for (feature, expected_str) in [
107            (Feature::AndroidFdr, "android_fdr"),
108            (Feature::AndroidSerialno, "android_serialno"),
109            (Feature::AndroidBootreason, "android_bootreason"),
110            (Feature::AspectRatio, "aspect_ratio"),
111            (Feature::Container, "container"),
112            (Feature::CustomArtifacts, "custom_artifacts"),
113            (Feature::Ashmem, "ashmem"),
114            (Feature::Framebuffer, "framebuffer"),
115            (Feature::Gralloc, "gralloc"),
116            (Feature::Kgsl, "kgsl"),
117            (Feature::Magma, "magma"),
118            (Feature::MagmaSupportedVendors, "magma_supported_vendors"),
119            (Feature::Nanohub, "nanohub"),
120            (Feature::NetstackMark, "netstack_mark"),
121            (Feature::NetworkManager, "network_manager"),
122            (Feature::Gfxstream, "gfxstream"),
123            (Feature::Bpf, "bpf"),
124            (Feature::EnableSuid, "enable_suid"),
125            (Feature::IoUring, "io_uring"),
126            (Feature::ErrorOnFailedReboot, "error_on_failed_reboot"),
127            (Feature::Perfetto, "perfetto"),
128            (Feature::PerfettoProducer, "perfetto_producer"),
129            (Feature::RootfsRw, "rootfs_rw"),
130            (Feature::RtnetlinkAssumeIfb0Existence, "rtnetlink_assume_ifb0_existence"),
131            (Feature::SelfProfile, "self_profile"),
132            (Feature::Selinux, "selinux"),
133            (Feature::SelinuxTestSuite, "selinux_test_suite"),
134            (Feature::TestData, "test_data"),
135            (Feature::Thermal, "thermal"),
136            (Feature::HvdcpOpti, "hvdcp_opti"),
137        ] {
138            let string = feature.to_string();
139            assert_eq!(string.as_str(), expected_str);
140            assert_eq!(Feature::try_parse(&string).expect("should parse"), feature);
141        }
142    }
143
144    #[test]
145    fn deserialize_feature_and_args() {
146        let FeatureAndArgs { feature, raw_args } =
147            FeatureAndArgs::try_parse("bpf:v2").expect("should parse successfully");
148        assert_eq!(feature, Feature::Bpf);
149        assert_eq!(raw_args.as_ref().expect("should be populated"), "v2");
150    }
151}