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

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

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

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

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

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

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

            
84
186
    let arg_matches = command.try_get_matches_from_mut(env::args_os())?;
85
180
    let opts = parse_matches(&arg_matches)?;
86

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

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