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
10263
    for token in &hurl_file.tokenize() {
26
10263
        buffer.push_str(format_token(token, color).as_str());
27
    }
28
81
    buffer
29
}
30

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

            
34
10263
    match token {
35
4155
        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
270
        Token::Comment(value) => {
49
270
            let mut s = StyledString::new();
50
270
            s.push_with(value, Style::new().bright_black());
51
270
            s.to_string(format)
52
        }
53
        Token::Value(value) => value.clone(),
54
438
        Token::Colon(value) => value.clone(),
55
363
        Token::QueryType(value) => {
56
363
            let mut s = StyledString::new();
57
363
            s.push_with(value, Style::new().cyan());
58
363
            s.to_string(format)
59
        }
60
345
        Token::PredicateType(value) => {
61
345
            let mut s = StyledString::new();
62
345
            s.push_with(value, Style::new().yellow());
63
345
            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
207
        Token::Boolean(value) | Token::Number(value) => {
71
279
            let mut s = StyledString::new();
72
279
            s.push_with(value, Style::new().cyan());
73
279
            s.to_string(format)
74
        }
75
1596
        Token::String(value) => {
76
1596
            let mut s = StyledString::new();
77
1596
            s.push_with(value, Style::new().green());
78
1596
            s.to_string(format)
79
        }
80
1185
        Token::StringDelimiter(value) => {
81
1185
            let mut s = StyledString::new();
82
1185
            s.push_with(value, Style::new().green());
83
1185
            s.to_string(format)
84
        }
85
537
        Token::CodeDelimiter(value) => {
86
537
            let mut s = StyledString::new();
87
537
            s.push_with(value, Style::new().green());
88
537
            s.to_string(format)
89
        }
90
171
        Token::CodeVariable(value) => {
91
171
            let mut s = StyledString::new();
92
171
            s.push_with(value, Style::new().green());
93
171
            s.to_string(format)
94
        }
95
132
        Token::Keyword(value) => value.clone(),
96
108
        Token::FilterType(value) => {
97
108
            let mut s = StyledString::new();
98
108
            s.push_with(value, Style::new().cyan());
99
108
            s.to_string(format)
100
        }
101
57
        Token::Lang(value) => value.clone(),
102
21
        Token::Unit(value) => {
103
21
            let mut s = StyledString::new();
104
21
            s.push_with(value, Style::new().cyan());
105
21
            s.to_string(format)
106
        }
107
    }
108
}