nimbus_fml/backends/
info.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::{BTreeMap, BTreeSet};
6
7use serde::Serialize;
8
9use crate::{
10    error::{FMLError, Result},
11    frontend::FeatureMetadata,
12    intermediate_representation::{FeatureDef, FeatureManifest},
13    util::loaders::FilePath,
14};
15
16#[derive(Serialize, Debug)]
17pub(crate) struct ManifestInfo {
18    file: String,
19    features: BTreeMap<String, FeatureInfo>,
20}
21
22impl ManifestInfo {
23    pub(crate) fn from(path: &FilePath, fm: &FeatureManifest) -> Self {
24        let mut features = BTreeMap::new();
25        for (fm, feature_def) in fm.iter_all_feature_defs() {
26            features.insert(
27                feature_def.name.to_string(),
28                FeatureInfo::from(fm, feature_def),
29            );
30        }
31        Self {
32            file: path.to_string(),
33            features,
34        }
35    }
36
37    pub(crate) fn from_feature(
38        path: &FilePath,
39        fm: &FeatureManifest,
40        feature_id: &str,
41    ) -> Result<Self> {
42        let (fm, feature_def) = fm
43            .find_feature(feature_id)
44            .ok_or_else(|| FMLError::InvalidFeatureError(feature_id.to_string()))?;
45        let info = FeatureInfo::from(fm, feature_def);
46        let features = BTreeMap::from([(feature_id.to_string(), info)]);
47        Ok(Self {
48            file: path.to_string(),
49            features,
50        })
51    }
52
53    pub(crate) fn to_json(&self) -> Result<String> {
54        Ok(serde_json::to_string_pretty(self)?)
55    }
56
57    pub(crate) fn to_yaml(&self) -> Result<String> {
58        Ok(serde_yaml::to_string(self)?)
59    }
60}
61
62#[derive(Serialize, Debug)]
63pub(crate) struct FeatureInfo {
64    #[serde(flatten)]
65    metadata: FeatureMetadata,
66    types: BTreeSet<String>,
67    hashes: HashInfo,
68}
69
70impl FeatureInfo {
71    fn from(fm: &FeatureManifest, feature_def: &FeatureDef) -> Self {
72        let hashes = HashInfo::from(fm, feature_def);
73        let types = fm
74            .feature_types(feature_def)
75            .iter()
76            .map(|t| t.to_string())
77            .collect();
78        let metadata = feature_def.metadata.clone();
79        Self {
80            types,
81            hashes,
82            metadata,
83        }
84    }
85}
86
87#[derive(Serialize, Debug)]
88pub(crate) struct HashInfo {
89    schema: String,
90    defaults: String,
91}
92
93impl HashInfo {
94    fn from(fm: &FeatureManifest, feature_def: &FeatureDef) -> Self {
95        let schema = fm.feature_schema_hash(feature_def);
96        let defaults = fm.feature_defaults_hash(feature_def);
97        HashInfo { schema, defaults }
98    }
99}