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

            
21
use crate::format::token::*;
22

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

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

            
34
9135
    match token {
35
3723
        Token::Whitespace(value) => value.clone(),
36
228
        Token::Method(value) => {
37
228
            let mut s = StyledString::new();
38
228
            s.push_with(value, Style::new().yellow());
39
228
            s.to_string(format)
40
        }
41
105
        Token::Version(value) => value.clone(),
42
105
        Token::Status(value) => value.clone(),
43
108
        Token::SectionHeader(value) => {
44
108
            let mut s = StyledString::new();
45
108
            s.push_with(value, Style::new().magenta());
46
108
            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
390
        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
201
        Token::Boolean(value) | Token::Number(value) => {
71
267
            let mut s = StyledString::new();
72
267
            s.push_with(value, Style::new().cyan());
73
267
            s.to_string(format)
74
        }
75
1416
        Token::String(value) => {
76
1416
            let mut s = StyledString::new();
77
1416
            s.push_with(value, Style::new().green());
78
1416
            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
453
        Token::CodeDelimiter(value) => {
86
453
            let mut s = StyledString::new();
87
453
            s.push_with(value, Style::new().green());
88
453
            s.to_string(format)
89
        }
90
129
        Token::CodeVariable(value) => {
91
129
            let mut s = StyledString::new();
92
129
            s.push_with(value, Style::new().green());
93
129
            s.to_string(format)
94
        }
95
123
        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
18
        Token::Unit(value) => {
103
18
            let mut s = StyledString::new();
104
18
            s.push_with(value, Style::new().cyan());
105
18
            s.to_string(format)
106
        }
107
    }
108
}