use std::iter;
use hir::Semantics;
use syntax::ast::{self, make, Pat};
use crate::RootDatabase;
#[derive(Clone, Copy)]
pub enum TryEnum {
Result,
Option,
}
impl TryEnum {
const ALL: [TryEnum; 2] = [TryEnum::Option, TryEnum::Result];
pub fn from_ty(sema: &Semantics<'_, RootDatabase>, ty: &hir::Type) -> Option<TryEnum> {
let enum_ = match ty.as_adt() {
Some(hir::Adt::Enum(it)) => it,
_ => return None,
};
TryEnum::ALL.iter().find_map(|&var| {
if enum_.name(sema.db).eq_ident(var.type_name()) {
return Some(var);
}
None
})
}
pub fn happy_case(self) -> &'static str {
match self {
TryEnum::Result => "Ok",
TryEnum::Option => "Some",
}
}
pub fn sad_pattern(self) -> ast::Pat {
match self {
TryEnum::Result => make::tuple_struct_pat(
make::ext::ident_path("Err"),
iter::once(make::wildcard_pat().into()),
)
.into(),
TryEnum::Option => make::ext::simple_ident_pat(make::name("None")).into(),
}
}
pub fn happy_pattern(self, pat: Pat) -> ast::Pat {
match self {
TryEnum::Result => {
make::tuple_struct_pat(make::ext::ident_path("Ok"), iter::once(pat)).into()
}
TryEnum::Option => {
make::tuple_struct_pat(make::ext::ident_path("Some"), iter::once(pat)).into()
}
}
}
pub fn happy_pattern_wildcard(self) -> ast::Pat {
match self {
TryEnum::Result => make::tuple_struct_pat(
make::ext::ident_path("Ok"),
iter::once(make::wildcard_pat().into()),
)
.into(),
TryEnum::Option => make::tuple_struct_pat(
make::ext::ident_path("Some"),
iter::once(make::wildcard_pat().into()),
)
.into(),
}
}
fn type_name(self) -> &'static str {
match self {
TryEnum::Result => "Result",
TryEnum::Option => "Option",
}
}
}