Skip to main content

veilid_tools/
network_attempt.rs

1use super::*;
2
3use core::fmt::{Debug, Display};
4
5/// Whether a network operation was performed at all.
6///
7/// Sibling to [`NetworkResult`], one layer out: every `NetworkResult` variant
8/// asserts something about the remote, so declining to act needs a variant of its
9/// own. `Result<NetworkAttempt<T>, E>` covers all three cases: `Err` for our own
10/// fault, `Skipped` for a deliberate choice not to act, `Performed` for anything
11/// the remote had a part in.
12pub enum NetworkAttempt<T> {
13    /// The operation happened; the result describes how it went
14    Performed(NetworkResult<T>),
15    /// Deliberately not performed. Carries no information about the remote: it must not
16    /// count against their contact methods, their liveness, or any route or relay built on them.
17    Skipped,
18}
19
20impl<T> NetworkAttempt<T> {
21    /// A performed attempt that succeeded
22    pub fn value(v: T) -> Self {
23        Self::Performed(NetworkResult::Value(v))
24    }
25}
26
27impl<T: Debug> Debug for NetworkAttempt<T> {
28    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
29        match self {
30            Self::Performed(nr) => f.debug_tuple("Performed").field(nr).finish(),
31            Self::Skipped => write!(f, "Skipped"),
32        }
33    }
34}
35
36impl<T> Display for NetworkAttempt<T> {
37    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
38        match self {
39            Self::Performed(nr) => write!(f, "Performed({})", nr),
40            Self::Skipped => write!(f, "Skipped"),
41        }
42    }
43}