mssf_core/conf/
mod.rs

1// ------------------------------------------------------------
2// Copyright (c) Microsoft Corporation.  All rights reserved.
3// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
4// ------------------------------------------------------------
5
6// Integrating service fabric config package with config-rs.
7// Wraps the SF ConfigurationPackage as a Source for config-rs.
8// config-rs can load from SF and user can use all higher level
9// features of config-rs.
10
11use config::{ConfigError, Source};
12
13use crate::runtime::config::ConfigurationPackage;
14pub use config::Config;
15
16/// Integrate with config-rs
17/// Example:
18/// let source = FabricConfigSource::new(config);
19/// let s = config::Config::builder()
20/// .add_source(source)
21/// .build()?
22///
23#[derive(Debug, Clone)]
24pub struct FabricConfigSource {
25    inner: ConfigurationPackage,
26}
27
28impl FabricConfigSource {
29    pub fn new(c: ConfigurationPackage) -> Self {
30        Self { inner: c }
31    }
32}
33
34impl Source for FabricConfigSource {
35    fn clone_into_box(&self) -> Box<dyn Source + Send + Sync> {
36        Box::new(self.clone())
37    }
38
39    fn collect(&self) -> Result<config::Map<String, config::Value>, ConfigError> {
40        let uri_origion = String::from("fabric source");
41        let mut res = config::Map::new();
42        let settings = self.inner.get_settings();
43        settings.sections.iter().for_each(|section| {
44            let section_name = section.name.to_string();
45            section.parameters.iter().for_each(|p| {
46                let param_name = p.name.to_string();
47                let param_val = p.value.to_string();
48                #[cfg(feature = "tracing")]
49                tracing::debug!(
50                    "Section: {} Param: {} Val: {}",
51                    section_name,
52                    param_name,
53                    param_val
54                );
55                let val =
56                    config::Value::new(Some(&uri_origion), config::ValueKind::String(param_val));
57                // section and param is separated by a dot.
58                res.insert(section_name.clone() + "." + &param_name, val);
59            })
60        });
61        Ok(res)
62    }
63}