Skip to content

Commit

Permalink
Add unknown option variant
Browse files Browse the repository at this point in the history
This allows infallible conversions and lets users handle CoAP options
that are unknown to us.
  • Loading branch information
martindisch committed Sep 12, 2019
1 parent 976ec8f commit 4f989ed
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 46 deletions.
17 changes: 0 additions & 17 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,6 @@ impl error::Error for MessageError {
}
}

/// The error that can occur when parsing an option.
#[derive(Debug, PartialEq)]
pub struct InvalidOption;

impl fmt::Display for InvalidOption {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "CoAP error: invalid option number")
}
}

#[cfg(feature = "std")]
impl error::Error for InvalidOption {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
None
}
}

/// The error that can occur when parsing a content-format.
#[derive(Debug, PartialEq)]
pub struct InvalidContentFormat;
Expand Down
56 changes: 27 additions & 29 deletions src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use alloc::{
use core::convert::TryFrom;

use super::{
error::{
InvalidContentFormat, InvalidObserve, InvalidOption, MessageError,
},
error::{InvalidContentFormat, InvalidObserve, MessageError},
header::{Header, HeaderRaw, MessageClass},
};

Expand Down Expand Up @@ -43,35 +41,34 @@ pub enum CoapOption {
Size1,
Size2,
NoResponse,
Unknown(usize),
}

impl TryFrom<usize> for CoapOption {
type Error = InvalidOption;

fn try_from(number: usize) -> Result<CoapOption, InvalidOption> {
impl From<usize> for CoapOption {
fn from(number: usize) -> CoapOption {
match number {
1 => Ok(CoapOption::IfMatch),
3 => Ok(CoapOption::UriHost),
4 => Ok(CoapOption::ETag),
5 => Ok(CoapOption::IfNoneMatch),
6 => Ok(CoapOption::Observe),
7 => Ok(CoapOption::UriPort),
8 => Ok(CoapOption::LocationPath),
9 => Ok(CoapOption::Oscore),
11 => Ok(CoapOption::UriPath),
12 => Ok(CoapOption::ContentFormat),
14 => Ok(CoapOption::MaxAge),
15 => Ok(CoapOption::UriQuery),
17 => Ok(CoapOption::Accept),
20 => Ok(CoapOption::LocationQuery),
23 => Ok(CoapOption::Block2),
27 => Ok(CoapOption::Block1),
35 => Ok(CoapOption::ProxyUri),
39 => Ok(CoapOption::ProxyScheme),
60 => Ok(CoapOption::Size1),
28 => Ok(CoapOption::Size2),
258 => Ok(CoapOption::NoResponse),
_ => Err(InvalidOption),
1 => CoapOption::IfMatch,
3 => CoapOption::UriHost,
4 => CoapOption::ETag,
5 => CoapOption::IfNoneMatch,
6 => CoapOption::Observe,
7 => CoapOption::UriPort,
8 => CoapOption::LocationPath,
9 => CoapOption::Oscore,
11 => CoapOption::UriPath,
12 => CoapOption::ContentFormat,
14 => CoapOption::MaxAge,
15 => CoapOption::UriQuery,
17 => CoapOption::Accept,
20 => CoapOption::LocationQuery,
23 => CoapOption::Block2,
27 => CoapOption::Block1,
35 => CoapOption::ProxyUri,
39 => CoapOption::ProxyScheme,
60 => CoapOption::Size1,
28 => CoapOption::Size2,
258 => CoapOption::NoResponse,
_ => CoapOption::Unknown(number),
}
}
}
Expand Down Expand Up @@ -100,6 +97,7 @@ impl From<CoapOption> for usize {
CoapOption::Size1 => 60,
CoapOption::Size2 => 28,
CoapOption::NoResponse => 258,
CoapOption::Unknown(number) => number,
}
}
}
Expand Down

0 comments on commit 4f989ed

Please sign in to comment.