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::*;
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::primitives::*;
23
use crate::parser::string::backtick_template;
24
use crate::parser::{xml, ParseResult};
25
use crate::reader::Reader;
26

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

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

            
49
24830
fn json_bytes(reader: &mut Reader) -> ParseResult<Bytes> {
50
24830
    match parse_json(reader) {
51
24515
        Err(e) => Err(e),
52
315
        Ok(value) => Ok(Bytes::Json(value)),
53
    }
54
}
55

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

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

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

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

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

            
76
#[cfg(test)]
77
mod tests {
78
    use super::super::error::*;
79
    use super::*;
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
}