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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//! Attributes that are defined in [RFC 5245].
//!
//! [RFC 5245]: https://tools.ietf.org/html/rfc5245
use crate::attribute::{Attribute, AttributeType};
use bytecodec::fixnum::{U32beDecoder, U32beEncoder, U64beDecoder, U64beEncoder};
use bytecodec::null::{NullDecoder, NullEncoder};
use bytecodec::{ByteCount, Decode, Encode, Eos, Result, SizedEncode, TryTaggedDecode};

macro_rules! impl_decode {
    ($decoder:ty, $item:ident, $and_then:expr) => {
        impl Decode for $decoder {
            type Item = $item;

            fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
                track!(self.0.decode(buf, eos))
            }

            fn finish_decoding(&mut self) -> Result<Self::Item> {
                track!(self.0.finish_decoding()).and_then($and_then)
            }

            fn requiring_bytes(&self) -> ByteCount {
                self.0.requiring_bytes()
            }

            fn is_idle(&self) -> bool {
                self.0.is_idle()
            }
        }
        impl TryTaggedDecode for $decoder {
            type Tag = AttributeType;

            fn try_start_decoding(&mut self, attr_type: Self::Tag) -> Result<bool> {
                Ok(attr_type.as_u16() == $item::CODEPOINT)
            }
        }
    };
}

macro_rules! impl_encode {
    ($encoder:ty, $item:ty, $map_from:expr) => {
        impl Encode for $encoder {
            type Item = $item;

            fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize> {
                track!(self.0.encode(buf, eos))
            }

            #[allow(clippy::redundant_closure_call)]
            fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
                track!(self.0.start_encoding($map_from(item)))
            }

            fn requiring_bytes(&self) -> ByteCount {
                self.0.requiring_bytes()
            }

            fn is_idle(&self) -> bool {
                self.0.is_idle()
            }
        }
        impl SizedEncode for $encoder {
            fn exact_requiring_bytes(&self) -> u64 {
                self.0.exact_requiring_bytes()
            }
        }
    };
}

/// `PRIORITY` attribute.
///
/// See [RFC 5245 -- 7.1.2.1 PRIORITY] about this attribute.
///
/// [RFC 5245 -- 7.1.2.1 PRIORITY]: https://tools.ietf.org/html/rfc5245#section-7.1.2.1
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Priority(u32);
impl Priority {
    /// The codepoint of the type of the attribute.
    pub const CODEPOINT: u16 = 0x0024;

    /// Makes a new `Priority` instance.
    pub fn new(prio: u32) -> Self {
        Priority(prio)
    }

    /// Returns the alternate address.
    pub fn prio(&self) -> u32 {
        self.0
    }
}
impl Attribute for Priority {
    type Decoder = PriorityDecoder;
    type Encoder = PriorityEncoder;

    fn get_type(&self) -> AttributeType {
        AttributeType::new(Self::CODEPOINT)
    }
}

/// [`Priority`] decoder.
#[derive(Debug, Default)]
pub struct PriorityDecoder(U32beDecoder);

impl PriorityDecoder {
    /// Makes a new `PriorityDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_decode!(PriorityDecoder, Priority, |prio| Ok(Priority(prio)));

/// [`Priority`] encoder.
#[derive(Debug, Default)]
pub struct PriorityEncoder(U32beEncoder);

impl PriorityEncoder {
    /// Makes a new `PriorityEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_encode!(PriorityEncoder, Priority, |item: Self::Item| item.0);

/// `USE-CANDIDATE` attribute.
///
/// See [RFC 5245 -- 7.1.2.1 USE-CANDIDATE] about this attribute.
///
/// [RFC 5245 -- 7.1.2.1 USE-CANDIDATE]: https://tools.ietf.org/html/rfc5245#section-7.1.2.1
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct UseCandidate;
impl UseCandidate {
    /// The codepoint of the type of the attribute.
    pub const CODEPOINT: u16 = 0x0025;

    /// Makes a new `UseCandidate` instance.
    pub fn new() -> Self {
        UseCandidate
    }
}
impl Attribute for UseCandidate {
    type Decoder = UseCandidateDecoder;
    type Encoder = UseCandidateEncoder;

