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