1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/// Content-Format
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub enum ContentFormat {
  /// `text/plain; charset=utf-8`
  Text,
  /// `application/link-format`
  LinkFormat,
  /// `application/xml`
  Xml,
  /// `application/octet-stream`
  OctetStream,
  /// `application/exi`
  Exi,
  /// `application/json`
  Json,
  /// Another content format
  Other(u16),
}

impl ContentFormat {
  /// Convert this content format to the CoAP byte value
  pub fn bytes(&self) -> [u8; 2] {
    u16::from(self).to_be_bytes()
  }
}

impl<'a> From<&'a ContentFormat> for u16 {
  fn from(f: &'a ContentFormat) -> Self {
    use ContentFormat::*;
    match *f {
      | Text => 0,
      | LinkFormat => 40,
      | Xml => 41,
      | OctetStream => 42,
      | Exi => 47,
      | Json => 50,
      | Other(n) => n,
    }
  }
}

impl<'a> IntoIterator for &'a ContentFormat {
  type Item = u8;

  type IntoIter = <[u8; 2] as IntoIterator>::IntoIter;

  fn into_iter(self) -> Self::IntoIter {
    self.bytes().into_iter()
  }
}