diff options
author | Lukasz Woznicki <75632179+makr11st@users.noreply.github.com> | 2022-05-17 13:30:05 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-17 13:30:05 +0100 |
commit | 2a8d917cbb377395cfeb4abcf34580541df80ace (patch) | |
tree | bc4ac1d65d227e30abe914cdb349af715edaf38d /crates/common/tedge_config | |
parent | ba49c8bdd3ca4e7ad5249a89ac90d2aad9479dfb (diff) | |
parent | f71bb3194f045110d1b5c41491caae5536a73e50 (diff) |
Merge pull request #1142 from makr11st/feature/1051_smartrest_templates
[1051] Add support for smartrest templates to config
Diffstat (limited to 'crates/common/tedge_config')
-rw-r--r-- | crates/common/tedge_config/src/models/mod.rs | 4 | ||||
-rw-r--r-- | crates/common/tedge_config/src/models/templates_set.rs | 57 | ||||
-rw-r--r-- | crates/common/tedge_config/src/settings.rs | 19 | ||||
-rw-r--r-- | crates/common/tedge_config/src/tedge_config.rs | 26 | ||||
-rw-r--r-- | crates/common/tedge_config/src/tedge_config_dto.rs | 3 |
5 files changed, 108 insertions, 1 deletions
diff --git a/crates/common/tedge_config/src/models/mod.rs b/crates/common/tedge_config/src/models/mod.rs index 4044168d..37871da7 100644 --- a/crates/common/tedge_config/src/models/mod.rs +++ b/crates/common/tedge_config/src/models/mod.rs @@ -3,4 +3,6 @@ pub mod file_path; pub mod flag; pub mod ipaddress; pub mod port; -pub use self::{connect_url::*, file_path::*, flag::*, ipaddress::*, port::*}; +pub mod templates_set; + +pub use self::{connect_url::*, file_path::*, flag::*, ipaddress::*, port::*, templates_set::*}; diff --git a/crates/common/tedge_config/src/models/templates_set.rs b/crates/common/tedge_config/src/models/templates_set.rs new file mode 100644 index 00000000..da9d32f6 --- /dev/null +++ b/crates/common/tedge_config/src/models/templates_set.rs @@ -0,0 +1,57 @@ +use std::convert::TryInto; + +/// Represents a set of smartrest templates. +/// +/// New type to add conversion methods and deduplicate provided templates. +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Eq, PartialEq)] +#[serde(transparent)] +pub struct TemplatesSet(pub Vec<String>); + +#[derive(thiserror::Error, Debug)] +#[error("TemplateSet to String conversion failed: {0:?}")] +pub struct TemplatesSetToStringConversionFailure(String); + +impl TryFrom<Vec<String>> for TemplatesSet { + type Error = TemplatesSetToStringConversionFailure; + + fn try_from(value: Vec<String>) -> Result<Self, Self::Error> { + Ok(TemplatesSet(value)) + } +} + +impl TryFrom<Vec<&str>> for TemplatesSet { + type Error = TemplatesSetToStringConversionFailure; + + fn try_from(value: Vec<&str>) -> Result<Self, Self::Error> { + Ok(TemplatesSet( + value.into_iter().map(|s| s.into()).collect::<Vec<String>>(), + )) + } +} + +impl TryInto<Vec<String>> for TemplatesSet { + type Error = TemplatesSetToStringConversionFailure; + + fn try_into(self) -> Result<Vec<String>, TemplatesSetToStringConversionFailure> { + Ok(self.0) + } +} + +impl From<TemplatesSet> for String { + fn from(val: TemplatesSet) -> Self { + val.to_string() + } +} + +impl From<String> for TemplatesSet { + fn from(val: String) -> Self { + let strings = val.split(',').map(|ss| ss.into()).collect(); + TemplatesSet(strings) + } +} + +impl std::fmt::Display for TemplatesSet { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?}", self.0) + } +} diff --git a/crates/common/tedge_config/src/settings.rs b/crates/common/tedge_config/src/settings.rs index fb018408..bca85b1a 100644 --- a/crates/common/tedge_config/src/settings.rs +++ b/crates/common/tedge_config/src/settings.rs @@ -102,6 +102,25 @@ impl ConfigSetting for C8yRootCertPathSetting { } /// +/// Smartrest templates to subsribe to. +/// +/// Example: template1,template2 +/// +#[derive(Debug)] +pub struct C8ySmartRestTemplates; + +impl ConfigSetting for C8ySmartRestTemplates { + const KEY: &'static str = "c8y.smartrest.templates"; + + const DESCRIPTION: &'static str = concat!( + "Set of SmartRest templates for the device ", + "Example: template1,template2" + ); + + type Value = TemplatesSet; +} + +/// /// Tenant endpoint URL of Azure IoT tenant. /// /// Example: MyAzure.azure-devices.net diff --git a/crates/common/tedge_config/src/tedge_config.rs b/crates/common/tedge_config/src/tedge_config.rs index f37ea0e3..9952b2d1 100644 --- a/crates/common/tedge_config/src/tedge_config.rs +++ b/crates/common/tedge_config/src/tedge_config.rs @@ -138,6 +138,32 @@ impl ConfigSettingAccessor<C8yUrlSetting> for TEdgeConfig { } } +impl ConfigSettingAccessor<C8ySmartRestTemplates> for TEdgeConfig { + fn query(&self, _setting: C8ySmartRestTemplates) -> ConfigSettingResult<TemplatesSet> { + self.data + .c8y + .smartrest_templates + .clone() + .ok_or(ConfigSettingError::ConfigNotSet { + key: C8ySmartRestTemplates::KEY, + }) + } + + fn update( + &mut self, + _setting: C8ySmartRestTemplates, + value: TemplatesSet, + ) -> ConfigSettingResult<()> { + self.data.c8y.smartrest_templates = Some(value); + Ok(()) + } + + fn unset(&mut self, _setting: C8ySmartRestTemplates) -> ConfigSettingResult<()> { + self.data.c8y.smartrest_templates = None; + Ok(()) + } +} + impl ConfigSettingAccessor<DeviceCertPathSetting> for TEdgeConfig { fn query(&self, _setting: DeviceCertPathSetting) -> ConfigSettingResult<FilePath> { Ok(self diff --git a/crates/common/tedge_config/src/tedge_config_dto.rs b/crates/common/tedge_config/src/tedge_config_dto.rs index 28cbfcbd..75b576b6 100644 --- a/crates/common/tedge_config/src/tedge_config_dto.rs +++ b/crates/common/tedge_config/src/tedge_config_dto.rs @@ -72,6 +72,9 @@ pub(crate) struct CumulocityConfigDto { /// Boolean whether Azure mapper adds timestamp or not. pub(crate) mapper_timestamp: Option<bool>, + + /// Set of c8y templates used for subscriptions. + pub(crate) smartrest_templates: Option<TemplatesSet>, } #[derive(Debug, Default, Deserialize, Serialize)] |