1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2025 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

            
19
use hurl_core::input::Input;
20
use hurl_core::parser::{self, ParseError};
21

            
22
use crate::{format, linter};
23

            
24
/// Represents a check error.
25
pub enum CheckError {
26
    IO {
27
        filename: String,
28
        message: String,
29
    },
30
    Parse {
31
        content: String,
32
        input_file: Input,
33
        error: ParseError,
34
    },
35
    Unformatted(String),
36
}
37

            
38
/// Run the check command for a list of input files
39
6
pub fn run(input_files: &[Input]) -> Vec<CheckError> {
40
6
    let mut errors = vec![];
41
21
    for input_file in input_files {
42
15
        if let Err(e) = run_check(input_file) {
43
9
            errors.push(e);
44
        }
45
    }
46
6
    errors
47
}
48

            
49
/// Run the check command for one input file
50
15
fn run_check(input_file: &Input) -> Result<(), CheckError> {
51
16
    let content = input_file.read_to_string().map_err(|e| CheckError::IO {
52
3
        filename: input_file.to_string(),
53
3
        message: e.to_string(),
54
16
    })?;
55
13
    let hurl_file = parser::parse_hurl_file(&content).map_err(|error| CheckError::Parse {
56
3
        content: content.clone(),
57
3
        input_file: input_file.clone(),
58
3
        error,
59
13
    })?;
60
9
    let hurl_file = linter::lint_hurl_file(&hurl_file);
61
9
    let formatted = format::format_text(&hurl_file, false);
62
9
    if formatted == content {
63
6
        Ok(())
64
    } else {
65
3
        Err(CheckError::Unformatted(input_file.to_string()))
66
    }
67
}