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
#[cfg(feature = "serde")]
use serde_json::Value;
use crate::protocol::errors::Error;
use super::errors::ErrorKind;
///
/// This enum represents the different types of arguments that can be passed over the SHDP protocol.
///
pub enum Arg {
/// A text argument.
Text(String),
/// An unsigned 8-bit integer argument.
U8(u8),
/// An unsigned 16-bit integer argument.
U16(u16),
/// An unsigned 32-bit integer argument.
U32(u32),
/// A boolean argument.
Boolean(bool),
/// A vector of text arguments.
VecText(Vec<String>),
/// An optional text argument.
OptionText(Option<String>),
///
/// An optional value argument.
///
/// This is only available when the `serde` feature is enabled.
///
#[cfg(feature = "serde")]
OptionValue(Option<Value>),
}
impl Arg {
///
/// Creates an argument from a string.
///
/// The string if converted to the appropriate type, first. Then the argument is created.
///
pub fn from_string(value: &str) -> Self {
if value.starts_with("0x") {
if let Ok(value) = u32::from_str_radix(&value[2..], 16) {
return Arg::U32(value);
}
}
if let Ok(value) = value.parse::<u8>() {
return Arg::U8(value);
}
if let Ok(value) = value.parse::<u16>() {
return Arg::U16(value);
}
if let Ok(value) = value.parse::<u32>() {
return Arg::U32(value);
}
if value == "true" {
return Arg::Boolean(true);
}
if value == "false" {
return Arg::Boolean(false);
}
Arg::Text(value.to_string())
}
///
/// Converts the argument to a string.
///
pub fn to_string(&self) -> String {
match self {
Arg::Text(value) => value.to_string(),
Arg::U8(value) => value.to_string(),
Arg::U16(value) => value.to_string(),
Arg::U32(value) => value.to_string(),
Arg::Boolean(value) => value.to_string(),
Arg::VecText(value) => value.join(", "),
Arg::OptionText(value) => match value {
Some(value) => value.to_string(),
None => "".to_string(),
},
#[cfg(feature = "serde")]
Arg::OptionValue(value) => match value {
Some(value) => value.to_string(),
None => "".to_string(),
},
}
}
///
/// Converts the argument to an unsigned 8-bit integer.
///
pub fn to_u8(&self) -> Result<u8, Error> {
match self {
Arg::U8(value) => Ok(*value),
_ => Err(Error {
code: 500,
message: "Expected u8".to_string(),
kind: ErrorKind::InternalServerError,
}),
}
}
///
/// Converts the argument to an unsigned 16-bit integer.
///
pub fn to_u16(&self) -> Result<u16, Error> {
match self {
Arg::U16(value) => Ok(*value),
_ => Err(Error {
code: 500,
message: "Expected u16".to_string(),
kind: ErrorKind::InternalServerError,
}),
}
}
///
/// Converts the argument to an unsigned 32-bit integer.
///
pub fn to_u32(&self) -> Result<u32, Error> {
match self {
Arg::U32(value) => Ok(*value),
_ => Err(Error {
code: 500,
message: "Expected u32".to_string(),
kind: ErrorKind::InternalServerError,
}),
}
}
///
/// Converts the argument to a boolean.
///
pub fn to_bool(&self) -> Result<bool, Error> {
match self {
Arg::Boolean(value) => Ok(*value),
_ => Err(Error {
code: 500,
message: "Expected boolean".to_string(),
kind: ErrorKind::InternalServerError,
}),
}
}
///
/// Converts the argument to a vector of text.
///
pub fn to_vec_text(&self) -> Result<Vec<String>, Error> {
match self {
Arg::VecText(value) => Ok(value.clone()),
_ => Err(Error {
code: 500,
message: "Expected Vec<String>".to_string(),
kind: ErrorKind::InternalServerError,
}),
}
}
///
/// Converts the argument to an optional text.
///
pub fn to_option_text(&self) -> Result<Option<String>, Error> {
match self {
Arg::OptionText(value) => Ok(value.clone()),
_ => Err(Error {
code: 500,
message: "Expected Option<String>".to_string(),
kind: ErrorKind::InternalServerError,
}),
}
}
///
/// Converts the argument to an optional value.
///
/// This is only available when the `serde` feature is enabled.
///
#[cfg(feature = "serde")]
pub fn to_option_value(&self) -> Result<Option<Value>, Error> {
match self {
Arg::OptionValue(value) => Ok(value.clone()),
_ => Err(Error {
code: 500,
message: "Expected Option<Value>".to_string(),
kind: ErrorKind::InternalServerError,
}),
}
}
}