async_coap/message/msg_type.rs
1// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16/// Enum representing the CoAP message type: `CON`, `NON`, `ACK`, and `RES`.
17#[derive(Debug, Copy, Eq, PartialEq, Clone)]
18pub enum MsgType {
19 /// Variant for confirmable CoAP messages.
20 Con = 0,
21
22 /// Variant for non-confirmable CoAP messages.
23 Non = 1,
24
25 /// Variant for CoAP message acknowledgements.
26 Ack = 2,
27
28 /// Variant for CoAP reset messages.
29 Res = 3,
30}
31
32impl MsgType {
33 /// Creates a new `MsgType` from the given value, panicing if the value is invalid.
34 pub fn from(tt: u8) -> MsgType {
35 MsgType::try_from(tt).expect("Invalid message type")
36 }
37
38 /// Creates a new `MsgType` from the given value, returning `None` if the value is invalid.
39 pub fn try_from(tt: u8) -> Option<MsgType> {
40 match tt {
41 0 => Some(MsgType::Con),
42 1 => Some(MsgType::Non),
43 2 => Some(MsgType::Ack),
44 3 => Some(MsgType::Res),
45 _ => None,
46 }
47 }
48
49 /// Returns true if this message type is nonconfirmable (NON).
50 pub fn is_non(self) -> bool {
51 self == MsgType::Non
52 }
53
54 /// Returns true if this message type is confirmable (CON).
55 pub fn is_con(self) -> bool {
56 self == MsgType::Con
57 }
58
59 /// Returns true if this message type is an acknowledgement (ACK).
60 pub fn is_ack(self) -> bool {
61 self == MsgType::Ack
62 }
63
64 /// Returns true if this message type is a reset (RES).
65 pub fn is_res(self) -> bool {
66 self == MsgType::Res
67 }
68}
69
70impl Default for MsgType {
71 fn default() -> Self {
72 MsgType::Con
73 }
74}