Skip to main content

axioval_ir/contract/
definitions.rs

1#![allow(missing_docs)]
2use super::{Citation, ExternalName, LocalizedText, PackageMetadata, ParameterValue, Source};
3use serde::{Deserialize, Serialize};
4use std::collections::BTreeMap;
5
6#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
7#[serde(rename_all = "camelCase", deny_unknown_fields)]
8pub struct ObjectTypeDefinition {
9    pub id: String,
10    pub name: LocalizedText,
11    pub description: Option<LocalizedText>,
12    pub external_names: Vec<ExternalName>,
13    #[serde(default)]
14    pub citations: Vec<Citation>,
15}
16#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
17#[serde(rename_all = "camelCase", deny_unknown_fields)]
18pub struct PropertyDefinition {
19    pub id: String,
20    pub name: LocalizedText,
21    pub description: Option<LocalizedText>,
22    pub value_kind: PropertyValueKind,
23    pub unit_dimension: Option<String>,
24    pub external_names: Vec<ExternalName>,
25    #[serde(default)]
26    pub citations: Vec<Citation>,
27}
28#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
29#[serde(rename_all = "camelCase", deny_unknown_fields)]
30pub struct PropertySetDefinition {
31    pub id: String,
32    pub name: LocalizedText,
33    pub description: Option<LocalizedText>,
34    pub external_names: Vec<ExternalName>,
35    #[serde(default)]
36    pub citations: Vec<Citation>,
37}
38#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
39#[serde(rename_all = "camelCase", deny_unknown_fields)]
40pub struct ParameterDefinition {
41    pub id: String,
42    pub name: LocalizedText,
43    pub description: Option<LocalizedText>,
44    pub kind: ParameterKind,
45    pub referenced_value_kind: Option<PropertyValueKind>,
46    #[serde(default = "yes")]
47    pub required: bool,
48    pub default_value: Option<ParameterValue>,
49    #[serde(default)]
50    pub allowed_values: Vec<ParameterValue>,
51    pub unit_dimension: Option<String>,
52    #[serde(default)]
53    pub citations: Vec<Citation>,
54    /// The columns of a `table` parameter, in presentation order; empty for
55    /// every other kind and then omitted, so existing packages serialize
56    /// unchanged.
57    #[serde(default, skip_serializing_if = "Vec::is_empty")]
58    pub columns: Vec<TableColumnDefinition>,
59}
60/// One named, typed column of a `table` parameter.
61#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
62#[serde(rename_all = "camelCase", deny_unknown_fields)]
63pub struct TableColumnDefinition {
64    pub id: String,
65    pub name: LocalizedText,
66    #[serde(default, skip_serializing_if = "Option::is_none")]
67    pub description: Option<LocalizedText>,
68    pub kind: ColumnKind,
69    /// Whether every row must carry a cell in this column.
70    #[serde(default = "yes")]
71    pub required: bool,
72    /// The dimension of a `quantity` column.
73    #[serde(default, skip_serializing_if = "Option::is_none")]
74    pub unit_dimension: Option<String>,
75}
76/// The kind of every cell in one table column.
77///
78/// Cells reuse the scalar value variants: a `string` or `textPattern` cell is
79/// a `string` value, a `selector` cell a `selector` value, and so on. A text
80/// pattern is a whole-value wildcard pattern (`*` any run, `?` one character,
81/// `\` escapes the next character).
82#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
83#[serde(rename_all = "camelCase")]
84pub enum ColumnKind {
85    String,
86    TextPattern,
87    Number,
88    Quantity,
89    Integer,
90    Boolean,
91    Selector,
92    Reference,
93    /// A calendar day, written as a `date` value.
94    Date,
95    /// An instant with its UTC offset, written as a `dateTime` value.
96    DateTime,
97}
98impl ColumnKind {
99    /// The kind's name in normalized packages.
100    #[must_use]
101    pub const fn as_str(self) -> &'static str {
102        match self {
103            Self::String => "string",
104            Self::TextPattern => "textPattern",
105            Self::Number => "number",
106            Self::Quantity => "quantity",
107            Self::Integer => "integer",
108            Self::Boolean => "boolean",
109            Self::Selector => "selector",
110            Self::Reference => "reference",
111            Self::Date => "date",
112            Self::DateTime => "dateTime",
113        }
114    }
115}
116#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
117#[serde(deny_unknown_fields)]
118pub struct RuleDefinition {
119    pub id: String,
120    pub name: LocalizedText,
121    pub description: Option<LocalizedText>,
122    pub capability: String,
123    #[serde(default)]
124    pub parameters: BTreeMap<String, ParameterDefinition>,
125    #[serde(default)]
126    pub tags: Vec<String>,
127    #[serde(default)]
128    pub citations: Vec<Citation>,
129}
130#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
131#[serde(rename_all = "camelCase", deny_unknown_fields)]
132pub struct DefinitionPackage {
133    pub schema_version: String,
134    pub package: PackageMetadata,
135    #[serde(default)]
136    pub sources: BTreeMap<String, Source>,
137    #[serde(default)]
138    pub object_types: BTreeMap<String, ObjectTypeDefinition>,
139    #[serde(default)]
140    pub properties: BTreeMap<String, PropertyDefinition>,
141    #[serde(default)]
142    pub property_sets: BTreeMap<String, PropertySetDefinition>,
143    #[serde(default)]
144    pub definitions: BTreeMap<String, RuleDefinition>,
145}
146#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
147#[serde(rename_all = "camelCase")]
148pub enum ParameterKind {
149    String,
150    Boolean,
151    Integer,
152    Number,
153    Quantity,
154    Enum,
155    /// An ISO 8601 calendar date.
156    Date,
157    /// An ISO 8601 date-time with a UTC offset.
158    DateTime,
159    Reference,
160    ObjectTypeReference,
161    PropertyReference,
162    Selector,
163    StringList,
164    ReferenceList,
165    /// Rows of typed cells; the definition declares the columns.
166    Table,
167}
168#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
169#[serde(rename_all = "camelCase")]
170pub enum PropertyValueKind {
171    String,
172    Boolean,
173    Integer,
174    Number,
175    Quantity,
176    Enum,
177    /// An ISO 8601 calendar date.
178    Date,
179    /// An ISO 8601 date-time with a UTC offset.
180    DateTime,
181    Reference,
182    StringList,
183    ReferenceList,
184}
185const fn yes() -> bool {
186    true
187}