From f089212f4c2e5898f13ff77882aaa8d18d1bb36c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Mon, 30 Sep 2024 17:24:37 +0200 Subject: [PATCH] wording, style, spelling --- miniconf/examples/cli.rs | 9 ++++++--- miniconf/src/error.rs | 2 +- miniconf/src/iter.rs | 2 +- miniconf_mqtt/Cargo.toml | 1 + miniconf_mqtt/src/lib.rs | 29 +++++++++++------------------ 5 files changed, 20 insertions(+), 23 deletions(-) diff --git a/miniconf/examples/cli.rs b/miniconf/examples/cli.rs index a2d9c293..dd9149e1 100644 --- a/miniconf/examples/cli.rs +++ b/miniconf/examples/cli.rs @@ -1,10 +1,14 @@ -use anyhow::{Context, Result}; +use anyhow::Context; use miniconf::{Error, JsonCoreSlash, Path, Traversal, TreeKey}; mod common; use common::Settings; -fn main() -> Result<()> { +// Simple command line interface example for miniconf. +// This exposes all leaf nodes as long options, parses the command line, +// and then prints the settings struct as a list of option key-value pairs. + +fn main() -> anyhow::Result<()> { let mut settings = Settings::default(); settings.enable(); @@ -15,7 +19,6 @@ fn main() -> Result<()> { let value = args.next().context("missing value")?; settings .set_json_by_key(&Path::<_, '-'>(key), value.as_bytes()) - .map_err(anyhow::Error::msg) .context("lookup/deserialize")?; } diff --git a/miniconf/src/error.rs b/miniconf/src/error.rs index 4305c2fa..49deadec 100644 --- a/miniconf/src/error.rs +++ b/miniconf/src/error.rs @@ -47,7 +47,7 @@ impl Display for Traversal { write!(f, "Variant absent (depth: {depth})") } Traversal::TooShort(depth) => { - write!(f, "Key does not read a leaf (depth: {depth})") + write!(f, "Key does not reach a leaf (depth: {depth})") } Traversal::NotFound(depth) => { write!(f, "Key not found (depth: {depth})") diff --git a/miniconf/src/iter.rs b/miniconf/src/iter.rs index d36927d0..e321244f 100644 --- a/miniconf/src/iter.rs +++ b/miniconf/src/iter.rs @@ -159,7 +159,7 @@ where Some(Err(depth)) } // TooLong: impossible due to Consume - // Absent, Finalization, Invalid, Access: not returned by traverse(traverse_by_key()) + // Absent, Finalization, Invalid, Access: not returned by transcode (traverse_by_key()) _ => unreachable!(), }; } diff --git a/miniconf_mqtt/Cargo.toml b/miniconf_mqtt/Cargo.toml index 68ee2753..f9c587c3 100644 --- a/miniconf_mqtt/Cargo.toml +++ b/miniconf_mqtt/Cargo.toml @@ -19,6 +19,7 @@ embedded-io = "0.6" log = "0.4" heapless = "0.8" serde-json-core = "0.6.0" +strum = { version = "0.26.3", features = ["derive"] } [[example]] name = "mqtt" diff --git a/miniconf_mqtt/src/lib.rs b/miniconf_mqtt/src/lib.rs index 994a7f6a..e24ddc2f 100644 --- a/miniconf_mqtt/src/lib.rs +++ b/miniconf_mqtt/src/lib.rs @@ -17,6 +17,7 @@ use minimq::{ types::{Properties, SubscriptionOptions, TopicFilter}, ConfigBuilder, DeferredPublication, ProtocolError, Publication, QoS, }; +use strum::IntoStaticStr; use embedded_io::Write; @@ -33,8 +34,6 @@ const DUMP_TIMEOUT_SECONDS: u32 = 2; const SEPARATOR: char = '/'; -type Iter = NodeIter, SEPARATOR>>; - /// Miniconf MQTT joint error type #[derive(Debug, PartialEq)] pub enum Error { @@ -114,7 +113,7 @@ mod sm { /// Cache correlation data and topic for multi-part responses. struct Multipart { - iter: Iter, + iter: NodeIter, SEPARATOR>>, response_topic: Option>, correlation_data: Option>, } @@ -164,7 +163,7 @@ impl, const Y: usize> TryFrom<&minimq::types::Properties<'_>> for } } -#[derive(Debug, Copy, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq, IntoStaticStr)] enum ResponseCode { Ok, Continue, @@ -173,15 +172,9 @@ enum ResponseCode { impl From for minimq::Property<'static> { fn from(value: ResponseCode) -> Self { - let string = match value { - ResponseCode::Ok => "Ok", - ResponseCode::Continue => "Continue", - ResponseCode::Error => "Error", - }; - minimq::Property::UserProperty( minimq::types::Utf8String("code"), - minimq::types::Utf8String(string), + minimq::types::Utf8String(value.into()), ) } } @@ -342,7 +335,7 @@ where } } // All states must handle MQTT traffic. - self.poll(settings).map(|c| c == Changed::Changed) + self.poll(settings).map(|c| c == State::Changed) } fn alive(&mut self) -> Result<(), minimq::PubError> { @@ -500,7 +493,7 @@ where }) } - fn poll(&mut self, settings: &mut Settings) -> Result> { + fn poll(&mut self, settings: &mut Settings) -> Result> { let Self { mqtt, state, @@ -515,7 +508,7 @@ where .map(Path::<_, SEPARATOR>::from) else { info!("Unexpected topic: {topic}"); - return Changed::Unchanged; + return State::Unchanged; }; if payload.is_empty() { @@ -562,7 +555,7 @@ where } } } - Changed::Unchanged + State::Unchanged } else { // Set settings @@ -578,7 +571,7 @@ where minimq::Error::SessionReset => { warn!("Session reset"); self.state.process_event(sm::Events::Reset).unwrap(); - Ok(Changed::Unchanged) + Ok(State::Unchanged) } other => Err(other.into()), }) @@ -586,13 +579,13 @@ where } #[derive(Default, Copy, Clone, PartialEq, PartialOrd)] -enum Changed { +enum State { #[default] Unchanged, Changed, } -impl From for Changed { +impl From for State { fn from(value: bool) -> Self { if value { Self::Changed