1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::error::Error;
use std::{fmt, str::FromStr};

/// Represents the set of features a CML file is compiled with. This struct can be
/// used to check whether a feature used in CML is enabled.
#[derive(Debug)]
pub struct FeatureSet(Vec<Feature>);

impl FeatureSet {
    /// Create an empty FeatureSet.
    pub fn empty() -> FeatureSet {
        FeatureSet(Vec::new())
    }

    /// Tests whether `feature` is enabled.
    pub fn has(&self, feature: &Feature) -> bool {
        self.0.iter().find(|f| *f == feature).is_some()
    }

    /// Returns an `Err` if `feature` is not enabled.
    pub fn check(&self, feature: Feature) -> Result<(), Error> {
        if self.has(&feature) {
            Ok(())
        } else {
            Err(Error::RestrictedFeature(feature.to_string()))
        }
    }
}

impl From<Vec<Feature>> for FeatureSet {
    fn from(features: Vec<Feature>) -> FeatureSet {
        FeatureSet(features)
    }
}

/// A feature that can be enabled/opt-into.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Feature {
    /// Allows `dictionary` capabilities to be used
    Dictionaries,

    // Allows dynamic child name lengths to exceed the default limit.
    AllowLongNames,

    // Allow tests to resolve non-hermetic packages. This requires EnableAllowNonHermeticPackagesFeature
    // to be enabled.
    AllowNonHermeticPackages,

    // Enable AllowNonHermeticPackages feature. This helps us to only enable
    // this in-tree.
    EnableAllowNonHermeticPackagesFeature,

    // Restrict test types in facet. This helps us to only restrict this in-tree.
    RestrictTestTypeInFacet,

    // Allows `config` capabilities to be used.
    ConfigCapabilities,

    // Allows customizing when the framework opens a capability when a consumer
    // component requests to connect to the capability.
    DeliveryType,
}

impl FromStr for Feature {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "dictionaries" => Ok(Feature::Dictionaries),
            "allow_long_names" => Ok(Feature::AllowLongNames),
            "allow_non_hermetic_packages" => Ok(Feature::AllowNonHermeticPackages),
            "enable_allow_non_hermetic_packages_feature" => {
                Ok(Feature::EnableAllowNonHermeticPackagesFeature)
            }
            "restrict_test_type_in_facets" => Ok(Feature::RestrictTestTypeInFacet),
            "config_capabilities" => Ok(Feature::ConfigCapabilities),
            "delivery_type" => Ok(Feature::DeliveryType),
            _ => Err(format!("unrecognized feature \"{}\"", s)),
        }
    }
}

impl fmt::Display for Feature {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Feature::Dictionaries => "dictionaries",
            Feature::AllowLongNames => "allow_long_names",
            Feature::AllowNonHermeticPackages => "allow_non_hermetic_packages",
            Feature::EnableAllowNonHermeticPackagesFeature => {
                "enable_allow_non_hermetic_packages_feature"
            }
            Feature::RestrictTestTypeInFacet => "restrict_test_type_in_facets",
            Feature::ConfigCapabilities => "config_capabilities",
            Feature::DeliveryType => "delivery_type",
        })
    }
}

#[cfg(test)]
mod tests {
    use {super::*, assert_matches::assert_matches};

    #[test]
    fn feature_is_parsed() {
        assert_eq!(Feature::AllowLongNames, "allow_long_names".parse::<Feature>().unwrap());
        assert_eq!(
            Feature::AllowNonHermeticPackages,
            "allow_non_hermetic_packages".parse::<Feature>().unwrap()
        );
    }

    #[test]
    fn feature_is_printed() {
        assert_eq!("allow_long_names", Feature::AllowLongNames.to_string());
        assert_eq!("allow_non_hermetic_packages", Feature::AllowNonHermeticPackages.to_string());
        assert_eq!(
            "enable_allow_non_hermetic_packages_feature",
            Feature::EnableAllowNonHermeticPackagesFeature.to_string()
        );
        assert_eq!("restrict_test_type_in_facets", Feature::RestrictTestTypeInFacet.to_string());
    }

    #[test]
    fn feature_set_has() {
        let set = FeatureSet::empty();
        assert!(!set.has(&Feature::AllowLongNames));

        let set = FeatureSet::from(vec![Feature::AllowLongNames]);
        assert!(set.has(&Feature::AllowLongNames));
    }

    #[test]
    fn feature_set_check() {
        let set = FeatureSet::empty();
        assert_matches!(
            set.check(Feature::AllowLongNames),
            Err(Error::RestrictedFeature(f)) if f == "allow_long_names"
        );

        let set = FeatureSet::from(vec![Feature::AllowLongNames]);
        assert_matches!(set.check(Feature::AllowLongNames), Ok(()));
    }
}