zebra_network/protocol/internal/
response_status.rs

1//! The status of a response to an inventory request.
2
3use std::fmt;
4
5#[cfg(any(test, feature = "proptest-impl"))]
6use proptest_derive::Arbitrary;
7
8use InventoryResponse::*;
9
10/// A generic peer inventory response status.
11///
12/// `Available` is used for inventory that is present in the response,
13/// and `Missing` is used for inventory that is missing from the response.
14#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
15#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
16pub enum InventoryResponse<A, M> {
17    /// An available inventory item.
18    Available(A),
19
20    /// A missing inventory item.
21    Missing(M),
22}
23
24impl<A, M> fmt::Display for InventoryResponse<A, M> {
25    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26        f.write_str(self.command())
27    }
28}
29
30impl<A, M> InventoryResponse<A, M> {
31    /// Returns the response status type as a string.
32    pub fn command(&self) -> &'static str {
33        match self {
34            InventoryResponse::Available(_) => "Available",
35            InventoryResponse::Missing(_) => "Missing",
36        }
37    }
38
39    /// Returns true if the inventory item was available.
40    pub fn is_available(&self) -> bool {
41        matches!(self, Available(_))
42    }
43
44    /// Returns true if the inventory item was missing.
45    pub fn is_missing(&self) -> bool {
46        matches!(self, Missing(_))
47    }
48
49    /// Maps a `InventoryResponse<A, M>` to `InventoryResponse<B, M>` by applying a function to a
50    /// contained [`Available`] value, leaving the [`Missing`] value untouched.
51    pub fn map_available<B, F: FnOnce(A) -> B>(self, f: F) -> InventoryResponse<B, M> {
52        // Based on Result::map from https://doc.rust-lang.org/src/core/result.rs.html#765
53        match self {
54            Available(a) => Available(f(a)),
55            Missing(m) => Missing(m),
56        }
57    }
58
59    /// Maps a `InventoryResponse<A, M>` to `InventoryResponse<A, N>` by applying a function to a
60    /// contained [`Missing`] value, leaving the [`Available`] value untouched.
61    pub fn map_missing<N, F: FnOnce(M) -> N>(self, f: F) -> InventoryResponse<A, N> {
62        // Based on Result::map_err from https://doc.rust-lang.org/src/core/result.rs.html#850
63        match self {
64            Available(a) => Available(a),
65            Missing(m) => Missing(f(m)),
66        }
67    }
68
69    /// Converts from `&InventoryResponse<A, M>` to `InventoryResponse<&A, &M>`.
70    pub fn as_ref(&self) -> InventoryResponse<&A, &M> {
71        match self {
72            Available(item) => Available(item),
73            Missing(item) => Missing(item),
74        }
75    }
76}
77
78impl<A: Clone, M: Clone> InventoryResponse<A, M> {
79    /// Get the available inventory item, if present.
80    pub fn available(&self) -> Option<A> {
81        if let Available(item) = self {
82            Some(item.clone())
83        } else {
84            None
85        }
86    }
87
88    /// Get the missing inventory item, if present.
89    pub fn missing(&self) -> Option<M> {
90        if let Missing(item) = self {
91            Some(item.clone())
92        } else {
93            None
94        }
95    }
96}