|
51 | 51 | // Length of parameter in bytes |
52 | 52 | type LengthAsBytes: IntoIterator<Item = u8>; |
53 | 53 |
|
54 | | - fn new(data: &str) -> Self; |
| 54 | + fn new(data: &str) -> Result<Self, Error>; |
55 | 55 |
|
56 | 56 | fn from_bytes(bytes: &[u8]) -> Result<Self, Error>; |
57 | 57 |
|
@@ -171,17 +171,21 @@ impl NinaConcreteParam for NinaByteParam { |
171 | 171 | type DataBuffer = Vec<u8, MAX_NINA_BYTE_PARAM_BUFFER_LENGTH>; |
172 | 172 | type LengthAsBytes = [u8; 1]; |
173 | 173 |
|
174 | | - fn new(data: &str) -> Self { |
| 174 | + fn new(data: &str) -> Result<Self, Error> { |
| 175 | + if data.len() > MAX_NINA_BYTE_PARAM_BUFFER_LENGTH { |
| 176 | + return Err(ProtocolError::PayloadTooLarge.into()); |
| 177 | + } |
| 178 | + |
175 | 179 | let data_as_bytes: Self::DataBuffer = String::from(data).into_bytes(); |
176 | | - Self { |
| 180 | + Ok(Self { |
177 | 181 | length: data_as_bytes.len() as u8, |
178 | 182 | data: data_as_bytes, |
179 | | - } |
| 183 | + }) |
180 | 184 | } |
181 | 185 |
|
182 | 186 | fn from_bytes(bytes: &[u8]) -> Result<Self, Error> { |
183 | 187 | if bytes.len() > MAX_NINA_BYTE_PARAM_BUFFER_LENGTH { |
184 | | - return Err(ProtocolError::TooManyParameters.into()); |
| 188 | + return Err(ProtocolError::PayloadTooLarge.into()); |
185 | 189 | } |
186 | 190 |
|
187 | 191 | let mut data_as_bytes: Self::DataBuffer = Vec::new(); |
@@ -218,17 +222,21 @@ impl NinaConcreteParam for NinaWordParam { |
218 | 222 | type DataBuffer = Vec<u8, MAX_NINA_WORD_PARAM_BUFFER_LENGTH>; |
219 | 223 | type LengthAsBytes = [u8; 1]; |
220 | 224 |
|
221 | | - fn new(data: &str) -> Self { |
| 225 | + fn new(data: &str) -> Result<Self, Error> { |
| 226 | + if data.len() > MAX_NINA_WORD_PARAM_BUFFER_LENGTH { |
| 227 | + return Err(ProtocolError::PayloadTooLarge.into()); |
| 228 | + } |
| 229 | + |
222 | 230 | let data_as_bytes: Self::DataBuffer = String::from(data).into_bytes(); |
223 | | - Self { |
| 231 | + Ok(Self { |
224 | 232 | length: data_as_bytes.len() as u8, |
225 | 233 | data: data_as_bytes, |
226 | | - } |
| 234 | + }) |
227 | 235 | } |
228 | 236 |
|
229 | 237 | fn from_bytes(bytes: &[u8]) -> Result<Self, Error> { |
230 | 238 | if bytes.len() > MAX_NINA_WORD_PARAM_BUFFER_LENGTH { |
231 | | - return Err(ProtocolError::TooManyParameters.into()); |
| 239 | + return Err(ProtocolError::PayloadTooLarge.into()); |
232 | 240 | } |
233 | 241 |
|
234 | 242 | let mut data_as_bytes: Self::DataBuffer = Vec::new(); |
@@ -265,17 +273,21 @@ impl NinaConcreteParam for NinaSmallArrayParam { |
265 | 273 | type DataBuffer = Vec<u8, MAX_NINA_SMALL_ARRAY_PARAM_BUFFER_LENGTH>; |
266 | 274 | type LengthAsBytes = [u8; 1]; |
267 | 275 |
|
268 | | - fn new(data: &str) -> Self { |
| 276 | + fn new(data: &str) -> Result<Self, Error> { |
| 277 | + if data.len() > MAX_NINA_SMALL_ARRAY_PARAM_BUFFER_LENGTH { |
| 278 | + return Err(ProtocolError::PayloadTooLarge.into()); |
| 279 | + } |
| 280 | + |
269 | 281 | let data_as_bytes: Self::DataBuffer = String::from(data).into_bytes(); |
270 | | - Self { |
| 282 | + Ok(Self { |
271 | 283 | length: data_as_bytes.len() as u8, |
272 | 284 | data: data_as_bytes, |
273 | | - } |
| 285 | + }) |
274 | 286 | } |
275 | 287 |
|
276 | 288 | fn from_bytes(bytes: &[u8]) -> Result<Self, Error> { |
277 | 289 | if bytes.len() > MAX_NINA_SMALL_ARRAY_PARAM_BUFFER_LENGTH { |
278 | | - return Err(ProtocolError::TooManyParameters.into()); |
| 290 | + return Err(ProtocolError::PayloadTooLarge.into()); |
279 | 291 | } |
280 | 292 |
|
281 | 293 | let mut data_as_bytes: Self::DataBuffer = Vec::new(); |
@@ -312,17 +324,21 @@ impl NinaConcreteParam for NinaLargeArrayParam { |
312 | 324 | type DataBuffer = Vec<u8, MAX_NINA_LARGE_ARRAY_PARAM_BUFFER_LENGTH>; |
313 | 325 | type LengthAsBytes = [u8; 2]; |
314 | 326 |
|
315 | | - fn new(data: &str) -> Self { |
| 327 | + fn new(data: &str) -> Result<Self, Error> { |
| 328 | + if data.len() > MAX_NINA_LARGE_ARRAY_PARAM_BUFFER_LENGTH { |
| 329 | + return Err(ProtocolError::PayloadTooLarge.into()); |
| 330 | + } |
| 331 | + |
316 | 332 | let data_as_bytes: Self::DataBuffer = String::from(data).into_bytes(); |
317 | | - Self { |
| 333 | + Ok(Self { |
318 | 334 | length: data_as_bytes.len() as u16, |
319 | 335 | data: data_as_bytes, |
320 | | - } |
| 336 | + }) |
321 | 337 | } |
322 | 338 |
|
323 | 339 | fn from_bytes(bytes: &[u8]) -> Result<Self, Error> { |
324 | 340 | if bytes.len() > MAX_NINA_LARGE_ARRAY_PARAM_BUFFER_LENGTH { |
325 | | - return Err(ProtocolError::TooManyParameters.into()); |
| 341 | + return Err(ProtocolError::PayloadTooLarge.into()); |
326 | 342 | } |
327 | 343 |
|
328 | 344 | let mut data_as_bytes: Self::DataBuffer = Vec::new(); |
|
0 commit comments