geogebra_types/
raw.rs

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
//! Raw GeoGebra structures.

use std::marker::PhantomData;

use serde::{de::Visitor, ser::SerializeMap, Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

/// Top-level element representing a Geogebra workspace
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", rename = "geogebra")]
pub struct Geogebra {
    /// Format version. Schema states this attribute is deprecated, but Geogebra complains
    /// if it's not here. Library was tested with 5.0
    #[serde(rename = "@format")]
    pub format: String,
    /// Application to load this file in.
    #[serde(rename = "@app")]
    pub app: String,
    /// Subapplication to load this file in.
    #[serde(rename = "@subApp")]
    pub sub_app: String,
    /// The contained construction
    pub construction: Construction,
}

/// The construction contained in the workspace
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Construction {
    /// Construction's items
    #[serde(rename = "$value")]
    pub items: Vec<ConstructionItem>,
}

/// An item of the construction element.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ConstructionItem {
    /// An element of the construction.
    Element(Element),
    /// A construction command.
    Command(Command),
    /// An expression
    Expression(Expression),
}

/// A construction element.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Element {
    /// Type of this element
    #[serde(rename = "@type")]
    pub type_: ElementType,
    /// The element's label
    #[serde(rename = "@label")]
    pub label: String,
    /// The element's caption
    pub caption: Option<Val<String>>,
    /// What should be displayed in place of the label
    pub label_mode: Val<LabelMode>,
    /// Which parts of the element should be shown
    pub show: Show,
    /// The element's coordinates
    pub coords: Option<Coords>,
    /// How to draw the line, if this is a line
    pub line_style: Option<LineStyle>,
    /// Color of this object
    pub obj_color: Option<ObjColorType>,
}

/// Type of an element
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ElementType {
    Point,
    Segment,
    Line,
    Numeric,
    Conic,
    Ray,
    List,
}

/// Style of a line
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
pub struct LineStyle {
    /// Thickness. 5 by default
    #[serde(rename = "@thickness")]
    pub thickness: Option<u16>,
    /// Stroke
    #[serde(rename = "@type")]
    pub type_: Option<LineType>,
    /// Opacity of this object
    #[serde(rename = "@opacity")]
    pub opacity: Option<f64>,
}

/// Stroke of a line
#[derive(Debug, Clone, Copy, Serialize_repr, Deserialize_repr)]
#[repr(u16)]
pub enum LineType {
    /// Solid line
    Solid = 0,
    /// Short dashes
    DashedShort = 10,
    /// Long dashes
    DashedLong = 15,
    /// Dots
    Dotted = 20,
    /// Dots and dashes
    DashedDotted = 30,
}

/// A value in an attribute
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct Val<T> {
    #[serde(rename = "@val")]
    pub val: T,
}

impl<T> From<T> for Val<T> {
    fn from(value: T) -> Self {
        Self { val: value }
    }
}

/// What to display in place of an element's label
#[derive(Debug, Clone, Copy, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum LabelMode {
    /// Label
    Label,
    /// Label = Value
    LabelAndValue,
    /// Value
    Value,
    /// Caption
    Caption,
    /// Caption = Value
    CaptionAndValue,
}

/// What parts of an element should be shown.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct Show {
    /// Show the object itself.
    #[serde(rename = "@object")]
    pub object: bool,
    /// Show the object's label
    #[serde(rename = "@label")]
    pub label: bool,
}

impl Show {
    /// Show only the object
    #[must_use]
    pub fn object() -> Self {
        Self {
            object: true,
            label: false,
        }
    }

    /// Show only the label
    #[must_use]
    pub fn label() -> Self {
        Self {
            object: false,
            label: true,
        }
    }

    /// Show both the object and its label
    #[must_use]
    pub fn object_and_label() -> Self {
        Self {
            object: true,
            label: true,
        }
    }

    /// Show neither the object nor its label
    #[must_use]
    pub fn none() -> Self {
        Self {
            object: false,
            label: false,
        }
    }
}

/// Cartesian coordinates of an element
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct Coords {
    /// X coordinate
    #[serde(rename = "@x")]
    x: f64,
    /// Y coordinate
    #[serde(rename = "@y")]
    y: f64,
    /// Z coordinate
    #[serde(rename = "@z")]
    z: f64,
}

impl Coords {
    /// Create new coords from X and Y coordinates. Z is automatically set to 1.
    #[must_use]
    pub fn xy(x: f64, y: f64) -> Self {
        Self { x, y, z: 1.0 }
    }
}

/// A construction command.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Command {
    /// The name of the command
    #[serde(rename = "@name")]
    pub name: String,
    /// Command inputs
    pub input: IndexedAttrs<String>,
    /// Command outputs
    pub output: IndexedAttrs<String>,
}

/// Helper for Geogebra's `a1`, `a2`, `a3` attributes in io.
#[derive(Debug, Clone)]
pub struct IndexedAttrs<T> {
    /// Attributes
    pub attrs: Vec<T>,
}

impl<T> From<Vec<T>> for IndexedAttrs<T> {
    fn from(value: Vec<T>) -> Self {
        Self { attrs: value }
    }
}

impl<T: Serialize> Serialize for IndexedAttrs<T> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut s = serializer.serialize_map(Some(self.attrs.len()))?;

        for (i, attr) in self.attrs.iter().enumerate() {
            s.serialize_entry(&format!("@a{i}"), attr)?;
        }

        s.end()
    }
}

impl<'de, T: Deserialize<'de>> Deserialize<'de> for IndexedAttrs<T> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_map(IndexedAttrsVisitor(PhantomData))
    }
}

struct IndexedAttrsVisitor<T>(PhantomData<T>);

impl<'de, T: Deserialize<'de>> Visitor<'de> for IndexedAttrsVisitor<T> {
    type Value = IndexedAttrs<T>;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "a map")
    }

    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: serde::de::MapAccess<'de>,
    {
        let mut attrs = Vec::new();

        while let Some(v) = map.next_value()? {
            attrs.push(v);
        }

        Ok(IndexedAttrs { attrs })
    }
}

/// A Geogebra expression
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Expression {
    /// Type of this expression
    #[serde(rename = "@type")]
    pub type_: ElementType,
    /// Label of this expression
    #[serde(rename = "@label")]
    pub label: String,
    /// The expression itself
    #[serde(rename = "@exp")]
    pub exp: String,
}

/// Color in Geogebra
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct ObjColorType {
    /// The red channel
    #[serde(rename = "@r")]
    pub r: u8,
    /// The green channel
    #[serde(rename = "@g")]
    pub g: u8,
    /// The blue channel
    #[serde(rename = "@b")]
    pub b: u8,
}