zebra_network/protocol/internal/
response_status.rs
1use std::fmt;
4
5#[cfg(any(test, feature = "proptest-impl"))]
6use proptest_derive::Arbitrary;
7
8use InventoryResponse::*;
9
10#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
15#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
16pub enum InventoryResponse<A, M> {
17 Available(A),
19
20 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 pub fn command(&self) -> &'static str {
33 match self {
34 InventoryResponse::Available(_) => "Available",
35 InventoryResponse::Missing(_) => "Missing",
36 }
37 }
38
39 pub fn is_available(&self) -> bool {
41 matches!(self, Available(_))
42 }
43
44 pub fn is_missing(&self) -> bool {
46 matches!(self, Missing(_))
47 }
48
49 pub fn map_available<B, F: FnOnce(A) -> B>(self, f: F) -> InventoryResponse<B, M> {
52 match self {
54 Available(a) => Available(f(a)),
55 Missing(m) => Missing(m),
56 }
57 }
58
59 pub fn map_missing<N, F: FnOnce(M) -> N>(self, f: F) -> InventoryResponse<A, N> {
62 match self {
64 Available(a) => Available(a),
65 Missing(m) => Missing(f(m)),
66 }
67 }
68
69 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 pub fn available(&self) -> Option<A> {
81 if let Available(item) = self {
82 Some(item.clone())
83 } else {
84 None
85 }
86 }
87
88 pub fn missing(&self) -> Option<M> {
90 if let Missing(item) = self {
91 Some(item.clone())
92 } else {
93 None
94 }
95 }
96}