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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
/*
 * Copyright (c) 2025 Jan Range
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

use std::{collections::HashMap, error::Error, fmt::Display, str::FromStr};

use crate::{
    attribute::{Attribute, DataType},
    markdown::frontmatter::FrontMatter,
    object::Object,
    option::AttrOption,
    prelude::DataModel,
    tree,
    xmltype::XMLType,
};
use clap::ValueEnum;
use colored::Colorize;
use convert_case::{Case, Casing};
use lazy_static::lazy_static;
use minijinja::{
    context,
    value::{Kwargs, ValueKind, ViaDeserialize},
    Environment, Value,
};
use textwrap::wrap;

#[cfg(feature = "python")]
use pyo3::pyclass;

#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::wasm_bindgen;

lazy_static! {
    /// Maps generic type names to Python-specific type names.
    static ref PYTHON_TYPE_MAPS: std::collections::HashMap<String, String> = {
        let mut m = std::collections::HashMap::new();
        m.insert("string".to_string(), "str".to_string());
        m.insert("integer".to_string(), "int".to_string());
        m.insert("boolean".to_string(), "bool".to_string());
        m.insert("number".to_string(), "float".to_string());
        m
    };

    /// Maps Python-specific type names to XSD-specific type names.
    static ref XSD_TYPE_MAPS: std::collections::HashMap<String, String> = {
        let mut m = std::collections::HashMap::new();
        m.insert("str".to_string(), "string".to_string());
        m.insert("bytes".to_string(), "base64Binary".to_string());
        m
    };

    /// Maps MD-Models type names to Typescript-specific type names.
    static ref TYPESCRIPT_TYPE_MAPS: std::collections::HashMap<String, String> = {
        let mut m = std::collections::HashMap::new();
        m.insert("integer".to_string(), "number".to_string());
        m.insert("float".to_string(), "number".to_string());
        m.insert("date".to_string(), "string".to_string());
        m.insert("bytes".to_string(), "string".to_string());
        m
    };

    /// Maps MD-Models type names to GraphQL-specific type names.
    static ref GRAPHQL_TYPE_MAPS: std::collections::HashMap<String, String> = {
        let mut m = std::collections::HashMap::new();
        m.insert("integer".to_string(), "Int".to_string());
        m.insert("number".to_string(), "Float".to_string());
        m.insert("float".to_string(), "Float".to_string());
        m.insert("boolean".to_string(), "Boolean".to_string());
        m.insert("string".to_string(), "String".to_string());
        m.insert("bytes".to_string(), "String".to_string());
        m.insert("date".to_string(), "String".to_string());
        m
    };

    /// Maps MD-Models type names to Owl-specific type names.
    static ref OWL_TYPE_MAPS: std::collections::HashMap<String, String> = {
        let mut m = std::collections::HashMap::new();
        m.insert("integer".to_string(), "xsd:integer".to_string());
        m.insert("number".to_string(), "xsd:decimal".to_string());
        m.insert("float".to_string(), "xsd:decimal".to_string());
        m.insert("boolean".to_string(), "xsd:boolean".to_string());
        m.insert("string".to_string(), "xsd:string".to_string());
        m.insert("bytes".to_string(), "xsd:base64Binary".to_string());
        m.insert("date".to_string(), "xsd:date".to_string());
        m
    };

    /// Forbidden enum variants for Rust (mainly for windows compatibility)
    static ref FORBIDDEN_RUST_ENUM_VARIANTS: Vec<String> = {
        vec![
            "yield".to_string(),
        ]
    };
}

/// Enumeration of available templates.
#[derive(Debug, ValueEnum, Clone, PartialEq)]
#[cfg_attr(feature = "python", pyclass(eq, eq_int, from_py_object))]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub enum Templates {
    /// XML Schema
    XmlSchema,
    /// Markdown
    Markdown,
    /// Compact Markdown
    CompactMarkdown,
    /// JSON Schema
    JsonSchema,
    /// JSON Schema All
    JsonSchemaAll,
    /// JSON-LD
    JsonLd,
    /// SHACL
    Shacl,
    /// OWL
    Owl,
    /// ShEx
    Shex,
    /// Python Dataclass
    PythonDataclass,
    /// Python Pydantic XML
    PythonPydanticXML,
    /// Python Pydantic
    PythonPydantic,
    /// MkDocs
    MkDocs,
    /// Internal
    Internal,
    /// Typescript (io-ts)
    Typescript,
    /// Typescript (Zod)
    TypescriptZod,
    /// Rust
    Rust,
    /// Protobuf
    Protobuf,
    /// Graphql
    Graphql,
    /// Golang
    Golang,
    /// Linkml
    Linkml,
    /// Julia
    Julia,
    /// Mermaid class diagram
    Mermaid,
}

impl Display for Templates {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Templates::PythonDataclass => write!(f, "python-dataclass"),
            Templates::PythonPydantic => write!(f, "python-pydantic"),
            Templates::PythonPydanticXML => write!(f, "python-pydantic-xml"),
            Templates::XmlSchema => write!(f, "xml-schema"),
            Templates::Markdown => write!(f, "markdown"),
            Templates::CompactMarkdown => write!(f, "compact-markdown"),
            Templates::Shacl => write!(f, "shacl"),
            Templates::JsonSchema => write!(f, "json-schema"),
            Templates::JsonSchemaAll => write!(f, "json-schema-all"),
            Templates::JsonLd => write!(f, "json-ld"),
            Templates::Shex => write!(f, "shex"),
            Templates::MkDocs => write!(f, "mk-docs"),
            Templates::Internal => write!(f, "internal"),
            Templates::Typescript => write!(f, "typescript"),
            Templates::TypescriptZod => write!(f, "typescript-zod"),
            Templates::Rust => write!(f, "rust"),
            Templates::Protobuf => write!(f, "protobuf"),
            Templates::Graphql => write!(f, "graphql"),
            Templates::Golang => write!(f, "golang"),
            Templates::Linkml => write!(f, "linkml"),
            Templates::Julia => write!(f, "julia"),
            Templates::Mermaid => write!(f, "mermaid"),
            Templates::Owl => write!(f, "owl"),
        }
    }
}

/// Converts string representation of a template to a `Templates` enum.
/// and returns an error if the string is not a valid template type.
impl FromStr for Templates {
    type Err = Box<dyn Error>;
    fn from_str(s: &str) -> Result<Self, Box<dyn Error>> {
        match s {
            "python-dataclass" => Ok(Templates::PythonDataclass),
            "python-sdrdm" => Ok(Templates::PythonPydanticXML),
            "python-pydantic" => Ok(Templates::PythonPydantic),
            "python-pydantic-xml" => Ok(Templates::PythonPydanticXML),
            "xml-schema" => Ok(Templates::XmlSchema),
            "markdown" => Ok(Templates::Markdown),
            "compact-markdown" => Ok(Templates::CompactMarkdown),
            "shacl" => Ok(Templates::Shacl),
            "json-schema" => Ok(Templates::JsonSchema),
            "json-schema-all" => Ok(Templates::JsonSchemaAll),
            "shex" => Ok(Templates::Shex),
            "mk-docs" => Ok(Templates::MkDocs),
            "internal" => Ok(Templates::Internal),
            "typescript" => Ok(Templates::Typescript),
            "typescript-zod" => Ok(Templates::TypescriptZod),
            "rust" => Ok(Templates::Rust),
            "protobuf" => Ok(Templates::Protobuf),
            "graphql" => Ok(Templates::Graphql),
            "golang" => Ok(Templates::Golang),
            "linkml" => Ok(Templates::Linkml),
            "julia" => Ok(Templates::Julia),
            "mermaid" => Ok(Templates::Mermaid),
            "owl" => Ok(Templates::Owl),
            _ => {
                let err = format!("Invalid template type: {s}");
                Err(err.into())
            }
        }
    }
}

/// Renders a Jinja template based on the provided template type and data model.
///
/// # Arguments
///
/// * `template` - The type of template to render.
/// * `model` - The data model to use for rendering the template.
///
/// # Returns
///
/// A Result containing the rendered template as a String or an error if rendering fails.
pub fn render_jinja_template(
    template: &Templates,
    model: &mut DataModel,
    config: Option<&HashMap<String, String>>,
) -> Result<String, minijinja::Error> {
    // Load the template environment
    let mut env = Environment::new();
    minijinja_embed::load_templates!(&mut env);

    // Keep track of fields that are artificially added,
    // but not part of the original model. Mainly used for
    // Database migrations and objects which do not have
    // a primary key.
    let mut artificial_fields = HashMap::new();

    // If there is no config, create an empty one
    // This is necessary to avoid errors when rendering the template
    let config = config.cloned().unwrap_or_default();

    // Perform type conversions and filtering based on the template
    match template {
        Templates::XmlSchema => convert_model_types(model, &XSD_TYPE_MAPS),
        Templates::Typescript => convert_model_types(model, &TYPESCRIPT_TYPE_MAPS),
        Templates::Graphql => convert_model_types(model, &GRAPHQL_TYPE_MAPS),
        Templates::Shacl | Templates::Shex => {
            convert_model_types(model, &OWL_TYPE_MAPS);
            if let Err(e) = filter_objects_wo_terms(model) {
                println!(
                    " [{}] {}",
                    template.to_string().yellow().bold(),
                    e.to_string().bold(),
                );
            }
        }
        Templates::Owl => {
            convert_model_types(model, &OWL_TYPE_MAPS);
            remove_default_prefixes(model);
        }
        Templates::PythonDataclass | Templates::PythonPydanticXML | Templates::PythonPydantic => {
            convert_astropy_types(model, &config);
            convert_model_types(model, &PYTHON_TYPE_MAPS);
            sort_attributes_by_required(model);
        }
        Templates::Julia => {
            sort_by_dependency(model);
        }
        Templates::Golang => {
            if config.contains_key("gorm") {
                add_id_pks(model, &mut artificial_fields);
            }
        }
        Templates::Rust => {
            check_for_forbidden_rust_enum_variants(model);
        }
        _ => {}
    }

    // Add custom functions to the Jinja environment
    env.add_function("wrap", wrap_text);
    env.add_function("replace", replace);
    env.add_function("trim", trim);
    env.add_function("default_value", default_value);
    env.add_filter("enumerate", enumerate);
    env.add_filter("cap_first", cap_first);
    env.add_filter("split_path_pairs", split_path_pairs);
    env.add_filter("pascal_case", pascal_case);
    env.add_filter("camel_case", camel_case);
    env.add_filter("snake_case", snake_case);
    env.add_filter("replace_lower", replace_lower);

    // Get the appropriate template
    let template = match template {
        Templates::PythonDataclass => env.get_template("python-dataclass.jinja")?,
        Templates::PythonPydantic => env.get_template("python-pydantic.jinja")?,
        Templates::XmlSchema => env.get_template("xml-schema.jinja")?,
        Templates::Markdown => env.get_template("markdown.jinja")?,
        Templates::CompactMarkdown => env.get_template("markdown-compact.jinja")?,
        Templates::Shacl => env.get_template("shacl.jinja")?,
        Templates::Shex => env.get_template("shex.jinja")?,
        Templates::PythonPydanticXML => env.get_template("python-pydantic-xml.jinja")?,
        Templates::MkDocs => env.get_template("mkdocs.jinja")?,
        Templates::Typescript => env.get_template("typescript.jinja")?,
        Templates::TypescriptZod => env.get_template("typescript-zod.jinja")?,
        Templates::Rust => env.get_template("rust.jinja")?,
        Templates::Protobuf => env.get_template("protobuf.jinja")?,
        Templates::Graphql => env.get_template("graphql.jinja")?,
        Templates::Golang => env.get_template("golang.jinja")?,
        Templates::Julia => env.get_template("julia.jinja")?,
        Templates::Mermaid => env.get_template("mermaid.jinja")?,
        Templates::Owl => env.get_template("owl.jinja")?,
        _ => {
            panic!(
                "The template is not available as a Jinja Template and should not be used using the jinja exporter.
                Instead, use the dedicated exporter in the DataModel struct (e.g. `DataModel::json_ld_header`, DataModel::json_schema)."
            )
        }
    };

    // If there is no config, create an empty one
    // This is necessary to avoid errors when rendering the template
    if model.config.is_none() {
        model.config = Some(FrontMatter::default());
    }

    // Render the template
    let prefixes = get_prefixes(model);
    let rendered = template.render(context! {
        objects => model.objects,
        object_names => model.objects.iter().map(|o| o.name.clone()).collect::<Vec<String>>(),
        enums => model.enums,
        enum_names => model.enums.iter().map(|e| e.name.clone()).collect::<Vec<String>>(),
        title => model.name,
        prefixes => prefixes,
        repo => model.config.as_ref().unwrap().repo.clone(),
        prefix => model.config.as_ref().unwrap().prefix.clone(),
        nsmap => model.config.as_ref().unwrap().nsmap.clone(),
        config => config,
        objects_with_wrapped => get_objects_with_wrapped(model),
        pk_objects => pk_objects(model),
        artificial_fields => artificial_fields,
        has_union_types => has_union_types(model),
    });

    match rendered {
        Ok(r) => Ok(clean_and_trim(&r)),
        Err(e) => Err(e),
    }
}

/// Returns a vector of object names that have attributes with XML wrapped types.
///
/// # Arguments
///
/// * `model` - The data model to search for wrapped objects
///
/// # Returns
///
/// A vector of strings containing the names of objects that have attributes with XML wrapped types
fn get_objects_with_wrapped(model: &mut DataModel) -> Vec<String> {
    model
        .objects
        .iter()
        .filter(|o| {
            o.attributes.iter().any(|a| {
                if let Some(xml) = &a.xml {
                    matches!(xml, XMLType::Wrapped { .. })
                } else {
                    false
                }
            })
        })
        .map(|o| o.name.clone())
        .collect()
}

/// Replaces all occurrences of a substring with another substring.
///
/// # Arguments
///
/// * `value` - The string to perform replacements on
/// * `from` - The substring to replace
/// * `to` - The substring to replace with
///
/// # Returns
///
/// A new string with all occurrences of `from` replaced with `to`
fn replace(value: String, from: &str, to: &str) -> String {
    value.replace(from, to)
}

/// Replaces all occurrences of a substring with another substring and converts the result to lowercase.
///
/// # Arguments
///
/// * `value` - The string to perform replacements on
/// * `from` - The substring to replace
/// * `to` - The substring to replace with
///
/// # Returns
///
/// A new string with all occurrences of `from` replaced with `to` and converted to lowercase
fn replace_lower(value: String, from: String, to: String) -> String {
    value.replace(&from, &to).to_lowercase()
}

/// Template function that allows to wrap text at a certain length.
///
/// # Arguments
///
/// * `text` - The text to wrap.
/// * `width` - The maximum length of a line.
/// * `offset` - The offset to use for all lines.
///
/// # Returns
///
/// A string with the wrapped text.
fn wrap_text(
    text: &str,
    width: usize,
    initial_offset: &str,
    offset: &str,
    delimiter: Option<&str>,
) -> String {
    let delimiter = delimiter.unwrap_or("");
    // Remove multiple spaces
    let options = textwrap::Options::new(width)
        .initial_indent(initial_offset)
        .subsequent_indent(offset)
        .width(width)
        .break_words(false);

    wrap(remove_multiple_spaces(text).as_str(), options).join(&format!("{delimiter}\n"))
}

/// Splits a path into pairs of (current, previous) components.
///
/// # Arguments
///
/// * `path` - The path to split, using '/' as separator
/// * `initial` - The initial previous value to use for the first component
///
/// # Returns
///
/// A vector of tuples containing (current_component, previous_component)
fn split_path_pairs(path: String, initial: Option<String>) -> Vec<Vec<String>> {
    let initial = initial.unwrap_or_default();
    let parts: Vec<&str> = path.split('/').collect();
    let mut pairs = Vec::new();
    let mut prev = initial;

    for part in parts {
        if !part.is_empty() {
            pairs.push(vec![part.to_string(), prev.clone()]);
            prev = part.to_string();
        }
    }

    pairs
}

/// Filter use only for Jinja templates.
/// Converts a string to PascalCase.
fn pascal_case(s: String) -> String {
    if s.ends_with("_") {
        s.to_case(Case::Pascal) + "_"
    } else {
        s.to_case(Case::Pascal)
    }
}

/// Filter use only for Jinja templates.
/// Converts a string to camelCase.
fn camel_case(s: String) -> String {
    s.to_case(Case::Camel)
}

/// Filter use only for Jinja templates.
/// Converts a string to snake_case.
fn snake_case(s: String) -> String {
    s.to_case(Case::Snake)
}

/// Removes leading and trailing whitespace and multiple spaces from a string.
fn remove_multiple_spaces(input: &str) -> String {
    input.split_whitespace().collect::<Vec<&str>>().join(" ")
}

/// Removes trailing underscores from a string.
fn trim(input: &str, prefix: &str) -> String {
    input
        .trim_start_matches(prefix)
        .trim_end_matches(prefix)
        .to_string()
}

/// Checks if an object has a primary key.
fn pk_objects(model: &mut DataModel) -> HashMap<String, (String, String, bool)> {
    let mut pk_objects = HashMap::new();
    for object in &mut model.objects {
        for attribute in &object.attributes {
            for option in &attribute.options {
                if let AttrOption::PrimaryKey(true) = option {
                    pk_objects.insert(
                        object.name.clone(),
                        (attribute.name.clone(), attribute.dtypes[0].clone(), true),
                    );
                    break;
                }
            }
        }
    }
    pk_objects
}

/// Converts the data types in the model according to the provided type map.
///
/// # Arguments
///
/// * `model` - The data model whose types are to be converted.
/// * `type_map` - A map of generic type names to specific type names.
fn convert_model_types(
    model: &mut DataModel,
    type_map: &std::collections::HashMap<String, String>,
) {
    for object in &mut model.objects {
        for attribute in &mut object.attributes {
            attribute.dtypes = attribute
                .dtypes
                .iter()
                .map(|t| type_map.get(t).unwrap_or(t))
                .map(|t| t.to_string())
                .collect();
        }
    }
}

/// Adds an ID field to objects in the model that don't have a primary key.
///
/// This function ensures that each object has a primary key by either:
/// 1. Adding a new 'id' attribute if the object has no primary key and no 'id' field
/// 2. Making an existing 'id' field a primary key if one exists but isn't already a primary key
///
/// # Arguments
///
/// * `model` - The data model to modify
fn add_id_pks(model: &mut DataModel, artificially_added_fields: &mut HashMap<String, String>) {
    for object in &mut model.objects {
        match (has_primary_key(object), has_id(object)) {
            (false, false) => {
                object.attributes.insert(0, id_attribute());
                artificially_added_fields.insert(object.name.clone(), "id".to_string());
            }
            (false, true) => {
                object
                    .attributes
                    .iter_mut()
                    .find(|a| a.name == "id")
                    .unwrap()
                    .options
                    .push(AttrOption::PrimaryKey(true));
            }
            _ => {}
        }
    }
}

/// Creates a new ID attribute with primary key option.
///
/// This function creates a new Attribute instance representing an ID field
/// with the following properties:
/// - Name: "id"
/// - Required: false
/// - Type: integer
/// - Primary key option enabled
///
/// # Returns
///
/// A new Attribute configured as an ID field
fn id_attribute() -> Attribute {
    let mut attr = Attribute::new("id".to_string(), true);
    attr.options.push(AttrOption::PrimaryKey(true));
    attr.set_dtype("integer".to_string()).unwrap();
    attr
}

/// Checks if an object has any attribute marked as a primary key.
///
/// # Arguments
///
/// * `object` - The object to check for primary key attributes
///
/// # Returns
///
/// `true` if the object has an attribute with primary key option, `false` otherwise
fn has_primary_key(object: &Object) -> bool {
    object
        .attributes
        .iter()
        .any(|a| a.options.iter().any(|o| o.key() == "primary key"))
}

/// Checks if an object has an attribute named "id".
///
/// # Arguments
///
/// * `object` - The object to check for an ID attribute
///
/// # Returns
///
/// `true` if the object has an attribute named "id", `false` otherwise
fn has_id(object: &Object) -> bool {
    object.attributes.iter().any(|a| a.name == "id")
}

/// Converts the data types in the model according to the provided type map.
///
/// # Arguments
///
/// * `model` - The data model whose types are to be converted.
fn convert_astropy_types(model: &mut DataModel, config: &HashMap<String, String>) {
    if !config.contains_key("astropy") {
        return;
    }

    // Replace UnitDefinition with UnitDefinitionAnnot
    for object in &mut model.objects {
        for attribute in &mut object.attributes {
            if attribute.dtypes.contains(&"UnitDefinition".to_string()) {
                attribute.dtypes = vec!["UnitDefinitionAnnot".to_string()];
            }
        }
    }

    model
        .objects
        .retain(|o| o.name != "UnitDefinition" && o.name != "BaseUnit");

    model.enums.retain(|e| e.name != "UnitType");
}

/// Retrieves the prefixes from the model configuration.
///
/// # Arguments
///
/// * `model` - The data model from which to retrieve the prefixes.
///
/// # Returns
///
/// A vector of prefix tuples (prefix, URI).
fn get_prefixes(model: &mut DataModel) -> Vec<(String, String)> {
    let mut prefixes = match &model.config {
        Some(config) => config.prefixes().unwrap_or(vec![]),
        None => vec![],
    };

    prefixes.sort_by(|a, b| a.0.cmp(&b.0));
    prefixes
}

/// Filters out objects from the model that do not have any terms.
///
/// # Arguments
///
/// * `model` - The data model to filter.
fn filter_objects_wo_terms(model: &mut DataModel) -> Result<(), Box<dyn Error>> {
    model.objects.retain(|o| o.has_any_terms());

    if model.objects.is_empty() {
        return Err(Box::new(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "No objects with terms found in the model. Unable to build SHACL or ShEx.",
        )));
    }
    Ok(())
}

/// Checks for forbidden Rust enum variants and replaces them with a valid variant.
///
/// # Arguments
///
/// * `model` - The data model to check for forbidden Rust enum variants.
fn check_for_forbidden_rust_enum_variants(model: &mut DataModel) {
    for enumeration in &mut model.enums {
        enumeration.mappings = enumeration
            .mappings
            .iter()
            .map(|(key, value)| {
                let new_key = if FORBIDDEN_RUST_ENUM_VARIANTS.contains(&key.to_lowercase()) {
                    format!("{key}_")
                } else {
                    key.to_lowercase()
                };
                (new_key, value.clone())
            })
            .collect();
    }
}

/// Sorts the objects in the model by their dependency.
///
/// This is important for languages like Julia, where forward declarations
/// are not supported.
///
/// # Arguments
///
/// * `model` - The data model whose objects are to be sorted.
fn sort_by_dependency(model: &mut DataModel) {
    let graph = tree::dependency_graph(model);
    let mut class_order = tree::get_topological_order(&graph);
    class_order.reverse();
    model
        .objects
        .sort_by_key(|o| class_order.iter().position(|c| c == &o.name).unwrap());
}

/// Sorts the attributes of each object in the model by their 'required' field.
///
/// # Arguments
///
/// * `model` - The data model whose attributes are to be sorted.
fn sort_attributes_by_required(model: &mut DataModel) {
    for object in &mut model.objects {
        object.sort_attrs_by_required();
    }
}

/// Cleans and trims a string by removing trailing whitespace and limiting consecutive empty lines.
///
/// This function processes a string by:
/// 1. Splitting it into lines
/// 2. Trimming trailing whitespace from each line
/// 3. Limiting consecutive empty lines to a maximum of 2
/// 4. Joining the lines back together
///
/// # Arguments
///
/// * `s` - The string to clean and trim
///
/// # Returns
///
/// A cleaned string with trailing whitespace removed and consecutive empty lines limited
fn clean_and_trim(s: &str) -> String {
    let splitted = s.split('\n').collect::<Vec<&str>>();
    let mut cleaned = vec![];
    let mut consec_empty = 0;

    for line in splitted {
        let trimmed = line.trim_end();
        if !trimmed.is_empty() {
            cleaned.push(trimmed);
            consec_empty = 0;
        } else {
            consec_empty += 1;
            if consec_empty < 3 {
                cleaned.push(trimmed);
            }
        }
    }

    cleaned.join("\n").trim().to_string()
}

// Enumerate a collection of objects
pub fn enumerate(v: &Value, _: Kwargs) -> Result<Value, minijinja::Error> {
    if v.kind() != ValueKind::Seq {
        return Err(minijinja::Error::new(
            minijinja::ErrorKind::InvalidOperation,
            "Can only enumerate sequences",
        ));
    }

    // Turn into iterator of (index, value)
    Ok(v.try_iter()
        .expect("Failed to iterate over sequence")
        .enumerate()
        .map(|(i, v)| Value::from(vec![Value::from(i), v]))
        .collect())
}

/// Capitalizes the first character of a string.
///
/// # Arguments
///
/// * `s` - The string to capitalize
///
/// # Returns
///
/// A new string with the first character capitalized
fn cap_first(s: String) -> String {
    let mut chars = s.chars();
    match chars.next() {
        Some(c) => c.to_uppercase().collect::<String>() + chars.as_str(),
        None => s.to_string(),
    }
}

/// Formats the default value of an attribute.
///
/// # Arguments
///
/// * `default` - The default value of an attribute.
///
/// # Returns
fn default_value(attribute: ViaDeserialize<Attribute>) -> String {
    match &attribute.default {
        Some(DataType::String(s)) => format!("\"{s}\""),
        Some(DataType::Integer(i)) => {
            if contains_numeric_type(&attribute) {
                i.to_string()
            } else {
                format!("\"{i}\"")
            }
        }
        Some(DataType::Float(f)) => {
            if contains_numeric_type(&attribute) {
                f.to_string()
            } else {
                format!("\"{f}\"")
            }
        }
        _ => "".to_string(),
    }
}

/// Checks if an attribute contains a numeric type.
///
/// # Arguments
///
/// * `attribute` - The attribute to check.
///
/// # Returns
///
/// `true` if the attribute contains a numeric type, `false` otherwise.
fn contains_numeric_type(attribute: &Attribute) -> bool {
    attribute
        .dtypes
        .iter()
        .any(|t| t == "integer" || t == "float")
}

/// Checks if an object has multiple types.
///
/// # Arguments
///
/// * `object` - The object to check.
///
/// # Returns
///
/// `true` if the object has union types, `false` otherwise.
fn has_union_types(model: &mut DataModel) -> bool {
    model
        .objects
        .iter()
        .any(|o| o.attributes.iter().any(|a| a.dtypes.len() > 1))
}

/// Removes the default prefixes from the model.
///
/// # Arguments
///
/// * `model` - The data model whose prefixes are to be removed.
fn remove_default_prefixes(model: &mut DataModel) {
    if let Some(prefixes) = &mut model.config.as_mut().unwrap().prefixes {
        prefixes.remove("xsd");
        prefixes.remove("rdfs");
        prefixes.remove("owl");
    }
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;
    use std::fs;

    use crate::markdown::parser::parse_markdown;

    use super::*;

    /// Helper function to build and convert a template.
    ///
    /// # Arguments
    ///
    /// * `template` - The template type to use for rendering.
    ///
    /// # Returns
    ///
    /// A string containing the rendered template.
    fn build_and_convert(
        path: &str,
        template: Templates,
        config: Option<&HashMap<String, String>>,
    ) -> String {
        let content = fs::read_to_string(path).expect("Could not read markdown file");
        let mut model = parse_markdown(&content, None).expect("Failed to parse markdown file");
        render_jinja_template(&template, &mut model, config)
            .expect("Could not render template")
            .to_string()
    }

    #[test]
    fn test_convert_to_shex() {
        // Arrange
        let rendered = build_and_convert("tests/data/model.md", Templates::Shex, None);

        // Assert
        let expected = fs::read_to_string("tests/data/expected_shex.shex")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_shacl() {
        // Arrange
        let rendered = build_and_convert("tests/data/model.md", Templates::Shacl, None);

        // Assert
        let expected = fs::read_to_string("tests/data/expected_shacl.ttl")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_owl() {
        // Arrange
        let rendered = build_and_convert("tests/data/model.md", Templates::Owl, None);

        // Assert
        let expected = fs::read_to_string("tests/data/expected_owl.ttl")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_python_dc() {
        // Arrange
        let rendered = build_and_convert("tests/data/model.md", Templates::PythonDataclass, None);

        // Assert
        let expected = fs::read_to_string("tests/data/expected_python_dc.py")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_python_pydantic_xml() {
        // Arrange
        let rendered = build_and_convert("tests/data/model.md", Templates::PythonPydanticXML, None);

        // Assert
        let expected = fs::read_to_string("tests/data/expected_python_pydantic_xml.py")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_xsd() {
        // Arrange
        let rendered = build_and_convert("tests/data/model.md", Templates::XmlSchema, None);

        // Assert
        let expected = fs::read_to_string("tests/data/expected_xml_schema.xsd")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_mkdocs() {
        // Arrange
        let rendered = build_and_convert("tests/data/model.md", Templates::MkDocs, None);

        // Assert
        let expected = fs::read_to_string("tests/data/expected_mkdocs.md")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_typescript() {
        // Arrange
        let rendered = build_and_convert("tests/data/model.md", Templates::Typescript, None);

        // Assert
        let expected = fs::read_to_string("tests/data/expected_typescript.ts")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_typescript_zod() {
        // Arrange
        let rendered = build_and_convert("tests/data/model.md", Templates::TypescriptZod, None);

        // Assert
        let expected = fs::read_to_string("tests/data/expected_typescript_zod.ts")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_typescript_zod_json_ld() {
        // Arrange
        let rendered = build_and_convert(
            "tests/data/model.md",
            Templates::TypescriptZod,
            Some(&HashMap::from([(
                "json-ld".to_string(),
                "true".to_string(),
            )])),
        );

        // Assert
        let expected = fs::read_to_string("tests/data/expected_typescript_zod_json_ld.ts")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_pydantic() {
        // Arrange
        let rendered = build_and_convert("tests/data/model.md", Templates::PythonPydantic, None);

        // Assert
        let expected = fs::read_to_string("tests/data/expected_pydantic.py")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_pydantic_unitdef() {
        // Arrange
        let rendered = build_and_convert(
            "tests/data/model_unitdef.md",
            Templates::PythonPydantic,
            Some(&HashMap::from([(
                "astropy".to_string(),
                "true".to_string(),
            )])),
        );

        // Assert
        let expected = fs::read_to_string("tests/data/expected_pydantic_unitdef.py")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_graphql() {
        // Arrange
        let rendered = build_and_convert("tests/data/model.md", Templates::Graphql, None);

        // Assert
        let expected = fs::read_to_string("tests/data/expected_graphql.graphql")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_golang() {
        // Arrange
        let rendered = build_and_convert("tests/data/model.md", Templates::Golang, None);

        // Assert
        let expected = fs::read_to_string("tests/data/expected_golang.go")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_golang_gorm() {
        // Arrange
        let rendered = build_and_convert(
            "tests/data/model_golang_gorm.md",
            Templates::Golang,
            Some(&HashMap::from([("gorm".to_string(), "true".to_string())])),
        );

        // Assert
        let expected = fs::read_to_string("tests/data/expected_golang_gorm.go")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_rust() {
        // Arrange
        let rendered = build_and_convert("tests/data/model.md", Templates::Rust, None);

        // Assert
        let expected = fs::read_to_string("tests/data/expected_rust.rs")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_rust_forbidden_names() {
        // Arrange
        let rendered =
            build_and_convert("tests/data/model_forbidden_names.md", Templates::Rust, None);

        // Assert
        let expected = fs::read_to_string("tests/data/expected_rust_forbidden.rs")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_rust_ld() {
        // Arrange
        let rendered = build_and_convert(
            "tests/data/model.md",
            Templates::Rust,
            Some(&HashMap::from([("jsonld".to_string(), "true".to_string())])),
        );

        // Assert
        let expected = fs::read_to_string("tests/data/expected_rust_ld.rs")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_protobuf() {
        // Arrange
        let rendered = build_and_convert("tests/data/model.md", Templates::Protobuf, None);

        // Assert
        let expected = fs::read_to_string("tests/data/expected_protobuf.proto")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }

    #[test]
    fn test_convert_to_mermaid() {
        // Arrange
        let rendered = build_and_convert("tests/data/model.md", Templates::Mermaid, None);

        // Assert
        let expected = fs::read_to_string("tests/data/expected_mermaid.md")
            .expect("Could not read expected file");
        assert_eq!(rendered, expected);
    }
}