edge_dhcp/
io.rs

1use core::fmt::{self, Debug};
2use core::net::{SocketAddr, SocketAddrV4};
3
4use crate as dhcp;
5
6pub mod client;
7pub mod server;
8
9pub const DEFAULT_SERVER_PORT: u16 = 67;
10pub const DEFAULT_CLIENT_PORT: u16 = 68;
11
12#[derive(Copy, Clone, Debug, Eq, PartialEq)]
13pub enum Error<E> {
14    Io(E),
15    Format(dhcp::Error),
16}
17
18pub type ErrorKind = Error<edge_nal::io::ErrorKind>;
19
20impl<E> Error<E>
21where
22    E: edge_nal::io::Error,
23{
24    pub fn erase(&self) -> Error<edge_nal::io::ErrorKind> {
25        match self {
26            Self::Io(e) => Error::Io(e.kind()),
27            Self::Format(e) => Error::Format(*e),
28        }
29    }
30}
31
32impl<E> From<dhcp::Error> for Error<E> {
33    fn from(value: dhcp::Error) -> Self {
34        Self::Format(value)
35    }
36}
37
38impl<E> fmt::Display for Error<E>
39where
40    E: fmt::Display,
41{
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match self {
44            Self::Io(err) => write!(f, "IO error: {err}"),
45            Self::Format(err) => write!(f, "Format error: {err}"),
46        }
47    }
48}
49
50#[cfg(feature = "defmt")]
51impl<E> defmt::Format for Error<E>
52where
53    E: defmt::Format,
54{
55    fn format(&self, f: defmt::Formatter<'_>) {
56        match self {
57            Self::Io(err) => defmt::write!(f, "IO error: {}", err),
58            Self::Format(err) => defmt::write!(f, "Format error: {}", err),
59        }
60    }
61}
62
63impl<E> core::error::Error for Error<E> where E: core::error::Error {}