valve_kv_tools/
linter.rs

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
use std::collections::HashMap;

use pest::error::LineColLocation;

#[cfg(target_arch = "wasm32")]
use js_sys::Array;
use wasm_bindgen::prelude::wasm_bindgen;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::JsValue;

use crate::{KeyValue, Position, Range, Value};

#[wasm_bindgen]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub enum KvErrorKind {
    #[default]
    SyntaxError,
    DuplicateError,
}

/// Representation of a KeyValue linter error
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct KvError {
    /// Range of the error
    pub range: Range,

    /// Ranges that are related to the error
    /// For example, the range of duplicate entries in a duplicate error
    pub additional_ranges: Vec<Range>,

    /// Error message of the error
    pub message: String,

    /// Kind of the error
    pub kind: KvErrorKind,
}

#[cfg(target_arch = "wasm32")]
impl KvError {
    pub(crate) fn to_js(&self) -> KvErrorJs {
        KvErrorJs {
            range: self.range,
            additional_ranges: self
                .additional_ranges
                .clone()
                .into_iter()
                .map(JsValue::from)
                .collect(),
            message: self.message.clone(),
            kind: self.kind,
        }
    }
}

/// Representation of a KeyValue linter error
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(js_name = KvError, getter_with_clone)]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct KvErrorJs {
    /// Range of the error
    #[wasm_bindgen(readonly)]
    pub range: Range,

    /// Ranges that are related to the error
    /// For example, the range of duplicate entries in a duplicate error
    #[wasm_bindgen(js_name = additionalRanges, readonly)]
    pub additional_ranges: Array,

    /// Error message of the error
    #[wasm_bindgen(readonly)]
    pub message: String,

    /// Kind of the error
    #[wasm_bindgen(readonly)]
    pub kind: KvErrorKind,
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
impl KvErrorJs {
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        Self::default()
    }
}

pub fn lint_keyvalue(input: &str) -> Vec<KvError> {
    let mut errors = vec![];
    let serialized = super::serialize_keyvalue(input);
    match serialized {
        Err(err) => {
            let range = match err.line_col {
                LineColLocation::Pos(pos) => Range {
                    start: Position {
                        line: pos.0 as u32,
                        character: pos.1 as u32,
                    },
                    end: Position {
                        line: pos.0 as u32,
                        character: pos.1 as u32,
                    },
                },
                LineColLocation::Span(start, end) => Range {
                    start: Position {
                        line: start.0 as u32,
                        character: start.1 as u32,
                    },
                    end: Position {
                        line: end.0 as u32,
                        character: end.1 as u32,
                    },
                },
            };
            errors.push(KvError {
                range,
                additional_ranges: vec![],
                message: err.variant.message().to_string(),
                kind: KvErrorKind::SyntaxError,
            });
        }
        Ok(kv) => {
            let mut dups = vec![];
            search_for_duplicates(&mut dups, &[kv]);
            for dup in dups {
                errors.push(KvError {
                    range: dup.original_declaration,
                    additional_ranges: dup.duplicate_declarations,
                    message: format!("Duplicate entry for key \"{}\"", dup.key),
                    kind: KvErrorKind::DuplicateError,
                });
            }
        }
    }

    errors
}

struct Duplicate {
    key: String,
    original_declaration: Range,
    duplicate_declarations: Vec<Range>,
}

impl Duplicate {
    fn new(keyvalue: &KeyValue) -> Self {
        Self {
            key: keyvalue.key.clone(),
            original_declaration: keyvalue.key_range,
            duplicate_declarations: vec![],
        }
    }
}

fn search_for_duplicates(dups: &mut Vec<Duplicate>, keyvalues: &[KeyValue]) {
    let mut keys: HashMap<String, Duplicate> = HashMap::default();
    for kv in keyvalues.iter() {
        match &kv.value {
            Value::String(_) => {
                if let Some(dup) = keys.get_mut(&kv.key) {
                    dup.duplicate_declarations.push(kv.key_range);
                } else {
                    keys.insert(kv.key.clone(), Duplicate::new(kv));
                }
            }
            Value::Section(section_val) => {
                if let Some(dup) = keys.get_mut(&kv.key) {
                    dup.duplicate_declarations.push(kv.key_range);
                } else {
                    keys.insert(kv.key.clone(), Duplicate::new(kv));
                }
                search_for_duplicates(dups, section_val)
            }
        }
    }
    dups.extend(keys.into_iter().filter_map(|(_, dup)| {
        if !dup.duplicate_declarations.is_empty() {
            Some(dup)
        } else {
            None
        }
    }));
}