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
195
pub fn run(
41
195
    input_files: &[Input],
42
195
    input_format: &InputFormat,
43
195
    output_format: &OutputFormat,
44
195
    standalone: bool,
45
195
    color: bool,
46
195
) -> Vec<Result<String, ExportError>> {
47
195
    input_files
48
195
        .iter()
49
263
        .map(|input_file| run_export(input_file, input_format, output_format, standalone, color))
50
195
        .collect()
51
}
52

            
53
/// Run the export command for one input file
54
198
fn run_export(
55
198
    input_file: &Input,
56
198
    input_format: &InputFormat,
57
198
    output_format: &OutputFormat,
58
198
    standalone: bool,
59
198
    color: bool,
60
198
) -> Result<String, ExportError> {
61
199
    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
199
    })?;
65

            
66
    // Parse input curl or Hurl file
67
195
    let input = match input_format {
68
192
        InputFormat::Hurl => content.to_string(),
69
3
        InputFormat::Curl => curl::parse(&content).map_err(ExportError::Curl)?,
70
    };
71
197
    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
197
    })?;
76

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