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
use anyhow::{bail, Result};
use colored::*;
use serde::Serialize;
use std::collections::BTreeMap;

use skyspell_core::{Checker, Dictionary, StorageBackend};
use skyspell_core::{Project, RelativePath};

use crate::{info_1, info_2, OutputFormat};

#[derive(Debug, Serialize)]
struct Range {
    line: usize,
    start_column: usize,
    end_column: usize,
}

#[derive(Debug, Serialize)]
struct Error {
    word: String,
    range: Range,
}

pub struct NonInteractiveChecker<D: Dictionary> {
    project: Project,
    dictionary: D,
    storage_backend: StorageBackend,
    output_format: OutputFormat,
    errors: BTreeMap<String, Vec<Error>>,
    num_errors: usize,
}

impl<D: Dictionary> NonInteractiveChecker<D> {
    pub fn new(
        project: Project,
        dictionary: D,
        storage_backend: StorageBackend,
        output_format: OutputFormat,
    ) -> Result<Self> {
        if output_format.is_text() {
            info_1!(
                "Checking project {} for spelling errors",
                project.path().as_str().bold()
            );
        }
        Ok(Self {
            project,
            dictionary,
            storage_backend,
            output_format,
            errors: BTreeMap::new(),
            num_errors: 0,
        })
    }

    fn print_error(&self, path: &RelativePath, error: &Error) {
        let Error { range, word } = error;
        let Range {
            line,
            start_column,
            end_column,
        } = range;
        let prefix = format!("{}:{}:{}:{}", path, line, start_column, end_column);
        println!(
            "{}: {}: {}: {}",
            prefix,
            "error".red(),
            "unknown word".clear(),
            word
        );
    }

    fn success_text(&self) -> Result<()> {
        match self.num_errors {
            0 => {
                info_2!("Success! No spelling errors found");
                Ok(())
            }
            1 => bail!("Found just one tiny spelling error"),
            n => bail!("Found {} spelling errors", n),
        }
    }

    fn success_json(&self) -> Result<()> {
        let json = serde_json::to_string(&self.errors).expect("errors should be serializable");
        println!("{json}");
        if self.errors.is_empty() {
            Ok(())
        } else {
            bail!("Found some errors");
        }
    }
}

impl<D: Dictionary> Checker for NonInteractiveChecker<D> {
    // line, column
    type Context = (usize, usize);

    fn dictionary(&self) -> &dyn Dictionary {
        &self.dictionary
    }

    fn handle_error(
        &mut self,
        token: &str,
        path: &RelativePath,
        context: &Self::Context,
    ) -> Result<()> {
        self.num_errors += 1;
        let &(line, column) = context;
        let start_column = column + 1;
        let end_column = start_column + token.chars().count() - 1;
        let range = Range {
            line,
            start_column,
            end_column,
        };
        let error = Error {
            word: token.to_string(),
            range,
        };
        if self.output_format == OutputFormat::Text {
            self.print_error(path, &error);
        }
        let entry = self.errors.entry(path.to_string());
        let errors_for_entry = entry.or_default();
        errors_for_entry.push(error);
        Ok(())
    }

    fn success(&self) -> Result<()> {
        match self.output_format {
            OutputFormat::Text => self.success_text(),
            OutputFormat::Json => self.success_json(),
        }
    }

    fn project(&self) -> &Project {
        &self.project
    }

    fn storage_backend(&mut self) -> &mut StorageBackend {
        &mut self.storage_backend
    }
}