1use std::borrow::Cow;
2
3pub trait Dictionary: Send + Sync {
5 fn correct_ident<'s>(&'s self, ident: crate::tokens::Identifier<'_>) -> Option<Status<'s>>;
9
10 fn correct_word<'s>(&'s self, word: crate::tokens::Word<'_>) -> Option<Status<'s>>;
14}
15
16#[derive(Clone, PartialEq, Eq, Debug, serde::Serialize)]
18#[serde(rename_all = "snake_case")]
19#[serde(untagged)]
20pub enum Status<'c> {
21 Valid,
22 Invalid,
23 Corrections(Vec<Cow<'c, str>>),
24}
25
26impl<'c> Status<'c> {
27 #[inline]
28 pub fn is_invalid(&self) -> bool {
29 matches!(self, Status::Invalid)
30 }
31 #[inline]
32 pub fn is_valid(&self) -> bool {
33 matches!(self, Status::Valid)
34 }
35 #[inline]
36 pub fn is_correction(&self) -> bool {
37 matches!(self, Status::Corrections(_))
38 }
39
40 #[inline]
41 pub fn corrections_mut(&mut self) -> impl Iterator<Item = &mut Cow<'c, str>> {
42 match self {
43 Status::Corrections(corrections) => itertools::Either::Left(corrections.iter_mut()),
44 _ => itertools::Either::Right([].iter_mut()),
45 }
46 }
47
48 pub fn into_owned(self) -> Status<'static> {
49 match self {
50 Status::Valid => Status::Valid,
51 Status::Invalid => Status::Invalid,
52 Status::Corrections(corrections) => {
53 let corrections = corrections
54 .into_iter()
55 .map(|c| Cow::Owned(c.into_owned()))
56 .collect();
57 Status::Corrections(corrections)
58 }
59 }
60 }
61
62 pub fn borrow(&self) -> Status<'_> {
63 match self {
64 Status::Corrections(corrections) => {
65 let corrections = corrections
66 .iter()
67 .map(|c| Cow::Borrowed(c.as_ref()))
68 .collect();
69 Status::Corrections(corrections)
70 }
71 _ => self.clone(),
72 }
73 }
74}