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 crate::ast::Bytes;
19
use crate::combinator::choice;
20
use crate::parser::json::parse as parse_json;
21
use crate::parser::multiline::multiline_string;
22
use crate::parser::string::backtick_template;
23
use crate::parser::{primitives, xml, ParseResult};
24
use crate::reader::Reader;
25

            
26
27035
pub fn bytes(reader: &mut Reader) -> ParseResult<Bytes> {
27
27035
    choice(
28
27035
        &[
29
27035
            multiline_string_bytes,
30
27035
            string_bytes,
31
27035
            json_bytes,
32
27035
            xml_bytes,
33
27035
            base64_bytes,
34
27035
            hex_bytes,
35
27035
            file_bytes,
36
27035
        ],
37
27035
        reader,
38
27035
    )
39
}
40

            
41
24530
fn xml_bytes(reader: &mut Reader) -> ParseResult<Bytes> {
42
24530
    match xml::parse(reader) {
43
24425
        Err(e) => Err(e),
44
105
        Ok(value) => Ok(Bytes::Xml(value)),
45
    }
46
}
47

            
48
24910
fn json_bytes(reader: &mut Reader) -> ParseResult<Bytes> {
49
24910
    match parse_json(reader) {
50
24570
        Err(e) => Err(e),
51
340
        Ok(value) => Ok(Bytes::Json(value)),
52
    }
53
}
54

            
55
23955
fn file_bytes(reader: &mut Reader) -> ParseResult<Bytes> {
56
23955
    primitives::file(reader).map(Bytes::File)
57
}
58

            
59
24420
fn base64_bytes(reader: &mut Reader) -> ParseResult<Bytes> {
60
24420
    primitives::base64(reader).map(Bytes::Base64)
61
}
62

            
63
24135
fn hex_bytes(reader: &mut Reader) -> ParseResult<Bytes> {
64
24135
    primitives::hex(reader).map(Bytes::Hex)
65
}
66

            
67
27035
pub fn multiline_string_bytes(reader: &mut Reader) -> ParseResult<Bytes> {
68
27035
    multiline_string(reader).map(Bytes::MultilineString)
69
}
70

            
71
26385
fn string_bytes(reader: &mut Reader) -> ParseResult<Bytes> {
72
26385
    backtick_template(reader).map(Bytes::OnelineString)
73
}
74

            
75
#[cfg(test)]
76
mod tests {
77
    use super::super::error::*;
78
    use super::*;
79
    use crate::ast::{JsonListElement, JsonValue, SourceInfo, Template, TemplateElement};
80
    use crate::reader::Pos;
81

            
82
    #[test]
83
    fn test_bytes_json() {
84
        let mut reader = Reader::new("[1,2,3] ");
85
        assert_eq!(
86
            bytes(&mut reader).unwrap(),
87
            Bytes::Json(JsonValue::List {
88
                space0: String::new(),
89
                elements: vec![
90
                    JsonListElement {
91
                        space0: String::new(),
92
                        value: JsonValue::Number("1".to_string()),
93
                        space1: String::new(),
94
                    },
95
                    JsonListElement {
96
                        space0: String::new(),
97
                        value: JsonValue::Number("2".to_string()),
98
                        space1: String::new(),
99
                    },
100
                    JsonListElement {
101
                        space0: String::new(),
102
                        value: JsonValue::Number("3".to_string()),
103
                        space1: String::new(),
104
                    },
105
                ],
106
            })
107
        );
108
        assert_eq!(reader.cursor().index, 7);
109

            
110
        let mut reader = Reader::new("{ } ");
111
        assert_eq!(
112
            bytes(&mut reader).unwrap(),
113
            Bytes::Json(JsonValue::Object {
114
                space0: " ".to_string(),
115
                elements: vec![],
116
            })
117
        );
118
        assert_eq!(reader.cursor().index, 3);
119

            
120
        let mut reader = Reader::new("true");
121
        assert_eq!(
122
            bytes(&mut reader).unwrap(),
123
            Bytes::Json(JsonValue::Boolean(true))
124
        );
125
        assert_eq!(reader.cursor().index, 4);
126

            
127
        let mut reader = Reader::new("\"\" x");
128
        assert_eq!(
129
            bytes(&mut reader).unwrap(),
130
            Bytes::Json(JsonValue::String(Template {
131
                delimiter: Some('"'),
132
                elements: vec![],
133
                source_info: SourceInfo::new(Pos::new(1, 2), Pos::new(1, 2)),
134
            }))
135
        );
136
        assert_eq!(reader.cursor().index, 2);
137
    }
138

            
139
    #[test]
140
    fn test_bytes_xml() {
141
        let mut reader = Reader::new("<a/>");
142
        assert_eq!(
143
            bytes(&mut reader).unwrap(),
144
            Bytes::Xml(String::from("<a/>"))
145
        );
146
    }
147

            
148
    #[test]
149
    fn test_bytes_json_error() {
150
        let mut reader = Reader::new("{ x ");
151
        let error = bytes(&mut reader).err().unwrap();
152
        assert_eq!(error.pos, Pos { line: 1, column: 3 });
153
        assert_eq!(
154
            error.kind,
155
            ParseErrorKind::Expecting {
156
                value: "\"".to_string()
157
            },
158
        );
159
    }
160

            
161
    #[test]
162
    fn test_bytes_multilines_error() {
163
        let mut reader = Reader::new("```\nxxx ");
164
        let error = bytes(&mut reader).err().unwrap();
165
        assert_eq!(error.pos, Pos { line: 2, column: 5 });
166
        assert_eq!(
167
            error.kind,
168
            ParseErrorKind::Expecting {
169
                value: "```".to_string()
170
            }
171
        );
172
    }
173

            
174
    #[test]
175
    fn test_bytes_eof() {
176
        let mut reader = Reader::new("");
177
        let error = bytes(&mut reader).err().unwrap();
178
        assert_eq!(
179
            error.kind,
180
            ParseErrorKind::Expecting {
181
                value: String::from("file")
182
            }
183
        );
184
        assert!(error.recoverable);
185
    }
186

            
187
    #[test]
188
    fn test_json_bytes() {
189
        let mut reader = Reader::new("100");
190
        assert_eq!(
191
            json_bytes(&mut reader).unwrap(),
192
            Bytes::Json(JsonValue::Number("100".to_string()))
193
        );
194
    }
195

            
196
    #[test]
197
    fn test_bytes_string() {
198
        let mut reader = Reader::new("`foo`  ");
199
        assert_eq!(
200
            bytes(&mut reader).unwrap(),
201
            Bytes::OnelineString(Template {
202
                delimiter: Some('`'),
203
                elements: vec![TemplateElement::String {
204
                    value: "foo".to_string(),
205
                    encoded: "foo".to_string()
206
                }],
207
                source_info: SourceInfo::new(Pos::new(1, 1), Pos::new(1, 6))
208
            })
209
        );
210
        assert_eq!(reader.cursor().index, 5);
211
    }
212
}