mssf_core/runtime/
config.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
6use crate::{WString, error::ErrorCode, strings::StringResult};
7use mssf_com::{
8    FabricRuntime::IFabricConfigurationPackage,
9    FabricTypes::{
10        FABRIC_CONFIGURATION_PARAMETER, FABRIC_CONFIGURATION_PARAMETER_EX1,
11        FABRIC_CONFIGURATION_SECTION,
12    },
13};
14
15#[derive(Debug, Clone)]
16pub struct ConfigurationPackage {
17    com: IFabricConfigurationPackage,
18}
19
20pub struct ConfigurationPackageDesc {
21    pub name: WString,
22    pub service_manifest_name: WString,
23    pub service_manifest_version: WString,
24    pub version: WString,
25}
26
27pub struct ConfigurationSettings {
28    pub sections: Vec<ConfigurationSection>,
29}
30
31impl From<IFabricConfigurationPackage> for ConfigurationPackage {
32    fn from(com: IFabricConfigurationPackage) -> Self {
33        Self { com }
34    }
35}
36
37impl From<ConfigurationPackage> for IFabricConfigurationPackage {
38    fn from(value: ConfigurationPackage) -> Self {
39        value.com
40    }
41}
42
43impl ConfigurationPackage {
44    pub fn get_description(&self) -> ConfigurationPackageDesc {
45        let raw = unsafe { self.com.get_Description().as_ref().unwrap() };
46
47        ConfigurationPackageDesc {
48            name: WString::from(raw.Name),
49            service_manifest_name: WString::from(raw.ServiceManifestName),
50            service_manifest_version: WString::from(raw.ServiceManifestVersion),
51            version: WString::from(raw.Version),
52        }
53    }
54
55    pub fn get_settings(&self) -> ConfigurationSettings {
56        let sections = unsafe { self.com.get_Settings().as_ref() }
57            .map(|list| {
58                unsafe { list.Sections.as_ref() }
59                    .map(|l| crate::iter::vec_from_raw_com(l.Count as usize, l.Items))
60                    .unwrap_or_default()
61            })
62            .unwrap_or_default();
63        ConfigurationSettings { sections }
64    }
65
66    pub fn get_path(&self) -> WString {
67        let raw = unsafe { self.com.get_Path() };
68        WString::from(raw)
69    }
70
71    pub fn get_section(&self, section_name: &WString) -> crate::Result<ConfigurationSection> {
72        let raw = unsafe { self.com.GetSection(section_name.as_pcwstr()) }?;
73        let raw_ref = unsafe { raw.as_ref() };
74        match raw_ref {
75            Some(c) => Ok(ConfigurationSection::from(c)),
76            None => Err(ErrorCode::E_POINTER.into()),
77        }
78    }
79
80    pub fn get_value(
81        &self,
82        section_name: &WString,
83        parameter_name: &WString,
84    ) -> crate::Result<(WString, bool)> {
85        let mut is_encrypted: u8 = Default::default();
86        let raw = unsafe {
87            self.com.GetValue(
88                section_name.as_pcwstr(),
89                parameter_name.as_pcwstr(),
90                std::ptr::addr_of_mut!(is_encrypted),
91            )
92        }?;
93        Ok((WString::from(raw), is_encrypted != 0))
94    }
95
96    pub fn decrypt_value(&self, encryptedvalue: &WString) -> crate::Result<WString> {
97        let s = unsafe { self.com.DecryptValue(encryptedvalue.as_pcwstr()) }?;
98        Ok(StringResult::from(&s).into_inner())
99    }
100}
101
102pub struct ConfigurationSection {
103    pub name: WString,
104    pub parameters: Vec<ConfigurationParameter>,
105}
106
107impl From<&FABRIC_CONFIGURATION_SECTION> for ConfigurationSection {
108    fn from(value: &FABRIC_CONFIGURATION_SECTION) -> Self {
109        let parameters = unsafe { value.Parameters.as_ref() }
110            .map(|list| crate::iter::vec_from_raw_com(list.Count as usize, list.Items))
111            .unwrap_or_default();
112        Self {
113            name: WString::from(value.Name),
114            parameters,
115        }
116    }
117}
118
119#[derive(Debug)]
120pub struct ConfigurationParameter {
121    pub is_encrypted: bool,
122    pub must_overrride: bool,
123    pub name: WString,
124    pub value: WString,
125    pub r#type: WString,
126}
127
128impl From<&FABRIC_CONFIGURATION_PARAMETER> for ConfigurationParameter {
129    fn from(value: &FABRIC_CONFIGURATION_PARAMETER) -> Self {
130        let raw1 = unsafe {
131            (value.Reserved as *const FABRIC_CONFIGURATION_PARAMETER_EX1)
132                .as_ref()
133                .unwrap()
134        };
135        Self {
136            name: WString::from(value.Name),
137            is_encrypted: value.IsEncrypted,
138            must_overrride: value.MustOverride,
139            value: WString::from(value.Value),
140            r#type: WString::from(raw1.Type),
141        }
142    }
143}