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

            
19
mod commands;
20
mod matches;
21

            
22
use std::env;
23
use std::path::PathBuf;
24

            
25
use clap::ArgMatches;
26
use hurl_core::input::Input;
27

            
28
#[derive(Clone, Debug, PartialEq, Eq)]
29
pub struct Options {
30
    pub check: bool,
31
    pub color: bool,
32
    pub in_place: bool,
33
    pub input_files: Vec<Input>,
34
    pub input_format: InputFormat,
35
    pub output_file: Option<PathBuf>,
36
    pub output_format: OutputFormat,
37
    pub standalone: bool,
38
}
39

            
40
#[derive(Clone, Debug, PartialEq, Eq)]
41
pub enum InputFormat {
42
    Curl,
43
    Hurl,
44
}
45

            
46
#[derive(Clone, Debug, PartialEq, Eq)]
47
pub enum OutputFormat {
48
    Hurl,
49
    Json,
50
    Html,
51
}
52

            
53
#[derive(Clone, Debug, PartialEq, Eq)]
54
pub enum OptionsError {
55
    Info(String),
56
    Error(String),
57
}
58

            
59
impl From<clap::Error> for OptionsError {
60
6
    fn from(error: clap::Error) -> Self {
61
6
        match error.kind() {
62
3
            clap::error::ErrorKind::DisplayVersion => OptionsError::Info(error.to_string()),
63
3
            clap::error::ErrorKind::DisplayHelp => OptionsError::Info(error.to_string()),
64
            _ => OptionsError::Error(error.to_string()),
65
        }
66
    }
67
}
68

            
69
195
pub fn parse() -> Result<Options, OptionsError> {
70
195
    let mut command = clap::Command::new("hurlfmt")
71
195
        .version(clap::crate_version!())
72
195
        .disable_colored_help(true)
73
195
        .about("Format Hurl files")
74
195
        .arg(commands::check())
75
195
        .arg(commands::color())
76
195
        .arg(commands::format())
77
195
        .arg(commands::in_place())
78
195
        .arg(commands::input_files())
79
195
        .arg(commands::input_format())
80
195
        .arg(commands::no_color())
81
195
        .arg(commands::output())
82
195
        .arg(commands::output_format())
83
195
        .arg(commands::standalone());
84

            
85
195
    let arg_matches = command.try_get_matches_from_mut(env::args_os())?;
86
189
    let opts = parse_matches(&arg_matches)?;
87

            
88
189
    if opts.input_files.is_empty() {
89
        let help = command.render_help().to_string();
90
        return Err(OptionsError::Error(help));
91
    }
92
189
    Ok(opts)
93
}
94

            
95
189
fn parse_matches(arg_matches: &ArgMatches) -> Result<Options, OptionsError> {
96
189
    let check = matches::check(arg_matches);
97
189
    let color = matches::color(arg_matches);
98
189
    let in_place = matches::in_place(arg_matches)?;
99
189
    let input_files = matches::input_files(arg_matches)?;
100
189
    let input_format = matches::input_format(arg_matches)?;
101
189
    let output_file = matches::output_file(arg_matches);
102
189
    let output_format = matches::output_format(arg_matches)?;
103
189
    let standalone = matches::standalone(arg_matches)?;
104
189
    Ok(Options {
105
189
        check,
106
189
        color,
107
189
        in_place,
108
189
        input_files,
109
189
        input_format,
110
189
        output_file,
111
189
        output_format,
112
189
        standalone,
113
189
    })
114
}