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

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

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

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

            
34
9297
    match token {
35
3774
        Token::Whitespace(value) => value.clone(),
36
234
        Token::Method(value) => {
37
234
            let mut s = StyledString::new();
38
234
            s.push_with(value, Style::new().yellow());
39
234
            s.to_string(format)
40
        }
41
105
        Token::Version(value) => value.clone(),
42
105
        Token::Status(value) => value.clone(),
43
111
        Token::SectionHeader(value) => {
44
111
            let mut s = StyledString::new();
45
111
            s.push_with(value, Style::new().magenta());
46
111
            s.to_string(format)
47
        }
48
231
        Token::Comment(value) => {
49
231
            let mut s = StyledString::new();
50
231
            s.push_with(value, Style::new().bright_black());
51
231
            s.to_string(format)
52
        }
53
        Token::Value(value) => value.clone(),
54
411
        Token::Colon(value) => value.clone(),
55
318
        Token::QueryType(value) => {
56
318
            let mut s = StyledString::new();
57
318
            s.push_with(value, Style::new().cyan());
58
318
            s.to_string(format)
59
        }
60
312
        Token::PredicateType(value) => {
61
312
            let mut s = StyledString::new();
62
312
            s.push_with(value, Style::new().yellow());
63
312
            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
273
            let mut s = StyledString::new();
72
273
            s.push_with(value, Style::new().cyan());
73
273
            s.to_string(format)
74
        }
75
1449
        Token::String(value) => {
76
1449
            let mut s = StyledString::new();
77
1449
            s.push_with(value, Style::new().green());
78
1449
            s.to_string(format)
79
        }
80
1068
        Token::StringDelimiter(value) => {
81
1068
            let mut s = StyledString::new();
82
1068
            s.push_with(value, Style::new().green());
83
1068
            s.to_string(format)
84
        }
85
477
        Token::CodeDelimiter(value) => {
86
477
            let mut s = StyledString::new();
87
477
            s.push_with(value, Style::new().green());
88
477
            s.to_string(format)
89
        }
90
141
        Token::CodeVariable(value) => {
91
141
            let mut s = StyledString::new();
92
141
            s.push_with(value, Style::new().green());
93
141
            s.to_string(format)
94
        }
95
126
        Token::Keyword(value) => value.clone(),
96
90
        Token::FilterType(value) => {
97
90
            let mut s = StyledString::new();
98
90
            s.push_with(value, Style::new().cyan());
99
90
            s.to_string(format)
100
        }
101
48
        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
}