problemreductions/registry/
schema.rs1use super::FieldInfo;
4use serde::Serialize;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct VariantDimension {
12 pub key: &'static str,
14 pub default_value: &'static str,
16 pub allowed_values: &'static [&'static str],
18}
19
20impl VariantDimension {
21 pub const fn new(
23 key: &'static str,
24 default_value: &'static str,
25 allowed_values: &'static [&'static str],
26 ) -> Self {
27 Self {
28 key,
29 default_value,
30 allowed_values,
31 }
32 }
33}
34
35pub struct ProblemSchemaEntry {
37 pub name: &'static str,
39 pub display_name: &'static str,
41 pub aliases: &'static [&'static str],
43 pub dimensions: &'static [VariantDimension],
45 pub module_path: &'static str,
47 pub description: &'static str,
49 pub fields: &'static [FieldInfo],
51}
52
53inventory::collect!(ProblemSchemaEntry);
54
55pub struct ProblemSizeFieldEntry {
60 pub name: &'static str,
62 pub fields: &'static [&'static str],
64}
65
66inventory::collect!(ProblemSizeFieldEntry);
67
68#[derive(Debug, Clone, Serialize)]
70pub struct ProblemSchemaJson {
71 pub name: String,
73 pub description: String,
75 pub fields: Vec<FieldInfoJson>,
77}
78
79#[derive(Debug, Clone, Serialize)]
81pub struct FieldInfoJson {
82 pub name: String,
84 pub type_name: String,
86 pub description: String,
88}
89
90pub fn collect_schemas() -> Vec<ProblemSchemaJson> {
92 let mut schemas: Vec<ProblemSchemaJson> = inventory::iter::<ProblemSchemaEntry>
93 .into_iter()
94 .map(|entry| ProblemSchemaJson {
95 name: entry.name.to_string(),
96 description: entry.description.to_string(),
97 fields: entry
98 .fields
99 .iter()
100 .map(|f| FieldInfoJson {
101 name: f.name.to_string(),
102 type_name: f.type_name.to_string(),
103 description: f.description.to_string(),
104 })
105 .collect(),
106 })
107 .collect();
108 schemas.sort_by(|a, b| a.name.cmp(&b.name));
109 schemas
110}
111
112pub fn declared_size_fields(name: &str) -> Vec<&'static str> {
114 inventory::iter::<ProblemSizeFieldEntry>()
115 .filter(|entry| entry.name == name)
116 .flat_map(|entry| entry.fields.iter().copied())
117 .collect()
118}
119
120#[cfg(test)]
121#[path = "../unit_tests/registry/schema.rs"]
122mod tests;