Skip to main content

dope_core/io/socket/install/
error.rs

1use std::num;
2
3use dope_sys::{
4    Cloexec, Errno, Nonblock,
5    io::socket::{Setup, SetupOrigin, addr},
6};
7
8use crate::{
9    driver::result,
10    io::{
11        socket,
12        socket::{
13            adoption,
14            install::{Operation, adopt, receive, slot},
15        },
16    },
17};
18
19/// Exact bootstrap failure with typed validation and numeric OS causes.
20#[derive(PartialEq, Eq, Debug)]
21pub enum Error {
22    Slot(slot::Error),
23    Receive(receive::Error),
24    Adopt(adopt::Error),
25    FastOpen(num::NonZero<i32>),
26    Address(socket::Error),
27    LocalAddress(addr::Local<Errno>),
28    DescriptorFlags(crate::Errno),
29    CloseOnExec(crate::Errno),
30    StatusFlags(crate::Errno),
31    Nonblocking(crate::Errno),
32    Sigpipe(crate::Errno),
33    System {
34        operation: Operation,
35        fault: crate::Errno,
36    },
37    Kernel {
38        operation: Operation,
39        cause: result::Kernel,
40    },
41}
42
43impl Error {
44    pub(crate) fn reuse((reuse, errno): (socket::Reuse, dope_sys::Errno)) -> Self {
45        Self::syscall(socket::reuse_step(reuse), errno)
46    }
47
48    pub(crate) fn setup(error: Setup) -> Self {
49        match error.origin() {
50            SetupOrigin::Cloexec(Cloexec::Get(error)) => Self::DescriptorFlags(error),
51            SetupOrigin::Cloexec(Cloexec::Set(error)) => Self::CloseOnExec(error),
52            SetupOrigin::Nonblock(Nonblock::Get(error)) => Self::StatusFlags(error),
53            SetupOrigin::Nonblock(Nonblock::Set(error)) => Self::Nonblocking(error),
54            SetupOrigin::Sigpipe(error) => Self::Sigpipe(error),
55        }
56    }
57
58    pub(crate) const fn syscall(op: Operation, fault: dope_sys::Errno) -> Self {
59        Self::System {
60            operation: op,
61            fault,
62        }
63    }
64
65    pub(crate) const fn kernel(op: Operation, cause: result::Kernel) -> Self {
66        Self::Kernel {
67            operation: op,
68            cause,
69        }
70    }
71
72    pub(crate) fn validation(error: adoption::Error) -> Self {
73        match error {
74            adoption::Error::Syscall(errno) => Self::syscall(Operation::Validate, errno),
75            adoption::Error::Role(error) => Self::Adopt(error),
76        }
77    }
78}