|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 | // https://github.com/imageworks/spk
|
4 | 4 |
|
| 5 | +use std::sync::Arc; |
| 6 | + |
5 | 7 | use ngrammatic::CorpusBuilder;
|
6 |
| -use serde::Serialize; |
7 | 8 |
|
8 |
| -#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Ord, PartialOrd, Serialize)] |
| 9 | +#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)] |
9 | 10 | pub struct UnknownKey {
|
10 |
| - pub key: String, |
11 |
| - pub message: String, |
| 11 | + unknown_key: String, |
| 12 | + struct_fields: Vec<Arc<str>>, |
12 | 13 | }
|
13 | 14 |
|
14 | 15 | impl UnknownKey {
|
15 | 16 | pub fn new(unknown_key: &str, struct_fields: Vec<&str>) -> Self {
|
16 |
| - let mut message = format!("Unrecognized key: {unknown_key}. "); |
| 17 | + Self { |
| 18 | + unknown_key: unknown_key.to_string(), |
| 19 | + struct_fields: struct_fields.iter().map(|v| Arc::from(*v)).collect(), |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + pub fn generate_message(&self) -> String { |
| 24 | + let mut message = format!("Unrecognized key: {}. ", self.unknown_key); |
17 | 25 | let mut corpus = CorpusBuilder::new().finish();
|
18 | 26 |
|
19 |
| - for field in struct_fields.iter() { |
| 27 | + for field in self.struct_fields.iter() { |
20 | 28 | corpus.add_text(field);
|
21 | 29 | }
|
22 | 30 |
|
23 |
| - match corpus.search(unknown_key, 0.6).first() { |
| 31 | + match corpus.search(&self.unknown_key, 0.6).first() { |
24 | 32 | Some(s) => message.push_str(format!("(Did you mean: '{}'?)", s.text).as_str()),
|
25 |
| - None => { |
26 |
| - message.push_str(format!("(No similar keys found for: {}.)", unknown_key).as_str()) |
27 |
| - } |
| 33 | + None => message |
| 34 | + .push_str(format!("(No similar keys found for: {}.)", self.unknown_key).as_str()), |
28 | 35 | };
|
29 | 36 |
|
30 |
| - Self { |
31 |
| - key: std::mem::take(&mut unknown_key.to_string()), |
32 |
| - message: message.to_string(), |
33 |
| - } |
| 37 | + message.to_string() |
34 | 38 | }
|
35 | 39 | }
|
36 | 40 |
|
37 |
| -#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Ord, PartialOrd, Serialize)] |
| 41 | +#[derive(Debug, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)] |
| 42 | +pub enum Lint { |
| 43 | + Key(UnknownKey), |
| 44 | +} |
| 45 | + |
| 46 | +#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)] |
38 | 47 | pub struct LintedItem<T> {
|
39 | 48 | pub item: T,
|
40 |
| - pub lints: Vec<String>, |
| 49 | + pub lints: Vec<Lint>, |
41 | 50 | }
|
42 | 51 |
|
43 | 52 | pub trait Lints {
|
44 |
| - fn lints(&mut self) -> Vec<String>; |
| 53 | + fn lints(&mut self) -> Vec<Lint>; |
45 | 54 | }
|
46 | 55 |
|
47 | 56 | impl<T, V> From<V> for LintedItem<T>
|
|
0 commit comments