Debian Codegraph @

fcheck-testdata/src/defs.rs

raw
1 // SPDX-FileCopyrightText: Peter Pentchev <[email protected]>
2 // SPDX-License-Identifier: BSD-2-Clause
3 //! Common definitions for the feature-check test generator.
4
5 use std::collections::HashMap;
6
7 use anyhow::Error as AnyError;
8 use handlebars::{RenderError, TemplateError};
9 use serde::{Deserialize, Serialize};
10 use serde_json::Error as JsonError;
11 use thiserror::Error;
12 use typed_format_version::LoadError as TypedError;
13
14 /// An error that occurred during the preparation of the test data.
15 #[derive(Debug, Error)]
16 #[non_exhaustive]
17 #[allow(clippy::error_impl_error)]
18 pub enum Error {
19 /// Could not read, write, examine, etc. a file.
20 #[error("An I/O error occurred")]
21 IO(#[source] AnyError),
22
23 /// Could not load the test data.
24 #[error("Could not parse the test data file")]
25 ParseJson(#[source] JsonError),
26
27 /// Could not load the handlebars template.
28 #[error("Could not parse the output template file")]
29 ParseTemplate(#[source] TemplateError),
30
31 /// Could not render a handlebars template.
32 #[error("Could not render the {0} template using the {1} data")]
33 Render(String, String, #[source] RenderError),
34
35 /// Could not parse the data file's format version.
36 #[error("Could not parse the data format version")]
37 VersionLoad(#[source] TypedError),
38
39 /// Unknown format version for the data file.
40 #[error("Unsupported data format version {0}.{1}")]
41 VersionUnknown(u32, u32),
42 }
43
44 /// Runtime configuration for the feature-check test generator.
45 #[derive(Debug)]
46 pub struct Config {
47 /// The path to the JSON test data file.
48 pub data_path: String,
49 /// The path to the output file to generate.
50 pub output_path: String,
51 /// The path to the Handlebars template to use.
52 pub template_path: String,
53 /// Verbose operation (diagnostic output).
54 pub verbose: bool,
55 }
56
57 /// The version of a test data's format specification.
58 #[derive(Debug, Deserialize, Serialize)]
59 pub struct TestFormatVersion {
60 /// The version major number.
61 pub major: u32,
62 /// The version minor number.
63 pub minor: u32,
64 }
65
66 /// Metadata about a test data file's format.
67 #[derive(Debug, Deserialize, Serialize)]
68 pub struct TestFormat {
69 /// The version of the test format specification.
70 pub version: TestFormatVersion,
71 }
72
73 /// A synonym for a comparison operator.
74 #[derive(Debug, Deserialize, Serialize)]
75 pub struct TestOpSynonym {
76 /// The word name of the comparison operator.
77 pub name: String,
78 /// The symbol to use for the comparison operator.
79 pub sym: String,
80 }
81
82 /// Test data for a single simple expression check.
83 #[derive(Debug, Deserialize, Serialize)]
84 pub struct TestSimpleCompare {
85 /// The feature to compare against ("base" or "beta").
86 pub name: String,
87 /// The comparison operator.
88 pub op: String,
89 /// The version to compare against.
90 pub version: String,
91 /// The comparison result.
92 pub result: bool,
93 }
94
95 /// Test data for simple expression checks.
96 #[derive(Debug, Deserialize, Serialize)]
97 pub struct SimpleTestData {
98 /// The base version to compare against.
99 pub features: HashMap<String, String>,
100 /// The synonyms to define for the comparison operators.
101 pub synonyms: Vec<TestOpSynonym>,
102 /// The versions to compare against.
103 pub compare: Vec<TestSimpleCompare>,
104 }
105
106 /// Test data parsed out of an input file.
107 #[derive(Debug, Deserialize, Serialize)]
108 pub struct TestData {
109 /// The input file's metadata format info.
110 pub format: TestFormat,
111 /// The simple expression tests to run.
112 pub simple: SimpleTestData,
113 }
114
115 /// The name of the template to use within the Handlebars renderer.
116 pub const TEMPLATE_NAME: &str = "test";