use ra_ap_syntax::SyntaxElement;
use std::fmt;
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub enum MetaOption<T: fmt::Display> {
Ok(T),
Err(Vec<SyntaxElement>),
#[default]
None,
}
impl<T: fmt::Display> MetaOption<T> {
pub fn is_ok(&self) -> bool {
matches!(self, Self::Ok(_))
}
pub fn is_err(&self) -> bool {
matches!(self, Self::Err(_))
}
pub fn is_none(&self) -> bool {
matches!(self, Self::None)
}
pub fn is_some(&self) -> bool {
!self.is_none()
}
pub fn option(&self) -> Option<Result<&T, &[SyntaxElement]>> {
match self {
Self::Ok(value) => Some(Ok(value)),
Self::Err(value) => Some(Err(value)),
Self::None => None,
}
}
pub fn result(&self) -> Result<&T, Option<&[SyntaxElement]>> {
match self {
Self::Ok(value) => Ok(value),
Self::Err(value) => Err(Some(value)),
Self::None => Err(None),
}
}
pub fn result_option(&self) -> Result<Option<&T>, &[SyntaxElement]> {
match self {
Self::Ok(value) => Ok(Some(value)),
Self::Err(value) => Err(value),
Self::None => Ok(None),
}
}
}
impl<T: fmt::Display> fmt::Display for MetaOption<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Ok(value) => value.fmt(f),
Self::Err(value) => write!(
f,
"{}",
value.iter().map(ToString::to_string).collect::<String>()
),
Self::None => write!(f, ""),
}
}
}