use alloc::string::String;
use alloc::vec::Vec;
#[cfg(feature = "schemars")]
use schemars::JsonSchema;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use crate::ErrorCode;
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(serialize = "T: Serialize", deserialize = "T: DeserializeOwned"))
)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "schemars", schemars(bound = "T: JsonSchema"))]
pub enum Outcome<T> {
Done(T),
Pending {
reason: String,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
expected_input: Option<Vec<String>>,
},
Error {
code: ErrorCode,
message: String,
},
}
impl<T> Outcome<T> {
pub fn is_done(&self) -> bool {
matches!(self, Self::Done(_))
}
pub fn is_pending(&self) -> bool {
matches!(self, Self::Pending { .. })
}
pub fn is_error(&self) -> bool {
matches!(self, Self::Error { .. })
}
pub fn map<U, F>(self, mut f: F) -> Outcome<U>
where
F: FnMut(T) -> U,
{
match self {
Outcome::Done(inner) => Outcome::Done(f(inner)),
Outcome::Pending {
reason,
expected_input,
} => Outcome::Pending {
reason,
expected_input,
},
Outcome::Error { code, message } => Outcome::Error { code, message },
}
}
}