typos_git_commit/
typosjsonline.rs

1use crate::cli::Cli;
2use serde::Deserialize;
3
4// A typical typo line in JSON format from typos output program:
5// {"type":"typo","path":"doc/man/man5/burst_buffer.conf.5","line_num":65,"byte_offset":20,"typo":"preceeded","corrections":["preceded","proceeded"]}
6
7#[derive(Deserialize, Debug, Clone)]
8pub struct TyposJsonLine {
9    #[serde(default, rename = "type")]
10    pub type_id: String,
11
12    #[serde(default)]
13    pub path: String,
14
15    #[serde(default)]
16    pub line_num: u32,
17
18    #[serde(default)]
19    pub byte_offset: u32,
20
21    #[serde(default)]
22    pub typo: String,
23
24    #[serde(default)]
25    pub corrections: Vec<String>,
26}
27
28impl TyposJsonLine {
29    // Tells whether the file has been excluded from typos corrections
30    pub fn is_file_excluded(&self, cli: &Cli) -> bool {
31        match &cli.exclude_file {
32            Some(list) => {
33                for file in list {
34                    if self.path.contains(file) {
35                        return true;
36                    }
37                }
38                false
39            }
40            None => false,
41        }
42    }
43
44    // Tells whether typo contains one excluded typo
45    pub fn is_typo_excluded(&self, cli: &Cli) -> bool {
46        match &cli.exclude_typo {
47            Some(list) => {
48                for t in list {
49                    if self.typo.contains(t) {
50                        return true;
51                    }
52                }
53                false
54            }
55            None => false,
56        }
57    }
58
59    // Tells whether typo contains one excluded correction
60    pub fn is_correction_excluded(&self, cli: &Cli) -> bool {
61        match &cli.exclude_correction {
62            Some(list) => {
63                for t in list {
64                    for correction in &self.corrections {
65                        if correction.contains(t) {
66                            return true;
67                        }
68                    }
69                }
70                false
71            }
72            None => false,
73        }
74    }
75
76    // A TypoJsonLine is excluded if at least one of typo, correction or file is excluded
77    // from being corrected
78    pub fn is_excluded(&self, cli: &Cli) -> bool {
79        self.is_typo_excluded(cli) || self.is_correction_excluded(cli) || self.is_file_excluded(cli)
80    }
81}