netlink_packet_utils/
errors.rs

1// SPDX-License-Identifier: MIT
2
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6#[non_exhaustive]
7pub enum EncodeError {
8    #[error(transparent)]
9    Other(#[from] Box<dyn std::error::Error>),
10}
11
12impl From<&str> for EncodeError {
13    fn from(msg: &str) -> Self {
14        let error: Box<dyn std::error::Error> = msg.to_string().into();
15        EncodeError::Other(error)
16    }
17}
18
19impl From<String> for EncodeError {
20    fn from(msg: String) -> Self {
21        let error: Box<dyn std::error::Error> = msg.into();
22        EncodeError::Other(error)
23    }
24}
25
26#[derive(Debug, Error)]
27#[non_exhaustive]
28pub enum DecodeError {
29    #[error(
30        "Invalid MAC address. Expected 6 bytes, received {received} bytes"
31    )]
32    InvalidMACAddress { received: usize },
33
34    #[error(
35        "Invalid IP address. Expected 4 or 16 bytes, received {received} bytes"
36    )]
37    InvalidIPAddress { received: usize },
38
39    #[error("Invalid string")]
40    Utf8Error(#[from] std::string::FromUtf8Error),
41
42    #[error(
43        "Invalid number. Expected {expected} bytes, received {received} bytes"
44    )]
45    InvalidNumber { expected: usize, received: usize },
46
47    #[error("Invalid buffer {name}. Expected at least {minimum_length} bytes, received {received} bytes")]
48    InvalidBuffer {
49        name: &'static str,
50        received: usize,
51        minimum_length: usize,
52    },
53
54    #[error(transparent)]
55    Nla(#[from] crate::nla::NlaError),
56
57    #[error(transparent)]
58    Other(#[from] Box<dyn std::error::Error>),
59}
60
61impl From<&str> for DecodeError {
62    fn from(msg: &str) -> Self {
63        let error: Box<dyn std::error::Error> = msg.to_string().into();
64        DecodeError::Other(error)
65    }
66}
67
68impl From<String> for DecodeError {
69    fn from(msg: String) -> Self {
70        let error: Box<dyn std::error::Error> = msg.into();
71        DecodeError::Other(error)
72    }
73}