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
use hurl_core::ast::HurlFile;
19
use hurl_core::text::{Format, Style, StyledString};
20

            
21
use crate::format::{Token, Tokenizable};
22

            
23
81
pub fn format(hurl_file: &HurlFile, color: bool) -> String {
24
81
    let mut buffer = String::new();
25
10338
    for token in &hurl_file.tokenize() {
26
10338
        buffer.push_str(format_token(token, color).as_str());
27
    }
28
81
    buffer
29
}
30

            
31
10338
pub fn format_token(token: &Token, color: bool) -> String {
32
10338
    let format = if color { Format::Ansi } else { Format::Plain };
33

            
34
10338
    match token {
35
4182
        Token::Whitespace(value) => value.clone(),
36
264
        Token::Method(value) => {
37
264
            let mut s = StyledString::new();
38
264
            s.push_with(value, Style::new().yellow());
39
264
            s.to_string(format)
40
        }
41
108
        Token::Version(value) => value.clone(),
42
108
        Token::Status(value) => value.clone(),
43
123
        Token::SectionHeader(value) => {
44
123
            let mut s = StyledString::new();
45
123
            s.push_with(value, Style::new().magenta());
46
123
            s.to_string(format)
47
        }
48
273
        Token::Comment(value) => {
49
273
            let mut s = StyledString::new();
50
273
            s.push_with(value, Style::new().bright_black());
51
273
            s.to_string(format)
52
        }
53
        Token::Value(value) => value.clone(),
54
444
        Token::Colon(value) => value.clone(),
55
366
        Token::QueryType(value) => {
56
366
            let mut s = StyledString::new();
57
366
            s.push_with(value, Style::new().cyan());
58
366
            s.to_string(format)
59
        }
60
348
        Token::PredicateType(value) => {
61
348
            let mut s = StyledString::new();
62
348
            s.push_with(value, Style::new().yellow());
63
348
            s.to_string(format)
64
        }
65
3
        Token::Not(value) => {
66
3
            let mut s = StyledString::new();
67
3
            s.push_with(value, Style::new().yellow());
68
3
            s.to_string(format)
69
        }
70
210
        Token::Boolean(value) | Token::Number(value) => {
71
282
            let mut s = StyledString::new();
72
282
            s.push_with(value, Style::new().cyan());
73
282
            s.to_string(format)
74
        }
75
1605
        Token::String(value) => {
76
1605
            let mut s = StyledString::new();
77
1605
            s.push_with(value, Style::new().green());
78
1605
            s.to_string(format)
79
        }
80
1191
        Token::StringDelimiter(value) => {
81
1191
            let mut s = StyledString::new();
82
1191
            s.push_with(value, Style::new().green());
83
1191
            s.to_string(format)
84
        }
85
543
        Token::CodeDelimiter(value) => {
86
543
            let mut s = StyledString::new();
87
543
            s.push_with(value, Style::new().green());
88
543
            s.to_string(format)
89
        }
90
174
        Token::CodeVariable(value) => {
91
174
            let mut s = StyledString::new();
92
174
            s.push_with(value, Style::new().green());
93
174
            s.to_string(format)
94
        }
95
132
        Token::Keyword(value) => value.clone(),
96
111
        Token::FilterType(value) => {
97
111
            let mut s = StyledString::new();
98
111
            s.push_with(value, Style::new().cyan());
99
111
            s.to_string(format)
100
        }
101
57
        Token::Lang(value) => value.clone(),
102
24
        Token::Unit(value) => {
103
24
            let mut s = StyledString::new();
104
24
            s.push_with(value, Style::new().cyan());
105
24
            s.to_string(format)
106
        }
107
    }
108
}