use ra_ap_syntax::SyntaxElement;
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub enum MetaOption<T: ToString> {
Ok(T),
Err(Vec<SyntaxElement>),
#[default]
None,
}
impl<T: ToString> 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, &Vec<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<&Vec<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>, &Vec<SyntaxElement>> {
match self {
Self::Ok(value) => Ok(Some(value)),
Self::Err(value) => Err(value),
Self::None => Ok(None),
}
}
}
impl<T: ToString> ToString for MetaOption<T> {
fn to_string(&self) -> String {
match self {
Self::Ok(value) => value.to_string(),
Self::Err(value) => value
.iter()
.map(|elem| elem.to_string())
.collect::<Vec<String>>()
.join(""),
Self::None => String::new(),
}
}
}