1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use std::fs::File;
use std::io::Read;

use bstr::ByteSlice;

use crate::report;
use crate::tokens;
use crate::Dictionary;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CheckSettings {
    check_filenames: bool,
    check_files: bool,
    binary: bool,
}

impl CheckSettings {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn check_filenames(&mut self, yes: bool) -> &mut Self {
        self.check_filenames = yes;
        self
    }

    pub fn check_files(&mut self, yes: bool) -> &mut Self {
        self.check_files = yes;
        self
    }

    pub fn binary(&mut self, yes: bool) -> &mut Self {
        self.binary = yes;
        self
    }

    pub fn build<'d, 'p>(
        &self,
        dictionary: &'d dyn Dictionary,
        parser: &'p tokens::Parser,
    ) -> Checks<'d, 'p> {
        Checks {
            dictionary,
            parser,
            check_filenames: self.check_filenames,
            check_files: self.check_files,
            binary: self.binary,
        }
    }
}

impl Default for CheckSettings {
    fn default() -> Self {
        Self {
            check_filenames: true,
            check_files: true,
            binary: false,
        }
    }
}

#[derive(Clone)]
pub struct Checks<'d, 'p> {
    dictionary: &'d dyn Dictionary,
    parser: &'p tokens::Parser,
    check_filenames: bool,
    check_files: bool,
    binary: bool,
}

impl<'d, 'p> Checks<'d, 'p> {
    pub fn check_filename(
        &self,
        path: &std::path::Path,
        report: report::Report,
    ) -> Result<bool, failure::Error> {
        let mut typos_found = false;

        if !self.check_filenames {
            return Ok(typos_found);
        }

        for part in path.components().filter_map(|c| c.as_os_str().to_str()) {
            for ident in self.parser.parse(part) {
                if let Some(correction) = self.dictionary.correct_ident(ident) {
                    let msg = report::FilenameCorrection {
                        path,
                        typo: ident.token(),
                        correction,
                        non_exhaustive: (),
                    };
                    report(msg.into());
                    typos_found = true;
                }
                for word in ident.split() {
                    if let Some(correction) = self.dictionary.correct_word(word) {
                        let msg = report::FilenameCorrection {
                            path,
                            typo: word.token(),
                            correction,
                            non_exhaustive: (),
                        };
                        report(msg.into());
                        typos_found = true;
                    }
                }
            }
        }

        Ok(typos_found)
    }

    pub fn check_file(
        &self,
        path: &std::path::Path,
        explicit: bool,
        report: report::Report,
    ) -> Result<bool, failure::Error> {
        let mut typos_found = false;

        if !self.check_files {
            return Ok(typos_found);
        }

        let mut buffer = Vec::new();
        File::open(path)?.read_to_end(&mut buffer)?;
        if !explicit && !self.binary && buffer.find_byte(b'\0').is_some() {
            let msg = report::BinaryFile {
                path,
                non_exhaustive: (),
            };
            report(msg.into());
            return Ok(typos_found);
        }

        for (line_idx, line) in buffer.lines().enumerate() {
            let line_num = line_idx + 1;
            for ident in self.parser.parse_bytes(line) {
                if let Some(correction) = self.dictionary.correct_ident(ident) {
                    let col_num = ident.offset();
                    let msg = report::Correction {
                        path,
                        line,
                        line_num,
                        col_num,
                        typo: ident.token(),
                        correction,
                        non_exhaustive: (),
                    };
                    typos_found = true;
                    report(msg.into());
                }
                for word in ident.split() {
                    if let Some(correction) = self.dictionary.correct_word(word) {
                        let col_num = word.offset();
                        let msg = report::Correction {
                            path,
                            line,
                            line_num,
                            col_num,
                            typo: word.token(),
                            correction,
                            non_exhaustive: (),
                        };
                        typos_found = true;
                        report(msg.into());
                    }
                }
            }
        }

        Ok(typos_found)
    }
}

impl std::fmt::Debug for Checks<'_, '_> {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        fmt.debug_struct("Checks")
            .field("parser", self.parser)
            .field("check_filenames", &self.check_filenames)
            .field("check_files", &self.check_files)
            .field("binary", &self.binary)
            .finish()
    }
}