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
use chrono::NaiveDate;
use serde::{Deserialize, Serialize};
use typeshare::typeshare;
use utoipa::ToSchema;
use uuid::Uuid;

/// Represents user drawing.
#[typeshare]
#[derive(Debug, Clone, Deserialize, ToSchema, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DrawingDto {
    pub id: Uuid,

    pub variant: DrawingVariant,

    pub layer_id: Uuid,
    pub add_date: Option<NaiveDate>,
    pub remove_date: Option<NaiveDate>,
    pub rotation: f32,
    pub scale_x: f32,
    pub scale_y: f32,
    pub x: i32,
    pub y: i32,
    pub notes: String,
}

#[typeshare]
#[derive(Debug, Clone, Deserialize, ToSchema, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RectangleProperties {
    pub width: f32,
    pub height: f32,
    pub color: String,
    pub fill_pattern: FillPatternType,
    pub stroke_width: f32,
}

#[typeshare]
#[derive(Debug, Clone, Deserialize, ToSchema, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EllipseProperties {
    pub radius_x: f32,
    pub radius_y: f32,
    pub color: String,
    pub fill_pattern: FillPatternType,
    pub stroke_width: f32,
}

#[typeshare]
#[derive(Debug, Clone, Deserialize, ToSchema, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FreeLineProperties {
    pub points: Vec<Vec<f32>>,
    pub color: String,
    pub fill_pattern: FillPatternType,
    pub stroke_width: f32,
}

#[typeshare]
#[derive(Debug, Clone, Deserialize, ToSchema, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PolygonProperties {
    pub points: Vec<Vec<f32>>,
    pub color: String,
    pub fill_pattern: FillPatternType,
    pub stroke_width: f32,
}

#[typeshare]
#[derive(Debug, Clone, Deserialize, ToSchema, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelTextProperties {
    pub text: String,
    pub width: i32,
    pub height: i32,
    pub color: String,
}

#[typeshare]
#[derive(Debug, Clone, Deserialize, ToSchema, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ImageProperties {
    pub path: String,
}

/// Represents user drawing.
#[typeshare]
#[derive(Debug, Clone, Deserialize, ToSchema, Serialize)]
#[serde(tag = "type", content = "properties")]
pub enum DrawingVariant {
    Rectangle(RectangleProperties),
    Ellipse(EllipseProperties),
    FreeLine(FreeLineProperties),
    BezierPolygon(PolygonProperties),
    LabelText(LabelTextProperties),
    Image(ImageProperties),
}

#[typeshare]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum FillPatternType {
    #[serde(rename = "fill")]
    Fill,
    #[serde(rename = "none")]
    None,
    #[serde(rename = "hatchdown")]
    HatchDown,
    #[serde(rename = "hatchup")]
    HatchUp,
    #[serde(rename = "crosshatch")]
    CrossHatch,
    #[serde(rename = "points")]
    Points,
    #[serde(rename = "wave")]
    Wave,
}

/// Used to change the `add_date` of a drawing.
#[typeshare]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct UpdateAddDateDrawingDto {
    /// The id of the drawing.
    pub id: Uuid,
    /// The date the drawing was added to the map.
    /// If None, the drawing always existed.
    pub add_date: Option<NaiveDate>,
}

/// Used to change the `remove_date` of a drawing.
#[typeshare]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct UpdateRemoveDateDrawingDto {
    /// The id of the drawing.
    pub id: Uuid,
    /// The date the drawing was removed from the map.
    /// If None, the drawing is still on the map.
    pub remove_date: Option<NaiveDate>,
}

/// Used to change the `notes` of a drawing.
#[typeshare]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct UpdateNotesDrawingDto {
    /// The id of the drawing.
    pub id: Uuid,
    /// The new Markdown note.
    pub notes: String,
}

/// Used to differentiate between different update operations on drawings.
///
/// Ordering of enum variants is important.
/// Serde will try to deserialize starting from the top.
#[typeshare]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(tag = "type", content = "content")]
pub enum UpdateDrawingsDto {
    /// Update the actual drawings data.
    Update(Vec<DrawingDto>),
    /// Change the `add_date` of a drawing.
    UpdateAddDate(Vec<UpdateAddDateDrawingDto>),
    /// Change the `remove_date` of drawings.
    UpdateRemoveDate(Vec<UpdateRemoveDateDrawingDto>),
    /// Change the `notes` of drawings.
    UpdateNotes(Vec<UpdateNotesDrawingDto>),
}