cml/
features.rs

1// Copyright 2021 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 crate::error::Error;
6use std::fmt;
7use std::str::FromStr;
8
9/// Represents the set of features a CML file is compiled with. This struct can be
10/// used to check whether a feature used in CML is enabled.
11#[derive(Debug)]
12pub struct FeatureSet(Vec<Feature>);
13
14impl FeatureSet {
15    /// Create an empty FeatureSet.
16    pub const fn empty() -> FeatureSet {
17        FeatureSet(Vec::new())
18    }
19
20    /// Tests whether `feature` is enabled.
21    pub fn has(&self, feature: &Feature) -> bool {
22        self.0.iter().find(|f| *f == feature).is_some()
23    }
24
25    /// Returns an `Err` if `feature` is not enabled.
26    pub fn check(&self, feature: Feature) -> Result<(), Error> {
27        if self.has(&feature) {
28            Ok(())
29        } else {
30            Err(Error::RestrictedFeature(feature.to_string()))
31        }
32    }
33}
34
35impl From<Vec<Feature>> for FeatureSet {
36    fn from(features: Vec<Feature>) -> FeatureSet {
37        FeatureSet(features)
38    }
39}
40
41/// A feature that can be enabled/opt-into.
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub enum Feature {
44    /// Allows `dictionary` capabilities to be used.
45    /// This is unused, and will be deleted in a followup
46    Dictionaries,
47
48    /// Allows `dictionary` capabilities with `extends: program/...` to be defined (a.k.a.
49    /// dynamic dictionaries)
50    DynamicDictionaries,
51
52    // Allows dynamic child name lengths to exceed the default limit.
53    AllowLongNames,
54
55    // Allow tests to resolve non-hermetic packages. This requires EnableAllowNonHermeticPackagesFeature
56    // to be enabled.
57    AllowNonHermeticPackages,
58
59    // Enable AllowNonHermeticPackages feature. This helps us to only enable
60    // this in-tree.
61    EnableAllowNonHermeticPackagesFeature,
62
63    // Restrict test types in facet. This helps us to only restrict this in-tree.
64    RestrictTestTypeInFacet,
65
66    // Allows customizing when the framework opens a capability when a consumer
67    // component requests to connect to the capability.
68    DeliveryType,
69}
70
71impl FromStr for Feature {
72    type Err = String;
73    fn from_str(s: &str) -> Result<Self, Self::Err> {
74        match s {
75            "dictionaries" => Ok(Feature::Dictionaries),
76            "dynamic_dictionaries" => Ok(Feature::DynamicDictionaries),
77            "allow_long_names" => Ok(Feature::AllowLongNames),
78            "allow_non_hermetic_packages" => Ok(Feature::AllowNonHermeticPackages),
79            "enable_allow_non_hermetic_packages_feature" => {
80                Ok(Feature::EnableAllowNonHermeticPackagesFeature)
81            }
82            "restrict_test_type_in_facets" => Ok(Feature::RestrictTestTypeInFacet),
83            "delivery_type" => Ok(Feature::DeliveryType),
84            _ => Err(format!("unrecognized feature \"{}\"", s)),
85        }
86    }
87}
88
89impl fmt::Display for Feature {
90    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91        f.write_str(match self {
92            Feature::Dictionaries => "dictionaries",
93            Feature::DynamicDictionaries => "dynamic_dictionaries",
94            Feature::AllowLongNames => "allow_long_names",
95            Feature::AllowNonHermeticPackages => "allow_non_hermetic_packages",
96            Feature::EnableAllowNonHermeticPackagesFeature => {
97                "enable_allow_non_hermetic_packages_feature"
98            }
99            Feature::RestrictTestTypeInFacet => "restrict_test_type_in_facets",
100            Feature::DeliveryType => "delivery_type",
101        })
102    }
103}
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108    use assert_matches::assert_matches;
109
110    #[test]
111    fn feature_is_parsed() {
112        assert_eq!(Feature::AllowLongNames, "allow_long_names".parse::<Feature>().unwrap());
113        assert_eq!(
114            Feature::AllowNonHermeticPackages,
115            "allow_non_hermetic_packages".parse::<Feature>().unwrap()
116        );
117    }
118
119    #[test]
120    fn feature_is_printed() {
121        assert_eq!("allow_long_names", Feature::AllowLongNames.to_string());
122        assert_eq!("allow_non_hermetic_packages", Feature::AllowNonHermeticPackages.to_string());
123        assert_eq!(
124            "enable_allow_non_hermetic_packages_feature",
125            Feature::EnableAllowNonHermeticPackagesFeature.to_string()
126        );
127        assert_eq!("restrict_test_type_in_facets", Feature::RestrictTestTypeInFacet.to_string());
128    }
129
130    #[test]
131    fn feature_set_has() {
132        let set = FeatureSet::empty();
133        assert!(!set.has(&Feature::AllowLongNames));
134
135        let set = FeatureSet::from(vec![Feature::AllowLongNames]);
136        assert!(set.has(&Feature::AllowLongNames));
137    }
138
139    #[test]
140    fn feature_set_check() {
141        let set = FeatureSet::empty();
142        assert_matches!(
143            set.check(Feature::AllowLongNames),
144            Err(Error::RestrictedFeature(f)) if f == "allow_long_names"
145        );
146
147        let set = FeatureSet::from(vec![Feature::AllowLongNames]);
148        assert_matches!(set.check(Feature::AllowLongNames), Ok(()));
149    }
150}