use std::fmt;
#[cfg(any(test, feature = "proptest-impl"))]
use proptest_derive::Arbitrary;
use InventoryResponse::*;
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
pub enum InventoryResponse<A, M> {
Available(A),
Missing(M),
}
impl<A, M> fmt::Display for InventoryResponse<A, M> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.command())
}
}
impl<A, M> InventoryResponse<A, M> {
pub fn command(&self) -> &'static str {
match self {
InventoryResponse::Available(_) => "Available",
InventoryResponse::Missing(_) => "Missing",
}
}
pub fn is_available(&self) -> bool {
matches!(self, Available(_))
}
pub fn is_missing(&self) -> bool {
matches!(self, Missing(_))
}
pub fn map_available<B, F: FnOnce(A) -> B>(self, f: F) -> InventoryResponse<B, M> {
match self {
Available(a) => Available(f(a)),
Missing(m) => Missing(m),
}
}
pub fn map_missing<N, F: FnOnce(M) -> N>(self, f: F) -> InventoryResponse<A, N> {
match self {
Available(a) => Available(a),
Missing(m) => Missing(f(m)),
}
}
pub fn as_ref(&self) -> InventoryResponse<&A, &M> {
match self {
Available(item) => Available(item),
Missing(item) => Missing(item),
}
}
}
impl<A: Clone, M: Clone> InventoryResponse<A, M> {
pub fn available(&self) -> Option<A> {
if let Available(item) = self {
Some(item.clone())
} else {
None
}
}
pub fn missing(&self) -> Option<M> {
if let Missing(item) = self {
Some(item.clone())
} else {
None
}
}
}