1 //! This module contains structures for filtering the expected types.
2 //! Use case for structures in this module is, for example, situation when you need to process
3 //! only certain `Enum`s.
8 use syntax::ast::{self, make, Pat};
10 use crate::RootDatabase;
12 /// Enum types that implement `std::ops::Try` trait.
13 #[derive(Clone, Copy)]
20 const ALL: [TryEnum; 2] = [TryEnum::Option, TryEnum::Result];
22 /// Returns `Some(..)` if the provided type is an enum that implements `std::ops::Try`.
23 pub fn from_ty(sema: &Semantics<'_, RootDatabase>, ty: &hir::Type) -> Option<TryEnum> {
24 let enum_ = match ty.as_adt() {
25 Some(hir::Adt::Enum(it)) => it,
28 TryEnum::ALL.iter().find_map(|&var| {
29 if enum_.name(sema.db).to_smol_str() == var.type_name() {
36 pub fn happy_case(self) -> &'static str {
38 TryEnum::Result => "Ok",
39 TryEnum::Option => "Some",
43 pub fn sad_pattern(self) -> ast::Pat {
45 TryEnum::Result => make::tuple_struct_pat(
46 make::ext::ident_path("Err"),
47 iter::once(make::wildcard_pat().into()),
50 TryEnum::Option => make::ext::simple_ident_pat(make::name("None")).into(),
54 pub fn happy_pattern(self, pat: Pat) -> ast::Pat {
57 make::tuple_struct_pat(make::ext::ident_path("Ok"), iter::once(pat)).into()
60 make::tuple_struct_pat(make::ext::ident_path("Some"), iter::once(pat)).into()
65 pub fn happy_pattern_wildcard(self) -> ast::Pat {
67 TryEnum::Result => make::tuple_struct_pat(
68 make::ext::ident_path("Ok"),
69 iter::once(make::wildcard_pat().into()),
72 TryEnum::Option => make::tuple_struct_pat(
73 make::ext::ident_path("Some"),
74 iter::once(make::wildcard_pat().into()),
80 fn type_name(self) -> &'static str {
82 TryEnum::Result => "Result",
83 TryEnum::Option => "Option",