-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathbuild.rs
More file actions
330 lines (304 loc) · 11.6 KB
/
build.rs
File metadata and controls
330 lines (304 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (c) 2014-2019 Appsicle
* Copyright (c) 2019-2025 QuestDB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#[cfg(feature = "json_tests")]
pub mod json_tests {
use indoc::indoc;
use serde::{Deserialize, Serialize};
use serde_json;
use slugify::slugify;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::PathBuf;
#[derive(Debug, Serialize, Deserialize)]
struct Symbol {
name: String,
value: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct StringColumn {
name: String,
value: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct LongColumn {
name: String,
value: i64,
}
#[derive(Debug, Serialize, Deserialize)]
struct DoubleColumn {
name: String,
value: f64,
}
#[derive(Debug, Serialize, Deserialize)]
struct BooleanColumn {
name: String,
value: bool,
}
#[derive(Debug, Serialize, Deserialize)]
struct DecimalColumn {
name: String,
value: String,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "UPPERCASE")]
enum Column {
String(StringColumn),
Long(LongColumn),
Double(DoubleColumn),
Boolean(BooleanColumn),
Decimal(DecimalColumn),
}
#[derive(Debug, Serialize, Deserialize)]
struct Expected {
line: Option<String>,
#[serde(rename = "binaryBase64")]
binary_base64: Option<String>,
#[serde(rename = "anyLines")]
any_lines: Option<Vec<String>>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "status", rename_all = "UPPERCASE")]
enum Outcome {
Success(Expected),
Error,
}
fn default_minimum_protocol_version() -> u32 {
1
}
#[derive(Debug, Serialize, Deserialize)]
struct TestSpec {
#[serde(rename = "testName")]
test_name: String,
#[serde(
rename = "minimumProtocolVersion",
default = "default_minimum_protocol_version"
)]
minimum_protocol_version: u32,
table: String,
symbols: Vec<Symbol>,
columns: Vec<Column>,
result: Outcome,
}
fn parse() -> Vec<TestSpec> {
let mut json_path = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
json_path.push("src");
json_path.push("tests");
json_path.push("interop");
json_path.push("ilp-client-interop-test.json");
let file = std::fs::File::open(json_path).unwrap();
serde_json::from_reader(file).unwrap()
}
pub fn build() -> Result<(), Box<dyn std::error::Error>> {
let specs = parse();
// eprintln!("Parsed JSON: {:#?}", specs);
let mut file_path = PathBuf::from(std::env::var("OUT_DIR")?);
file_path.push("json_tests.rs");
let mut output = BufWriter::new(File::create(file_path)?);
// let mut output = String::new();
writeln!(
output,
"{}",
indoc! {r#"
// This file is auto-generated by build.rs.
use crate::{Result, ingress::{Buffer, ProtocolVersion}};
use crate::tests::{TestResult};
use base64ct::Base64;
use base64ct::Encoding;
use rstest::rstest;
use bigdecimal::BigDecimal;
use std::str::FromStr;
fn matches_any_line(line: &[u8], expected: &[&str]) -> bool {
for &exp in expected {
if line == exp.as_bytes() {
return true;
}
}
let line = String::from_utf8_lossy(line);
eprintln!("Could not match:\n {line:?}\nTo any of: {expected:#?}");
false
}
"#}
)?;
for (index, spec) in specs.iter().enumerate() {
write!(
output,
indoc! {r#"
/// {}
#[rstest]
fn test_{:03}_{}(
#[values(ProtocolVersion::V1, ProtocolVersion::V2, ProtocolVersion::V3)] version: ProtocolVersion
) -> TestResult {{
if (version as u8) < {} {{
return Ok(());
}}
let mut buffer = Buffer::new(version);
"#},
spec.test_name,
index,
slugify!(&spec.test_name, separator = "_"),
spec.minimum_protocol_version
)?;
let (expected, indent) = match &spec.result {
Outcome::Success(line) => (Some(line), ""),
Outcome::Error => (None, " "),
};
if expected.is_none() {
writeln!(output, " || -> Result<()> {{")?;
}
writeln!(output, "{indent} buffer")?;
writeln!(output, "{indent} .table({:?})?", spec.table)?;
for symbol in spec.symbols.iter() {
writeln!(
output,
"{} .symbol({:?}, {:?})?",
indent, symbol.name, symbol.value
)?;
}
for column in spec.columns.iter() {
match column {
Column::String(column) => writeln!(
output,
"{} .column_str({:?}, {:?})?",
indent, column.name, column.value
)?,
Column::Long(column) => writeln!(
output,
"{} .column_i64({:?}, {:?})?",
indent, column.name, column.value
)?,
Column::Double(column) => writeln!(
output,
"{} .column_f64({:?}, {:?})?",
indent, column.name, column.value
)?,
Column::Boolean(column) => writeln!(
output,
"{} .column_bool({:?}, {:?})?",
indent, column.name, column.value
)?,
Column::Decimal(column) => writeln!(
output,
"{} .column_dec({:?}, &BigDecimal::from_str({:?}).unwrap())?",
indent, column.name, column.value
)?,
}
}
writeln!(output, "{indent} .at_now()?;")?;
if let Some(expected) = expected {
if let Some(ref base64) = expected.binary_base64 {
writeln!(output, " if version != ProtocolVersion::V1 {{")?;
writeln!(
output,
" let exp = Base64::decode_vec(\"{base64}\").unwrap();"
)?;
writeln!(
output,
" assert_eq!(buffer.as_bytes(), exp.as_slice());"
)?;
writeln!(output, " }} else {{")?;
if let Some(ref line) = expected.line {
let exp_ln = format!("{line}\n");
writeln!(output, " let exp = {exp_ln:?};")?;
writeln!(
output,
" assert_eq!(buffer.as_bytes(), exp.as_bytes());"
)?;
} else {
// Checking V1 any_lines
let any: Vec<String> = expected
.any_lines
.as_ref()
.unwrap()
.iter()
.map(|line| format!("{line}\n"))
.collect();
writeln!(output, " let any = [")?;
for line in any.iter() {
writeln!(output, " {line:?},")?;
}
writeln!(output, " ];")?;
writeln!(
output,
" assert!(matches_any_line(buffer.as_bytes(), &any));"
)?;
}
writeln!(output, " }}")?;
} else if let Some(ref line) = expected.line {
let exp_ln = format!("{line}\n");
writeln!(output, " let exp = {exp_ln:?};")?;
writeln!(output, " assert_eq!(buffer.as_bytes(), exp.as_bytes());")?;
} else {
let any: Vec<String> = expected
.any_lines
.as_ref()
.unwrap()
.iter()
.map(|line| format!("{line}\n"))
.collect();
writeln!(output, " let any = [")?;
for line in any.iter() {
writeln!(output, " {line:?},")?;
}
writeln!(output, " ];")?;
writeln!(
output,
" assert!(matches_any_line(buffer.as_bytes(), &any));"
)?;
}
} else {
writeln!(output, " Ok(())")?;
writeln!(output, " }}().unwrap_err();")?;
}
writeln!(output, " Ok(())")?;
writeln!(output, "}}")?;
}
Ok(())
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(not(any(feature = "tls-webpki-certs", feature = "tls-native-certs")))]
compile_error!(
"At least one of `tls-webpki-certs` or `tls-native-certs` features must be enabled."
);
#[cfg(not(any(feature = "_sender-tcp", feature = "_sender-http")))]
compile_error!(
"At least one of `sync-sender-tcp` or `sync-sender-http` features must be enabled"
);
#[cfg(not(any(feature = "aws-lc-crypto", feature = "ring-crypto")))]
compile_error!(
"You must enable exactly one of the `aws-lc-crypto` or `ring-crypto` features, but none are enabled."
);
#[cfg(all(feature = "aws-lc-crypto", feature = "ring-crypto"))]
compile_error!(
"You must enable exactly one of the `aws-lc-crypto` or `ring-crypto` features, but both are enabled."
);
#[cfg(feature = "json_tests")]
{
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=Cargo.lock");
println!("cargo:rerun-if-changed=src/test/interop/ilp-client-interop-test.json");
json_tests::build()?;
}
Ok(())
}