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
use std::fmt::Display;

use serde::Deserialize;

use super::{objects::*, Omf1Error};

/// Converts a `&Model` into either a reference to the individual item, or into
/// a subset enum.
///
/// This is used by `Omf1Root::get` to check variants on load.
pub trait FromModel {
    type Output<'a>;

    fn from_model(model: &Model) -> Result<Self::Output<'_>, Omf1Error>;
}

/// Creates enums and `FromModel` implementations for the UidModel objects in OMF v1.
macro_rules! model {
    ($( $variant:ident )*) => {
        /// Contains an OMF v1 top-level object.
        #[derive(Debug, Deserialize)]
        #[serde(tag = "__class__")]
        pub enum Model {
            $( $variant($variant), )*
        }

        /// The types of object allowed at the top level of OMF v1.
        #[derive(Debug)]
        pub enum ModelType {
            $( $variant, )*
        }

        impl Model {
            /// Return the model type.
            fn model_type(&self) -> ModelType {
                match self {
                    $( Self::$variant(_) => ModelType::$variant, )*
                }
            }
        }

        $(
            impl FromModel for $variant {
                type Output<'a> = &'a $variant;

                fn from_model(model: &Model) -> Result<Self::Output<'_>, Omf1Error> {
                    match model {
                        Model::$variant(x) => Ok(x),
                        _ => Err(Omf1Error::WrongType {
                            found: model.model_type(),
                            expected: &[ModelType::$variant],
                        }),
                    }
                }
            }
        )*
    };
}

/// Creates marker type, a subset of `Model`, and a `FromModel` implementation to tie them
/// together.
///
/// This lets us have type-tagged keys for a subset of model types in the objects that
/// `Omf1Root::get` can load and check automatically. The loading code can then match
/// exhaustively without worrying about the incorrect types.
macro_rules! model_subset {
    ($model_name:ident $enum_name:ident { $( $variant:ident )* }) => {
        #[derive(Debug)]
        pub struct $model_name {}

        #[derive(Debug, Clone, Copy)]
        #[allow(clippy::enum_variant_names)]
        pub enum $enum_name<'a> {
            $( $variant(&'a $variant), )*
        }

        impl FromModel for $model_name {
            type Output<'a> = $enum_name<'a>;

            fn from_model(model: &Model) -> Result<Self::Output<'_>, Omf1Error> {
                match model {
                    $( Model::$variant(x) => Ok($enum_name::$variant(x)), )*
                    _ => Err(Omf1Error::WrongType {
                        found: model.model_type(),
                        expected: &[$( ModelType::$variant ),*],
                    }),
                }
            }
        }
    };
}

model! {
    Project
    PointSetElement
    PointSetGeometry
    LineSetElement
    LineSetGeometry
    SurfaceElement
    SurfaceGeometry
    SurfaceGridGeometry
    VolumeElement
    VolumeGridGeometry
    ScalarColormap
    DateTimeColormap
    Legend
    ScalarData
    DateTimeData
    Vector2Data
    Vector3Data
    ColorData
    StringData
    MappedData
    ImageTexture
    ScalarArray
    Vector2Array
    Vector3Array
    Int2Array
    Int3Array
    StringArray
    DateTimeArray
    ColorArray
}

impl Display for ModelType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

model_subset! {
    Elements ElementModel {
        PointSetElement
        LineSetElement
        SurfaceElement
        VolumeElement
    }
}

model_subset! {
    Data DataModel {
        ScalarData
        DateTimeData
        Vector2Data
        Vector3Data
        ColorData
        StringData
        MappedData
    }
}

model_subset! {
    SurfaceGeometries SurfaceGeometryModel {
        SurfaceGeometry
        SurfaceGridGeometry
    }
}

model_subset! {
    LegendArrays LegendArrayModel {
        ColorArray
        DateTimeArray
        StringArray
        ScalarArray
    }
}

model_subset! {
    ColorArrays ColorArrayModel {
        Int3Array
        ColorArray
    }
}