mdmodels 0.2.9

A tool to generate models, code and schemas from markdown files
Documentation
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! This file contains Rust struct definitions with serde serialization.
//!
//! WARNING: This is an auto-generated file.
//! Do not edit directly - any changes will be overwritten.

use derive_builder::Builder;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use derivative::Derivative;
use std::collections::HashMap;
use serde_json::Value;
use uuid;

//
// Type definitions
//
/// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
/// eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
/// enim ad minim veniam, quis nostrud exercitation ullamco laboris
/// nisi ut aliquip ex ea commodo consequat.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Builder, Derivative)]
#[derivative(Default)]
#[serde(default)]
#[allow(non_snake_case)]
pub struct Test {
    /// JSON-LD header
    #[serde(flatten)]
    #[builder(default = "default_test_jsonld_header()")]
    #[derivative(Default(value = "default_test_jsonld_header()"))]
    pub jsonld: Option<JsonLdHeader>,

    /// The name of the test. This is a unique identifier that helps track
    /// individual test cases across the system. It should be
    /// descriptive and follow the standard naming conventions.

    #[builder(default = "2.0.to_string().into()", setter(into))]
    #[derivative(Default(value = "\"2.0\".to_string()"))]
    pub name: String,

    /// number
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    #[derivative(Default)]
    pub number: Option<TestNumberType>,

    /// test2
    #[serde(skip_serializing_if = "Vec::is_empty")]
    #[builder(default, setter(into, each(name = "to_test2")))]
    #[derivative(Default)]
    pub test2: Vec<Test2>,

    /// ontology
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    #[derivative(Default)]
    pub ontology: Option<Ontology>,

    /// Additional properties outside of the schema
    #[serde(flatten)]
    #[builder(default)]
    pub additional_properties: Option<HashMap<String, Value>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Builder, Derivative)]
#[derivative(Default)]
#[serde(default)]
#[allow(non_snake_case)]
pub struct Test2 {
    /// JSON-LD header
    #[serde(flatten)]
    #[builder(default = "default_test2_jsonld_header()")]
    #[derivative(Default(value = "default_test2_jsonld_header()"))]
    pub jsonld: Option<JsonLdHeader>,

    /// names
    #[serde(skip_serializing_if = "Vec::is_empty")]
    #[builder(default, setter(into, each(name = "to_names")))]
    #[derivative(Default)]
    pub names: Vec<String>,

    /// number
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    #[derivative(Default)]
    pub number: Option<f64>,

    /// Additional properties outside of the schema
    #[serde(flatten)]
    #[builder(default)]
    pub additional_properties: Option<HashMap<String, Value>>,
}

//
// Enum definitions
//
#[allow(non_camel_case_types)]
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Default, PartialEq, Eq)]
pub enum Ontology {
    #[default]
    #[serde(rename = "https://www.evidenceontology.org/term/")]
    Eco,

    #[serde(rename = "https://amigo.geneontology.org/amigo/term/")]
    Go,

    #[serde(rename = "http://semanticscience.org/resource/")]
    Sio,
}

/// Union type for Test.number
#[allow(non_camel_case_types)]
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub enum TestNumberType {
    F64(f64),
    String(String),
}
// Default JSON-LD header function for each object

pub fn default_test_jsonld_header() -> Option<JsonLdHeader> {
    let mut context = SimpleContext::default();

    // Add main prefix and repository URL
    context.terms.insert("tst".to_string(), TermDef::Simple("https://www.github.com/my/repo/".to_string()));

    // Add configured prefixes
    context.terms.insert("schema".to_string(), TermDef::Simple("http://schema.org/".to_string()));

    // Add class-scoped prefixes
    context.terms.insert("Test".to_string(), TermDef::Simple("https://www.github.com/my/repo/Test/".to_string()));
    context.terms.insert("Test2".to_string(), TermDef::Simple("https://www.github.com/my/repo/Test2/".to_string()));
    context.terms.insert("Ontology".to_string(), TermDef::Simple("https://www.github.com/my/repo/Ontology/".to_string()));

    // Add attribute terms
    context.terms.insert("name".to_string(), TermDef::Detailed(TermDetail {
        id: Some("schema:hello".to_string()),
        type_: Some("@id".to_string()),
        container: None,
        context: None,
    }));
    context.terms.insert("number".to_string(), TermDef::Simple("schema:one".to_string()));
    context.terms.insert("test2".to_string(), TermDef::Detailed(TermDetail {
        id: Some("schema:something".to_string()),
        type_: None,
        container: Some("@list".to_string()),
        context: None,
    }));

    Some(JsonLdHeader {
        import: Vec::new(),
        context: Some(JsonLdContext::Object(context)),
        id: Some(format!("tst:Test/{}", uuid::Uuid::new_v4())),
        type_: Some(TypeOrVec::Multi(vec![
            "tst:Test".to_string(),
        ])),
    })
}


