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::cli::options::{InputFormat, OutputFormat};
23
use crate::{curl, format, linter};
24

            
25
/// Represents an export error.
26
pub enum ExportError {
27
    IO {
28
        filename: String,
29
        message: String,
30
    },
31
    Parse {
32
        content: String,
33
        input_file: Input,
34
        error: ParseError,
35
    },
36
    Curl(String),
37
}
38

            
39
/// Run the export command for a list of input files
40
186
pub fn run(
41
186
    input_files: &[Input],
42
186
    input_format: &InputFormat,
43
186
    output_format: &OutputFormat,
44
186
    standalone: bool,
45
186
    color: bool,
46
186
) -> Vec<Result<String, ExportError>> {
47
186
    input_files
48
186
        .iter()
49
251
        .map(|input_file| run_export(input_file, input_format, output_format, standalone, color))
50
186
        .collect()
51
}
52

            
53
/// Run the export command for one input file
54
189
fn run_export(
55
189
    input_file: &Input,
56
189
    input_format: &InputFormat,
57
189
    output_format: &OutputFormat,
58
189
    standalone: bool,
59
189
    color: bool,
60
189
) -> Result<String, ExportError> {
61
190
    let content = input_file.read_to_string().map_err(|e| ExportError::IO {
62
3
        filename: input_file.to_string(),
63
3
        message: e.to_string(),
64
190
    })?;
65

            
66
    // Parse input curl or Hurl file
67
186
    let input = match input_format {
68
183
        InputFormat::Hurl => content.to_string(),
69
3
        InputFormat::Curl => curl::parse(&content).map_err(ExportError::Curl)?,
70
    };
71
188
    let hurl_file = parser::parse_hurl_file(&input).map_err(|error| ExportError::Parse {
72
6
        content: input.clone(),
73
6
        input_file: input_file.clone(),
74
6
        error,
75
188
    })?;
76

            
77
180
    let output = match output_format {
78
        OutputFormat::Hurl => {
79
69
            let hurl_file = linter::lint_hurl_file(&hurl_file);
80
69
            format::format_text(&hurl_file, color)
81
        }
82
54
        OutputFormat::Json => format::format_json(&hurl_file),
83
57
        OutputFormat::Html => hurl_core::format::format_html(&hurl_file, standalone),
84
    };
85
180
    Ok(output)
86
}