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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use core::marker::PhantomData;
use core::str::Utf8Error;

use toad_array::Array;
use toad_macros::rfc_7252_doc;

use crate::{observe,
            Code,
            ContentFormat,
            Id,
            Message,
            MessageOptions,
            OptNumber,
            OptValue,
            OptionMap,
            Payload,
            SetOptionError,
            Token,
            Type,
            Version};

/// Builder for [`Message`]s
#[derive(Debug, Clone)]
pub struct MessageBuilder<PayloadBytes, Options> {
  id: Option<Id>,
  token: Option<Token>,
  ty: Type,
  code: Code,
  opts: Options,
  payload: Option<Payload<PayloadBytes>>,
}

impl<C, O> MessageBuilder<C, O>
  where O: OptionMap,
        C: Array<Item = u8>
{
  /// Create a new builder
  pub fn new(ty: Type, code: Code) -> Self {
    Self { id: None,
           token: None,
           ty,
           code,
           opts: Default::default(),
           payload: None }
  }

  /// Sets message [`Type`]
  pub fn ty(self, ty: Type) -> Self {
    Self { ty, ..self }
  }

  /// Sets message [`Code`]
  pub fn code(self, code: Code) -> Self {
    Self { code, ..self }
  }

  /// Sets message [`Payload`]
  pub fn payload(self, p: C) -> Self {
    Self { payload: Some(Payload(p)),
           ..self }
  }

  /// Sets message [`Id`]
  pub fn id(self, id: Id) -> Self {
    Self { id: Some(id),
           ..self }
  }

  /// Sets message [`Token`]
  pub fn token(self, token: Token) -> Self {
    Self { token: Some(token),
           ..self }
  }

  /// Update the value for the [Uri-Host](opt::known::no_repeat::HOST) option,
  /// discarding any existing values.
  pub fn host<S>(mut self, host: S) -> Self
    where S: AsRef<str>
  {
    self.opts = self.opts.set_host(host).unwrap();
    self
  }

  /// [`opt::known::no_repeat::BLOCK1`]
  pub fn block1(mut self, size: u16, num: u32, more: bool) -> Self {
    self.opts = self.opts.set_block1(size, num, more).unwrap();
    self
  }

  /// [`opt::known::no_repeat::BLOCK2`]
  pub fn block2(mut self, size: u16, num: u32, more: bool) -> Self {
    self.opts = self.opts.set_block2(size, num, more).unwrap();
    self
  }

  /// Update the value for the [Uri-Port](opt::known::no_repeat::PORT) option,
  /// discarding any existing values.
  pub fn port(mut self, port: u16) -> Self {
    self.opts = self.opts.set_port(port).unwrap();
    self
  }

  /// Update the value for the [Uri-Path](opt::known::no_repeat::PATH) option,
  /// discarding any existing values.
  pub fn path<S>(mut self, path: S) -> Self
    where S: AsRef<str>
  {
    self.opts = self.opts.set_path(path).unwrap();
    self
  }

  /// Update the value for the [Content-Format](opt::known::no_repeat::CONTENT_FORMAT) option,
  /// discarding any existing values.
  #[doc = rfc_7252_doc!("5.10.3")]
  pub fn content_format(mut self, format: ContentFormat) -> Self {
    self.opts = self.opts.set_content_format(format).unwrap();
    self
  }

  /// Set the value for the [Observe](opt::known::no_repeat::OBSERVE) option,
  /// discarding any existing values.
  pub fn observe(mut self, a: observe::Action) -> Self {
    self.opts = self.opts.set_observe(a).unwrap();
    self
  }

  /// Update the value for the [Accept](opt::known::no_repeat::ACCEPT) option,
  /// discarding any existing values.
  pub fn accept(mut self, format: ContentFormat) -> Self {
    self.opts = self.opts.set_accept(format).unwrap();
    self
  }

  /// Update the value for the [Size1](opt::known::no_repeat::SIZE1) option,
  /// discarding any existing values.
  #[doc = rfc_7252_doc!("5.10.9")]
  pub fn size1(mut self, size_bytes: u64) -> Self {
    self.opts = self.opts.set_size1(size_bytes).unwrap();
    self
  }

  /// Update the value for the [Size2](opt::known::no_repeat::SIZE2) option,
  /// discarding any existing values.
  pub fn size2(mut self, size_bytes: u64) -> Self {
    self.opts = self.opts.set_size2(size_bytes).unwrap();
    self
  }

  /// Discard all values for [If-Match](opt::known::repeat::IF_MATCH), and replace them with
  /// an empty value.
  ///
  /// This signals that our request should only be processed if we're trying to update
  /// a resource that exists (e.g. this ensures PUT only updates and will never insert)
  #[doc = rfc_7252_doc!("5.10.8.1")]
  pub fn if_exists(mut self) -> Self {
    self.opts = self.opts.set_if_exists().unwrap();
    self
  }

  /// Enable the [If-None-Match](opt::known::no_repeat::IF_NONE_MATCH) flag
  ///
  /// This signals that our request should only be processed if we're trying to insert
  /// a resource that does not exist (e.g. this ensures PUT only inserts and will never update)
  #[doc = rfc_7252_doc!("5.10.8.2")]
  pub fn if_not_exists(mut self) -> Self {
    self.opts = self.opts.set_if_not_exists().unwrap();
    self
  }

  /// Update the value for the [Max-Age](opt::known::no_repeat::MAX_AGE) option,
  /// discarding any existing values.
  #[doc = rfc_7252_doc!("5.10.5")]
  pub fn max_age(mut self, max_age_seconds: u32) -> Self {
    self.opts = self.opts.set_max_age(max_age_seconds).unwrap();
    self
  }

  /// Update the value for the [Proxy-Uri](opt::known::no_repeat::PROXY_URI) option,
  /// discarding any existing values.
  #[doc = rfc_7252_doc!("5.10.2")]
  pub fn proxy_uri<S>(mut self, uri: S) -> Self
    where S: AsRef<str>
  {
    self.opts = self.opts.set_proxy_uri(uri).unwrap();
    self
  }

  /// Insert a new value for the [Uri-Query](opt::known::repeat::QUERY) option,
  /// alongside any existing values.
  pub fn add_query<S>(mut self, query: S) -> Self
    where S: AsRef<str>
  {
    self.opts = self.opts.add_query(query).unwrap();
    self
  }

  /// Insert a new value for the [If-Match](opt::known::repeat::IF_MATCH) option,
  /// alongside any existing values.
  #[doc = rfc_7252_doc!("5.10.8.1")]
  pub fn add_if_match<B>(mut self, tag: B) -> Self
    where B: AsRef<[u8]>
  {
    self.opts = self.opts.add_if_match(tag).unwrap();
    self
  }

  /// Insert a new value for the [Location-Path](opt::known::repeat::LOCATION_PATH) option,
  /// alongside any existing values.
  #[doc = rfc_7252_doc!("5.10.7")]
  pub fn add_location_path<S>(mut self, path: S) -> Self
    where S: AsRef<str>
  {
    self.opts = self.opts.add_location_path(path).unwrap();
    self
  }

  /// Insert a new value for the [Location-Query](opt::known::repeat::LOCATION_QUERY) option,
  /// alongside any existing values.
  #[doc = rfc_7252_doc!("5.10.7")]
  pub fn add_location_query<S>(mut self, query: S) -> Self
    where S: AsRef<str>
  {
    self.opts = self.opts.add_location_query(query).unwrap();
    self
  }

  /// Insert a new value for the [ETag](opt::known::repeat::ETAG) option,
  /// alongside any existing values.
  #[doc = rfc_7252_doc!("5.10.7")]
  pub fn add_etag<B>(mut self, tag: B) -> Self
    where B: AsRef<[u8]>
  {
    self.opts = self.opts.add_etag(tag).unwrap();
    self
  }
}

impl<C, O> MessageBuilder<C, O>
  where O: OptionMap,
        C: Array<Item = u8>
{
  /// Consume the builder, returning the built message
  pub fn build(self) -> Message<C, O> {
    Message { ver: Version::default(),
              ty: self.ty,
              code: self.code,
              id: self.id.unwrap_or(Id(0)),
              token: self.token.unwrap_or(Token(Default::default())),
              opts: self.opts,
              payload: self.payload.unwrap_or(Payload(Default::default())) }
  }
}