diff --git a/examples/udp_multicast.rs b/examples/udp_multicast.rs index 5996d90..69cf2b5 100644 --- a/examples/udp_multicast.rs +++ b/examples/udp_multicast.rs @@ -9,17 +9,15 @@ async fn main() -> Result<(), FatalServerError> { env_logger::init(); // Try `coap-client -B 5 -N -m get coap://224.0.1.187/.well-known/core`. - let transport = UdpTransport::new("0.0.0.0:5683") - .enable_multicast(); + let transport = UdpTransport::new("0.0.0.0:5683").enable_multicast(); let server = CoapServer::bind(transport).await?; server .serve( - app::new() - .resource( - app::resource("/hello") - .link_attr(LINK_ATTR_RESOURCE_TYPE, "hello") - .get(handle_get_hello), - ) + app::new().resource( + app::resource("/hello") + .link_attr(LINK_ATTR_RESOURCE_TYPE, "hello") + .get(handle_get_hello), + ), ) .await } diff --git a/src/app/coap_utils.rs b/src/app/coap_utils.rs index 27d75c5..8de7804 100644 --- a/src/app/coap_utils.rs +++ b/src/app/coap_utils.rs @@ -1,60 +1,63 @@ use std::collections::HashMap; -use coap_lite::{CoapOption, CoapRequest}; use coap_lite::error::IncompatibleOptionValueFormat; use coap_lite::option_value::OptionValueType; +use coap_lite::{CoapOption, CoapRequest}; use crate::app::CoapError; pub fn request_get_queries(request: &CoapRequest) -> HashMap { - request.message.get_options_as::(CoapOption::UriQuery) - .map_or_else( - || { HashMap::new() }, - |options| { - options.into_iter() - .filter_map(Result::ok) - .map(|query| { - (query.key, query.value) - }) - .collect::>() - }) + request + .message + .get_options_as::(CoapOption::UriQuery) + .map_or_else( + || HashMap::new(), + |options| { + options + .into_iter() + .filter_map(Result::ok) + .map(|query| (query.key, query.value)) + .collect::>() + }, + ) } pub struct OptionValueQuery { - key: String, - value: String, + key: String, + value: String, } impl From for Vec { - fn from(option_value: OptionValueQuery) -> Self { - format!("{}={}", option_value.key, option_value.value).into_bytes() - } + fn from(option_value: OptionValueQuery) -> Self { + format!("{}={}", option_value.key, option_value.value).into_bytes() + } } impl TryFrom> for OptionValueQuery { - type Error = IncompatibleOptionValueFormat; + type Error = IncompatibleOptionValueFormat; - fn try_from(value: Vec) -> Result { - Self::do_try_from(value) - .map_err(|e| IncompatibleOptionValueFormat { message: e.to_string() }) - } + fn try_from(value: Vec) -> Result { + Self::do_try_from(value).map_err(|e| IncompatibleOptionValueFormat { + message: e.to_string(), + }) + } } -impl OptionValueType for OptionValueQuery { -} +impl OptionValueType for OptionValueQuery {} impl OptionValueQuery { - fn do_try_from(value: Vec) -> Result { - let mut key_value_iter = value.split(|&c| c == b'='); - let key_vec = key_value_iter.next().unwrap().to_vec(); - let value_vec = key_value_iter.next() - .ok_or_else(|| CoapError::bad_request("Missing '=', not a valid query string"))? - .to_vec(); - let key = String::from_utf8(key_vec) - .map_err(|e| CoapError::bad_request(format!("Key is not UTF-8: {e}")))?; - let value = String::from_utf8(value_vec) - .map_err(|e| CoapError::bad_request(format!("Value is not UTF-8: {e}")))?; - - Ok(OptionValueQuery { key, value }) - } -} \ No newline at end of file + fn do_try_from(value: Vec) -> Result { + let mut key_value_iter = value.split(|&c| c == b'='); + let key_vec = key_value_iter.next().unwrap().to_vec(); + let value_vec = key_value_iter + .next() + .ok_or_else(|| CoapError::bad_request("Missing '=', not a valid query string"))? + .to_vec(); + let key = String::from_utf8(key_vec) + .map_err(|e| CoapError::bad_request(format!("Key is not UTF-8: {e}")))?; + let value = String::from_utf8(value_vec) + .map_err(|e| CoapError::bad_request(format!("Value is not UTF-8: {e}")))?; + + Ok(OptionValueQuery { key, value }) + } +} diff --git a/src/app/core_handler.rs b/src/app/core_handler.rs index ecbf9fa..7445721 100644 --- a/src/app/core_handler.rs +++ b/src/app/core_handler.rs @@ -54,7 +54,7 @@ impl RequestHandler for CoreRequestHandler { fn filter_by_query(resource: &DiscoverableResource, queries: &HashMap) -> bool { for (key, value) in queries { if Some(value) != resource.attributes_as_string.get(key.as_str()) { - return false + return false; } } true diff --git a/src/app/core_link.rs b/src/app/core_link.rs index 1ee3412..c907cf0 100644 --- a/src/app/core_link.rs +++ b/src/app/core_link.rs @@ -1,8 +1,8 @@ -use std::collections::HashMap; use crate::app::resource_builder::DiscoverableResource; use coap_lite::link_format::{LinkAttributeWrite, LinkFormatWrite}; use coap_lite::ContentFormat; use dyn_clone::DynClone; +use std::collections::HashMap; use std::fmt::{Debug, Error, Write}; #[derive(Default, Debug)] diff --git a/src/app/mod.rs b/src/app/mod.rs index 6661c9a..5bde3e7 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -8,6 +8,7 @@ pub use response::Response; pub mod app_builder; pub mod app_handler; +mod coap_utils; mod core_handler; mod core_link; pub mod error; @@ -19,7 +20,6 @@ mod request_type_key; pub mod resource_builder; mod resource_handler; pub mod response; -mod coap_utils; /// Main starting point to build a robust CoAP-based application pub fn new() -> AppBuilder { diff --git a/src/udp/mod.rs b/src/udp/mod.rs index 26bbe46..df75010 100644 --- a/src/udp/mod.rs +++ b/src/udp/mod.rs @@ -41,8 +41,7 @@ pub enum MulticastGroupJoin { impl UdpTransport { pub fn new(addresses: A) -> Self { - let (mtu, multicast, multicast_joiner) = - Default::default(); + let (mtu, multicast, multicast_joiner) = Default::default(); Self { addresses, mtu, @@ -121,9 +120,7 @@ impl MulticastJoiner { socket.join_multicast_v4(addr, resolved_interface)?; } MulticastGroupJoin::Ipv6(addr, interface) => { - let resolved_interface = interface - .or(self.default_ipv6_interface) - .unwrap_or(0); + let resolved_interface = interface.or(self.default_ipv6_interface).unwrap_or(0); socket.join_multicast_v6(&addr, resolved_interface)?; } } @@ -136,7 +133,10 @@ impl MulticastJoiner { let mut joins = Vec::new(); match socket.local_addr()? { SocketAddr::V4(_) => { - joins.push(MulticastGroupJoin::Ipv4("224.0.1.187".parse().unwrap(), None)); + joins.push(MulticastGroupJoin::Ipv4( + "224.0.1.187".parse().unwrap(), + None, + )); } SocketAddr::V6(_) => { joins.push(MulticastGroupJoin::Ipv6("ff02::fd".parse().unwrap(), None));