toad_msg/msg/opt/known/
content_format.rs

1/// Content-Format
2#[non_exhaustive]
3#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
4pub enum ContentFormat {
5  /// `text/plain; charset=utf-8`
6  Text,
7  /// `application/link-format`
8  LinkFormat,
9  /// `application/xml`
10  Xml,
11  /// `application/octet-stream`
12  OctetStream,
13  /// `application/exi`
14  Exi,
15  /// `application/json`
16  Json,
17  /// Another content format
18  Other(u16),
19}
20
21impl ContentFormat {
22  /// Convert this content format to the CoAP byte value
23  pub fn bytes(&self) -> [u8; 2] {
24    u16::from(self).to_be_bytes()
25  }
26}
27
28impl<'a> From<&'a ContentFormat> for u16 {
29  fn from(f: &'a ContentFormat) -> Self {
30    use ContentFormat::*;
31    match *f {
32      | Text => 0,
33      | LinkFormat => 40,
34      | Xml => 41,
35      | OctetStream => 42,
36      | Exi => 47,
37      | Json => 50,
38      | Other(n) => n,
39    }
40  }
41}
42
43impl From<u16> for ContentFormat {
44  fn from(n: u16) -> Self {
45    use ContentFormat::*;
46    match n {
47      | 0 => Text,
48      | 40 => LinkFormat,
49      | 41 => Xml,
50      | 42 => OctetStream,
51      | 47 => Exi,
52      | 50 => Json,
53      | n => Other(n),
54    }
55  }
56}
57
58impl<'a> IntoIterator for &'a ContentFormat {
59  type Item = u8;
60
61  type IntoIter = <[u8; 2] as IntoIterator>::IntoIter;
62
63  fn into_iter(self) -> Self::IntoIter {
64    self.bytes().into_iter()
65  }
66}