pub fn default_test2_jsonld_header() -> Option<JsonLdHeader> {
    let mut context = SimpleContext::default();

    // Add main prefix and repository URL
    context.terms.insert("tst".to_string(), TermDef::Simple("https://www.github.com/my/repo/".to_string()));

    // Add configured prefixes
    context.terms.insert("schema".to_string(), TermDef::Simple("http://schema.org/".to_string()));

    // Add class-scoped prefixes
    context.terms.insert("Test".to_string(), TermDef::Simple("https://www.github.com/my/repo/Test/".to_string()));
    context.terms.insert("Test2".to_string(), TermDef::Simple("https://www.github.com/my/repo/Test2/".to_string()));
    context.terms.insert("Ontology".to_string(), TermDef::Simple("https://www.github.com/my/repo/Ontology/".to_string()));

    // Add attribute terms
    context.terms.insert("names".to_string(), TermDef::Detailed(TermDetail {
        id: Some("schema:hello".to_string()),
        type_: None,
        container: Some("@list".to_string()),
        context: None,
    }));
    context.terms.insert("number".to_string(), TermDef::Simple("schema:one".to_string()));

    Some(JsonLdHeader {
        import: Vec::new(),
        context: Some(JsonLdContext::Object(context)),
        id: Some(format!("tst:Test2/{}", uuid::Uuid::new_v4())),
        type_: Some(TypeOrVec::Multi(vec![
            "tst:Test2".to_string(),
        ])),
    })
}


/// JSON-LD Header
///
/// JSON-LD (JavaScript Object Notation for Linked Data) provides a way to express
/// linked data using JSON syntax, enabling semantic web technologies and structured
/// data interchange with context and meaning preservation.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Eq, PartialEq)]
pub struct JsonLdHeader {
    /// JSON-LD context (IRI, object, or array)
    #[serde(rename = "@context", skip_serializing_if = "Option::is_none")]
    pub context: Option<JsonLdContext>,

    /// Node identifier (IRI or blank node)
    #[serde(rename = "@id", skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,

    // Import context from a file
    #[serde(rename = "@import", skip_serializing_if = "Vec::is_empty")]
    pub import: Vec<String>,

    /// Type IRI(s) for the node, e.g. schema:Person
    #[serde(rename = "@type", skip_serializing_if = "Option::is_none")]
    pub type_: Option<TypeOrVec>,
}

impl Default for JsonLdHeader {
    /// Returns the default JSON-LD header.
    fn default() -> Self {
        Self {
            context: None,
            id: None,
            import: Vec::new(),
            type_: None,
        }
    }
}

impl JsonLdHeader {
    /// Adds a new term definition to the JSON-LD context, creating a context object if none exists.
    ///
    /// This method provides a convenient way to extend the JSON-LD context with additional term
    /// mappings, allowing for semantic annotation of properties and values within the document.
    /// If the header does not already contain a context, a new SimpleContext object will be
    /// created automatically to hold the term definition.
    ///
    /// # Arguments
    ///
    /// * `name` - The term name to be defined in the context
    /// * `term` - The term definition, either a simple IRI mapping or a detailed definition
    ///
    /// # Example
    ///
    /// ```no_compile
    /// let mut header = JsonLdHeader::default();
    /// header.add_term("name", TermDef::Simple("https://schema.org/name".to_string()));
    /// ```
    pub fn add_term(&mut self, name: &str, term: TermDef) {
        let context = self
            .context
            .get_or_insert_with(|| JsonLdContext::Object(SimpleContext::default()));

        if let JsonLdContext::Object(object) = context {
            object.terms.insert(name.to_string(), term);
        }
    }

    /// Updates an existing term definition in the JSON-LD context or adds it if it doesn't exist.
    ///
    /// This method functions similarly to add_term but provides clearer semantics when the
    /// intention is to modify an existing term definition. The behavior is identical to add_term
    /// as HashMap::insert will overwrite existing entries with the same key, but this method
    /// name makes the intent more explicit in code that is updating rather than initially
    /// defining terms.
    ///
    /// # Arguments
    ///
    /// * `name` - The term name to be updated in the context
    /// * `term` - The new term definition to replace any existing definition
    ///
    /// # Example
    ///
    /// ```no_compile
    /// let mut header = JsonLdHeader::default();
    /// header.add_term("name", TermDef::Simple("https://schema.org/name".to_string()));
    /// header.update_term("name", TermDef::Simple("https://example.org/fullName".to_string()));
    /// ```
    pub fn update_term(&mut self, name: &str, term: TermDef) {
        let context = self
            .context
            .get_or_insert_with(|| JsonLdContext::Object(SimpleContext::default()));

        if let JsonLdContext::Object(object) = context {
            object.terms.insert(name.to_string(), term);
        }
    }