    fn get_type(&self) -> AttributeType {
        AttributeType::new(Self::CODEPOINT)
    }
}
impl Default for UseCandidate {
    fn default() -> Self {
        Self::new()
    }
}

/// [`UseCandidate`] decoder.
#[derive(Debug, Default)]
pub struct UseCandidateDecoder(NullDecoder);

impl UseCandidateDecoder {
    /// Makes a new `UseCandidateDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_decode!(UseCandidateDecoder, UseCandidate, |_| Ok(UseCandidate));

/// [`UseCandidate`] encoder.
#[derive(Debug, Default)]
pub struct UseCandidateEncoder(NullEncoder);

impl UseCandidateEncoder {
    /// Makes a new `UseCandidateEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_encode!(UseCandidateEncoder, UseCandidate, |_item: Self::Item| ());

/// `ICE-CONTROLLED` attribute.
///
/// See [RFC 5245 -- 7.1.2.1 ICE-CONTROLLED] about this attribute.
///
/// [RFC 5245 -- 7.1.2.1 ICE-CONTROLLED]: https://tools.ietf.org/html/rfc5245#section-7.1.2.2
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IceControlled(u64);
impl IceControlled {
    /// The codepoint of the type of the attribute.
    pub const CODEPOINT: u16 = 0x8029;

    /// Makes a new `IceControlled` instance.
    pub fn new(rnd: u64) -> Self {
        IceControlled(rnd)
    }

    /// Returns the alternate address.
    pub fn prio(&self) -> u64 {
        self.0
    }
}
impl Attribute for IceControlled {
    type Decoder = IceControlledDecoder;
    type Encoder = IceControlledEncoder;

    fn get_type(&self) -> AttributeType {
        AttributeType::new(Self::CODEPOINT)
    }
}

/// [`IceControlled`] decoder.
#[derive(Debug, Default)]
pub struct IceControlledDecoder(U64beDecoder);

impl IceControlledDecoder {
    /// Makes a new `IceControlledDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_decode!(IceControlledDecoder, IceControlled, |prio| Ok(
    IceControlled(prio)
));

/// [`IceControlled`] encoder.
#[derive(Debug, Default)]
pub struct IceControlledEncoder(U64beEncoder);

impl IceControlledEncoder {
    /// Makes a new `IceControlledEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_encode!(IceControlledEncoder, IceControlled, |item: Self::Item| item
    .0);

/// `ICE-CONTROLLING` attribute.
///
/// See [RFC 5245 -- 7.1.2.1 ICE-CONTROLLING] about this attribute.
///
/// [RFC 5245 -- 7.1.2.1 ICE-CONTROLLING]: https://tools.ietf.org/html/rfc5245#section-7.1.2.2
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IceControlling(u64);
impl IceControlling {
    /// The codepoint of the type of the attribute.
    pub const CODEPOINT: u16 = 0x802A;

    /// Makes a new `IceControlling` instance.
    pub fn new(rnd: u64) -> Self {
        IceControlling(rnd)
    }

    /// Returns the alternate address.
    pub fn prio(&self) -> u64 {
        self.0
    }
}
impl Attribute for IceControlling {
    type Decoder = IceControllingDecoder;
    type Encoder = IceControllingEncoder;

    fn get_type(&self) -> AttributeType {
        AttributeType::new(Self::CODEPOINT)
    }
}

/// [`IceControlling`] decoder.
#[derive(Debug, Default)]
pub struct IceControllingDecoder(U64beDecoder);

impl IceControllingDecoder {
    /// Makes a new `IceControllingDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_decode!(IceControllingDecoder, IceControlling, |prio| Ok(
    IceControlling(prio)
));

/// [`IceControlling`] encoder.
#[derive(Debug, Default)]
pub struct IceControllingEncoder(U64beEncoder);

impl IceControllingEncoder {
    /// Makes a new `IceControllingEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_encode!(IceControllingEncoder, IceControlling, |item: Self::Item| {
    item.0
});