Skip to content

Commit

Permalink
Add a proper example for multi c8y config
Browse files Browse the repository at this point in the history
Signed-off-by: James Rhodes <[email protected]>
  • Loading branch information
jarhodes314 committed Sep 20, 2024
1 parent a879f50 commit 56525cc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/common/tedge_config_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ url = { workspace = true }

[dev-dependencies]
serde = { workspace = true, features = ["rc"] }
toml = { workspace = true }

[lints]
workspace = true
53 changes: 31 additions & 22 deletions crates/common/tedge_config_macros/examples/multi.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
use camino::Utf8PathBuf;
// use std::net::IpAddr;
// use std::net::Ipv4Addr;
use std::num::NonZeroU16;
// use std::path::PathBuf;
use tedge_config_macros::*;

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -35,7 +30,6 @@ impl<T> AppendRemoveItem for T {
}
}

#[allow(dead_code)]
define_tedge_config! {
#[tedge_config(multi)]
c8y: {
Expand All @@ -44,23 +38,38 @@ define_tedge_config! {
},
}

fn url_for<'a>(reader: &'a TEdgeConfigReader, o: Option<&str>) -> &'a str {
reader.c8y.get(o).unwrap().url.or_config_not_set().unwrap()
}

fn main() {
// let dto = TEdgeConfigDto::default();
// dto.mqtt.bind.address = Some(IpAddr::V4(Ipv4Addr::new(1, 2, 3, 4)));
let single_c8y_toml = "c8y.url = \"https://example.com\"";
let single_c8y_dto = toml::from_str(&single_c8y_toml).unwrap();
let single_c8y_reader = TEdgeConfigReader::from_dto(&single_c8y_dto, &TEdgeConfigLocation);
assert_eq!(url_for(&single_c8y_reader, None), "https://example.com");

// let config = TEdgeConfigReader::from_dto(&dto, &TEdgeConfigLocation);
let multi_c8y_toml = "c8y.cloud.url = \"https://cloud.example.com\"\nc8y.edge.url = \"https://edge.example.com\"";
let multi_c8y_dto = toml::from_str(&multi_c8y_toml).unwrap();
let multi_c8y_reader = TEdgeConfigReader::from_dto(&multi_c8y_dto, &TEdgeConfigLocation);
assert_eq!(
url_for(&multi_c8y_reader, Some("cloud")),
"https://cloud.example.com"
);
assert_eq!(
url_for(&multi_c8y_reader, Some("edge")),
"https://edge.example.com"
);

// Typed reads
// println!(
// "Device id is {}.",
// // We have to pass the config into try_read to avoid TEdgeConfigReader being
// // self-referential
// config.device.id.try_read(&config).as_ref().unwrap()
// );
// assert_eq!(u16::from(config.mqtt.bind.port), 1883);
// assert_eq!(config.mqtt.external.bind.port.or_none(), None);
// assert_eq!(
// config.read_string(ReadableKey::DeviceId).unwrap(),
// "dummy-device-id"
// );
assert!(matches!(
single_c8y_reader.c8y.get(Some("cloud")),
Err(MultiError::SingleNotMulti)
));
assert!(matches!(
multi_c8y_reader.c8y.get(Some("unknown")),
Err(MultiError::MultiKeyNotFound)
));
assert!(matches!(
multi_c8y_reader.c8y.get(None),
Err(MultiError::MultiNotSingle)
));
}
11 changes: 6 additions & 5 deletions crates/common/tedge_config_macros/src/multi.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize, doku::Document)]
#[serde(untagged)]
pub enum Multi<T> {
Single(T),
// TODO The ordering of fields is important since we have a default value for T - add a test for it
Multi(::std::collections::HashMap<String, T>),
Single(T),
}

impl<T: Default> Default for Multi<T> {
Expand Down Expand Up @@ -35,17 +36,17 @@ impl<T> Multi<T> {
match (self, key) {
(Self::Single(val), None) => Ok(val),
(Self::Multi(map), Some(key)) => map.get(key).ok_or(MultiError::MultiKeyNotFound),
(Self::Multi(_), None) => Err(MultiError::SingleNotMulti),
(Self::Single(_), Some(_key)) => Err(MultiError::MultiNotSingle),
(Self::Multi(_), None) => Err(MultiError::MultiNotSingle),
(Self::Single(_), Some(_key)) => Err(MultiError::SingleNotMulti),
}
}

pub fn get_mut(&mut self, key: Option<&str>) -> Result<&mut T, MultiError> {
match (self, key) {
(Self::Single(val), None) => Ok(val),
(Self::Multi(map), Some(key)) => map.get_mut(key).ok_or(MultiError::MultiKeyNotFound),
(Self::Multi(_), None) => Err(MultiError::SingleNotMulti),
(Self::Single(_), Some(_key)) => Err(MultiError::MultiNotSingle),
(Self::Multi(_), None) => Err(MultiError::MultiNotSingle),
(Self::Single(_), Some(_key)) => Err(MultiError::SingleNotMulti),
}
}

Expand Down

0 comments on commit 56525cc

Please sign in to comment.