1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2024 Orange
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *          http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
use hurl_core::ast::SourceInfo;
19
use hurl_core::error;
20
use hurl_core::error::DisplaySourceError;
21
use hurl_core::text::{Style, StyledString};
22

            
23
#[derive(Clone, Debug, PartialEq, Eq)]
24
pub struct LinterError {
25
    pub source_info: SourceInfo,
26
    pub kind: LinterErrorKind,
27
}
28

            
29
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
30
pub enum LinterErrorKind {
31
    UnnecessarySpace,
32
    UnnecessaryJsonEncoding,
33
    OneSpace,
34
}
35

            
36
///
37
/// Textual Output for linter errors
38
///
39
impl DisplaySourceError for LinterError {
40
45
    fn source_info(&self) -> SourceInfo {
41
45
        self.source_info
42
    }
43

            
44
9
    fn description(&self) -> String {
45
9
        match self.kind {
46
6
            LinterErrorKind::UnnecessarySpace => "Unnecessary space".to_string(),
47
            LinterErrorKind::UnnecessaryJsonEncoding => "Unnecessary json encoding".to_string(),
48
3
            LinterErrorKind::OneSpace => "One space".to_string(),
49
        }
50
    }
51

            
52
9
    fn fixme(&self, content: &[&str]) -> StyledString {
53
9
        let message = match self.kind {
54
6
            LinterErrorKind::UnnecessarySpace => "Remove space".to_string(),
55
            LinterErrorKind::UnnecessaryJsonEncoding => "Use Simple String".to_string(),
56
3
            LinterErrorKind::OneSpace => "Use only one space".to_string(),
57
        };
58
9
        let mut s = StyledString::new();
59
9
        let message = error::add_carets(&message, self.source_info(), content);
60
9
        s.push_with(&message, Style::new().cyan());
61
9
        s
62
    }
63
}