1use strum_macros::EnumString;
6use thiserror::Error;
7
8use std::fmt::Display;
9use std::str::FromStr;
10
11#[derive(Debug, Clone, Copy, PartialEq, EnumString, strum_macros::Display)]
16#[strum(serialize_all = "snake_case")]
17pub enum Feature {
18 AndroidUsb,
22 AspectRatio,
23 Ashmem,
24 BootNotifier,
25 BootNotifierCpuBoost,
26 Cooling,
27 DataCollectionConsentSync,
28 Fastrpc,
29 Framebuffer,
30 Gfxstream,
31 GoogleOdpm,
35 Gralloc,
36 HvdcpOpti,
40 Kgsl,
41 Magma,
42 MagmaSupportedVendors,
43 MmcblkStub,
44 Nanohub,
45 NetworkManager,
46 Perfetto,
47 Thermal,
48 Usbfs,
49 WakeupTest,
50
51 AdditionalMounts,
53 AndroidBootreason,
54 AndroidSerialno,
55 Bpf,
56 Container,
57 CustomArtifacts,
58 Devtmpfs,
59 EnableSuid,
60 ErrorOnFailedReboot,
61 FakeIon,
63 IoUring,
64 LogSysfsLookupMisses,
65 PingGroupRange,
66 RemotevolInProcess,
67 RootfsRw,
68 Selinux,
69 SelinuxTestSuite,
70 TestData,
71 UnifiedTracing,
72 Wifi,
73}
74
75#[derive(Debug, Error)]
77#[error("unsupported feature: {0}")]
78pub struct UnsupportedFeatureError(String);
79
80impl Feature {
81 pub fn try_parse(s: &str) -> Result<Feature, UnsupportedFeatureError> {
83 Feature::from_str(s).map_err(|_| UnsupportedFeatureError(s.to_string()))
84 }
85}
86
87#[derive(Debug, Clone, PartialEq)]
89pub struct FeatureAndArgs {
90 pub feature: Feature,
92 pub raw_args: Option<String>,
94}
95
96impl FeatureAndArgs {
97 pub fn try_parse(s: &str) -> Result<FeatureAndArgs, UnsupportedFeatureError> {
101 let (raw_flag, raw_args) =
102 s.split_once(':').map(|(f, a)| (f, Some(a.to_string()))).unwrap_or((s, None));
103 let feature = Feature::try_parse(raw_flag)?;
104 Ok(FeatureAndArgs { feature, raw_args })
105 }
106}
107
108impl Display for FeatureAndArgs {
109 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
110 let FeatureAndArgs { feature, raw_args } = self;
111 match raw_args {
112 None => feature.fmt(f),
113 Some(raw_args) => format_args!("{feature}:{raw_args}").fmt(f),
114 }
115 }
116}
117
118#[cfg(test)]
119mod test {
120 use super::*;
121
122 #[test]
123 fn feature_serde() {
124 for (feature, expected_str) in [
125 (Feature::AndroidSerialno, "android_serialno"),
126 (Feature::AndroidBootreason, "android_bootreason"),
127 (Feature::AspectRatio, "aspect_ratio"),
128 (Feature::Container, "container"),
129 (Feature::CustomArtifacts, "custom_artifacts"),
130 (Feature::Devtmpfs, "devtmpfs"),
131 (Feature::Ashmem, "ashmem"),
132 (Feature::BootNotifier, "boot_notifier"),
133 (Feature::BootNotifierCpuBoost, "boot_notifier_cpu_boost"),
134 (Feature::Framebuffer, "framebuffer"),
135 (Feature::Gralloc, "gralloc"),
136 (Feature::Kgsl, "kgsl"),
137 (Feature::Magma, "magma"),
138 (Feature::MagmaSupportedVendors, "magma_supported_vendors"),
139 (Feature::Nanohub, "nanohub"),
140 (Feature::NetworkManager, "network_manager"),
141 (Feature::Gfxstream, "gfxstream"),
142 (Feature::Bpf, "bpf"),
143 (Feature::EnableSuid, "enable_suid"),
144 (Feature::IoUring, "io_uring"),
145 (Feature::ErrorOnFailedReboot, "error_on_failed_reboot"),
146 (Feature::Perfetto, "perfetto"),
147 (Feature::PingGroupRange, "ping_group_range"),
148 (Feature::RootfsRw, "rootfs_rw"),
149 (Feature::Selinux, "selinux"),
150 (Feature::SelinuxTestSuite, "selinux_test_suite"),
151 (Feature::TestData, "test_data"),
152 (Feature::Thermal, "thermal"),
153 (Feature::Cooling, "cooling"),
154 (Feature::DataCollectionConsentSync, "data_collection_consent_sync"),
155 (Feature::HvdcpOpti, "hvdcp_opti"),
156 (Feature::Wifi, "wifi"),
157 (Feature::AdditionalMounts, "additional_mounts"),
158 (Feature::WakeupTest, "wakeup_test"),
159 (Feature::MmcblkStub, "mmcblk_stub"),
160 (Feature::AndroidUsb, "android_usb"),
161 (Feature::GoogleOdpm, "google_odpm"),
162 (Feature::Usbfs, "usbfs"),
163 (Feature::FakeIon, "fake_ion"),
165 (Feature::UnifiedTracing, "unified_tracing"),
166 (Feature::RemotevolInProcess, "remotevol_in_process"),
167 (Feature::LogSysfsLookupMisses, "log_sysfs_lookup_misses"),
168 ] {
169 let string = feature.to_string();
170 assert_eq!(string.as_str(), expected_str);
171 assert_eq!(Feature::try_parse(&string).expect("should parse"), feature);
172 }
173 }
174
175 #[test]
176 fn deserialize_feature_and_args() {
177 let FeatureAndArgs { feature, raw_args } =
178 FeatureAndArgs::try_parse("bpf:v2").expect("should parse successfully");
179 assert_eq!(feature, Feature::Bpf);
180 assert_eq!(raw_args.as_ref().expect("should be populated"), "v2");
181 }
182}