Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jasta committed Apr 15, 2022
1 parent bc9dc70 commit fae8fd0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 55 deletions.
14 changes: 6 additions & 8 deletions examples/udp_multicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
79 changes: 41 additions & 38 deletions src/app/coap_utils.rs
Original file line number Diff line number Diff line change
@@ -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<Endpoint>(request: &CoapRequest<Endpoint>) -> HashMap<String, String> {
request.message.get_options_as::<OptionValueQuery>(CoapOption::UriQuery)
.map_or_else(
|| { HashMap::new() },
|options| {
options.into_iter()
.filter_map(Result::ok)
.map(|query| {
(query.key, query.value)
})
.collect::<HashMap<_, _>>()
})
request
.message
.get_options_as::<OptionValueQuery>(CoapOption::UriQuery)
.map_or_else(
|| HashMap::new(),
|options| {
options
.into_iter()
.filter_map(Result::ok)
.map(|query| (query.key, query.value))
.collect::<HashMap<_, _>>()
},
)
}

pub struct OptionValueQuery {
key: String,
value: String,
key: String,
value: String,
}

impl From<OptionValueQuery> for Vec<u8> {
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<Vec<u8>> for OptionValueQuery {
type Error = IncompatibleOptionValueFormat;
type Error = IncompatibleOptionValueFormat;

fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
Self::do_try_from(value)
.map_err(|e| IncompatibleOptionValueFormat { message: e.to_string() })
}
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
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<u8>) -> Result<Self, CoapError> {
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 })
}
}
fn do_try_from(value: Vec<u8>) -> Result<Self, CoapError> {
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 })
}
}
2 changes: 1 addition & 1 deletion src/app/core_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<Endpoint: Send + 'static> RequestHandler<Endpoint> for CoreRequestHandler {
fn filter_by_query(resource: &DiscoverableResource, queries: &HashMap<String, String>) -> bool {
for (key, value) in queries {
if Some(value) != resource.attributes_as_string.get(key.as_str()) {
return false
return false;
}
}
true
Expand Down
2 changes: 1 addition & 1 deletion src/app/core_link.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Endpoint>() -> AppBuilder<Endpoint> {
Expand Down
12 changes: 6 additions & 6 deletions src/udp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ pub enum MulticastGroupJoin {

impl<A: ToSocketAddrs> UdpTransport<A> {
pub fn new(addresses: A) -> Self {
let (mtu, multicast, multicast_joiner) =
Default::default();
let (mtu, multicast, multicast_joiner) = Default::default();
Self {
addresses,
mtu,
Expand Down Expand Up @@ -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)?;
}
}
Expand All @@ -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));
Expand Down

0 comments on commit fae8fd0

Please sign in to comment.