nimbus_fml/client/
descriptor.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2* License, v. 2.0. If a copy of the MPL was not distributed with this
3* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use std::collections::BTreeSet;
6
7use email_address::EmailAddress;
8use url::Url;
9
10use crate::{frontend::DocumentationLink, intermediate_representation::FeatureDef, FmlClient};
11
12#[derive(Debug, PartialEq, Default)]
13pub struct FmlFeatureDescriptor {
14    pub(crate) id: String,
15    pub(crate) description: String,
16    pub(crate) is_coenrolling: bool,
17    pub(crate) documentation: Vec<DocumentationLink>,
18    pub(crate) contacts: Vec<EmailAddress>,
19    pub(crate) meta_bug: Option<Url>,
20    pub(crate) events: Vec<Url>,
21    pub(crate) configurator: Option<Url>,
22}
23
24impl From<&FeatureDef> for FmlFeatureDescriptor {
25    fn from(f: &FeatureDef) -> Self {
26        Self {
27            id: f.name(),
28            description: f.doc(),
29            is_coenrolling: f.allow_coenrollment,
30            documentation: f.metadata.documentation.clone(),
31            contacts: f.metadata.contacts.clone(),
32            meta_bug: f.metadata.meta_bug.clone(),
33            events: f.metadata.events.clone(),
34            configurator: f.metadata.configurator.clone(),
35        }
36    }
37}
38
39impl FmlClient {
40    pub fn get_feature_ids(&self) -> Vec<String> {
41        let mut res: BTreeSet<String> = Default::default();
42        for (_, f) in self.manifest.iter_all_feature_defs() {
43            res.insert(f.name());
44        }
45        res.into_iter().collect()
46    }
47
48    pub fn get_feature_descriptor(&self, id: String) -> Option<FmlFeatureDescriptor> {
49        let (_, f) = self.manifest.find_feature(&id)?;
50        Some(f.into())
51    }
52
53    pub fn get_feature_descriptors(&self) -> Vec<FmlFeatureDescriptor> {
54        let mut res: Vec<_> = Default::default();
55        for (_, f) in self.manifest.iter_all_feature_defs() {
56            res.push(f.into());
57        }
58        res
59    }
60}
61
62#[cfg(test)]
63mod unit_tests {
64    use super::*;
65    use crate::{client::test_helper::client, error::Result};
66
67    #[test]
68    fn test_feature_ids() -> Result<()> {
69        let client = client("./bundled_resouces.yaml", "testing")?;
70        let result = client.get_feature_ids();
71
72        assert_eq!(result, vec!["my_images", "my_strings"]);
73        Ok(())
74    }
75
76    #[test]
77    fn test_get_feature() -> Result<()> {
78        let client = client("./bundled_resouces.yaml", "testing")?;
79
80        let result = client.get_feature_descriptor("my_strings".to_string());
81        assert!(result.is_some());
82        assert_eq!(
83            result.unwrap(),
84            FmlFeatureDescriptor {
85                id: "my_strings".to_string(),
86                description: "Testing all the ways bundled text can work".to_string(),
87                is_coenrolling: false,
88                ..Default::default()
89            }
90        );
91
92        let result = client.get_feature_descriptor("my_images".to_string());
93        assert!(result.is_some());
94        assert_eq!(
95            result.unwrap(),
96            FmlFeatureDescriptor {
97                id: "my_images".to_string(),
98                description: "Testing all the ways bundled images can work".to_string(),
99                is_coenrolling: false,
100                ..Default::default()
101            }
102        );
103
104        Ok(())
105    }
106}