    /// Removes a term definition from the JSON-LD context if it exists.
    ///
    /// This method allows for the removal of previously defined terms from the JSON-LD context,
    /// which can be useful when dynamically managing context definitions or when certain terms
    /// are no longer needed in the semantic annotation of the document. The method will only
    /// attempt removal if the context exists and is an object type; it will silently do nothing
    /// if the context is missing or is not an object.
    ///
    /// # Arguments
    ///
    /// * `name` - The term name to be removed from the context
    ///
    /// # Returns
    ///
    /// Returns `true` if the term was found and removed, `false` if the term was not present
    /// or if the context is not an object type.
    ///
    /// # Example
    ///
    /// ```no_compile
    /// let mut header = JsonLdHeader::default();
    /// header.add_term("name", TermDef::Simple("https://schema.org/name".to_string()));
    /// let was_removed = header.remove_term("name");
    /// assert!(was_removed);
    /// ```
    pub fn remove_term(&mut self, name: &str) -> bool {
        if let Some(JsonLdContext::Object(object)) = &mut self.context {
            object.terms.remove(name).is_some()
        } else {
            false
        }
    }

    /// Adds a new import to the JSON-LD context.
    ///
    /// This method allows for the addition of additional context files to be imported into the
    /// JSON-LD context, which can be useful when working with complex or large context definitions.
    /// The method will simply append the new import to the existing list of imports, allowing for
    /// flexible and dynamic management of context dependencies within the document.
    ///
    /// # Arguments
    /// * `import` - The IRI of the context file to be imported
    pub fn add_import(&mut self, import: String) {
        self.import.push(import);
    }
}

/// Accept either a single type IRI or an array of them.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Eq, PartialEq)]
#[serde(untagged)]
pub enum TypeOrVec {
    Single(String),
    Multi(Vec<String>),
}

/// JSON-LD Context:
/// - a single IRI (remote context)
/// - an inline context object
/// - or an array of these (merged sequentially)
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Eq, PartialEq)]
#[serde(untagged)]
pub enum JsonLdContext {
    Iri(String),
    Object(SimpleContext),
    Array(Vec<JsonLdContext>),
}

/// A simple inline @context object with essential global keys and term definitions.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Default, Eq, PartialEq)]
pub struct SimpleContext {
    /// Base IRI used for relative resolution.
    #[serde(rename = "@base", skip_serializing_if = "Option::is_none")]
    pub base: Option<String>,

    /// Default vocabulary IRI for terms without explicit IRIs.
    #[serde(rename = "@vocab", skip_serializing_if = "Option::is_none")]
    pub vocab: Option<String>,

    /// Default language for string literals.
    #[serde(rename = "@language", skip_serializing_if = "Option::is_none")]
    pub language: Option<String>,

    /// Mapping of term → IRI or detailed definition.
    #[serde(flatten, skip_serializing_if = "HashMap::is_empty", default)]
    pub terms: HashMap<String, TermDef>,
}

/// Term definition can be a simple mapping (string → IRI) or a detailed object.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Eq, PartialEq)]
#[serde(untagged)]
pub enum TermDef {
    /// Simple alias: `"name": "https://schema.org/name"`
    Simple(String),
    /// Expanded form with type coercion, container behavior, or nested context.
    Detailed(TermDetail),
}

/// Detailed term definition (subset of JSON-LD 1.1 features).
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Default, Eq, PartialEq)]
pub struct TermDetail {
    /// Absolute or relative IRI that the term expands to, or a keyword like "@id".
    #[serde(rename = "@id", skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,

    /// Type coercion or value type ("@id", "@vocab", or datatype IRI).
    #[serde(rename = "@type", skip_serializing_if = "Option::is_none")]
    pub type_: Option<String>,

    /// Container behavior ("@list", "@set", "@index", etc.).
    #[serde(rename = "@container", skip_serializing_if = "Option::is_none")]
    pub container: Option<String>,

    /// Optional nested (scoped) context.
    #[serde(rename = "@context", skip_serializing_if = "Option::is_none")]
    pub context: Option<Box<JsonLdContext>>,
}