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
use serde::ser::Serialize;
use std::collections::HashMap;
use std::fmt;

#[derive(Debug, Clone)]
pub enum Credentials {
    Basic(String, String), // Username, password
    None,
}

pub enum RequestBody<S: Serialize> {
    Xml(S),
    Form(S),
    RawForm(Vec<u8>),
    None,
}

#[derive(Debug, PartialEq, Eq)]
pub struct VersionRange {
    pub minimum: String,
    pub maximum: String,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct Status {
    pub database: String,
    pub api: String,
    pub gpx: String,
}

#[derive(Debug, PartialEq)]
pub struct Capabilities {
    pub versions: VersionRange,
    pub maximum_area: f64,
    pub maximum_note_area: f64,
    pub tracepoints_per_page: u64,
    pub maximum_waynodes: u64,
    pub maximum_changeset_elements: u64,
    pub timeout: u64,
    pub status: Status,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct Blacklist {
    pub regex: String,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct Imagery {
    pub blacklist: Vec<Blacklist>,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct Policy {
    pub imagery: Imagery,
}

#[derive(Debug, PartialEq)]
pub struct CapabilitiesAndPolicy {
    pub capabilities: Capabilities,
    pub policy: Policy,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct BoundingBox {
    pub left: f64,
    pub bottom: f64,
    pub right: f64,
    pub top: f64,
}

#[derive(Debug, PartialEq, Eq, Deserialize, Serialize, Clone)]
pub struct Tag {
    pub k: String,
    pub v: String,
}

impl Tag {
    pub fn new(k: &str, v: &str) -> Self {
        Tag {
            k: k.into(),
            v: v.into(),
        }
    }
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
#[serde(rename = "node")]
pub struct Node {
    pub id: u64,
    pub visible: bool,
    pub version: u64,
    pub changeset: u64,
    pub timestamp: String,
    pub user: Option<String>,
    pub uid: Option<u64>,
    pub lat: Option<f64>,
    pub lon: Option<f64>,
    #[serde(rename = "tag", default)]
    pub tags: Vec<Tag>,
}

#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct NodeRef {
    #[serde(rename = "ref")]
    pub node_id: u64,
}

#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct Member {
    #[serde(rename = "type")]
    pub member_type: String,
    #[serde(rename = "ref")]
    pub node_id: u64,
    pub role: String,
}

#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename = "way")]
pub struct Way {
    pub id: u64,
    pub visible: bool,
    pub version: u64,
    pub changeset: u64,
    pub timestamp: String,
    pub user: String,
    pub uid: u64,
    #[serde(rename = "nd", default)]
    pub node_refs: Vec<NodeRef>,
    #[serde(rename = "tag", default)]
    pub tags: Vec<Tag>,
}

#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename = "relation")]
pub struct Relation {
    pub id: u64,
    pub visible: bool,
    pub version: u64,
    pub changeset: u64,
    pub timestamp: String,
    pub user: String,
    pub uid: u64,
    #[serde(rename = "tag", default)]
    pub tags: Vec<Tag>,
    #[serde(rename = "member", default)]
    pub members: Vec<Member>,
}

#[derive(Debug, PartialEq)]
pub struct Map {
    pub bounds: BoundingBox,
    pub nodes: Vec<Node>,
    pub ways: Vec<Way>,
    pub relations: Vec<Relation>,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct Permission {
    pub name: String,
}

#[derive(Debug, PartialEq, Eq, Deserialize, Serialize, Clone)]
#[serde(rename = "changeset")]
pub struct ChangesetCreate {
    version: String,
    generator: String,
    #[serde(rename = "tag", default)]
    tags: Vec<Tag>,
}

impl ChangesetCreate {
    pub fn new(version: &str, generator: &str, tags: Vec<Tag>) -> Self {
        ChangesetCreate {
            version: version.into(),
            generator: generator.into(),
            tags,
        }
    }
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct DiscussionComment {
    pub date: String,
    pub uid: u64,
    pub user: String,
    pub text: String,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct Discussion {
    #[serde(rename = "comment", default)]
    pub comments: Vec<DiscussionComment>,
}

#[derive(Debug, PartialEq, Deserialize)]
pub struct Changeset {
    pub id: u64,
    pub user: String,
    pub uid: u64,
    pub created_at: String,
    pub closed_at: Option<String>,
    pub open: bool,
    pub discussion: Option<Discussion>,
    #[serde(rename = "tag", default)]
    pub tags: Vec<Tag>,

    // The bounding box attributes will be missing for an empty changeset
    pub min_lon: Option<f64>,
    pub min_lat: Option<f64>,
    pub max_lon: Option<f64>,
    pub max_lat: Option<f64>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct Modification {
    #[serde(rename = "node", default)]
    pub nodes: Vec<Node>,
    #[serde(rename = "way", default)]
    pub ways: Vec<Way>,
    #[serde(rename = "relation", default)]
    pub relations: Vec<Relation>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct Creation {
    #[serde(rename = "node", default)]
    pub nodes: Vec<Node>,
    #[serde(rename = "way", default)]
    pub ways: Vec<Way>,
    #[serde(rename = "relation", default)]
    pub relations: Vec<Relation>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct Deletion {
    #[serde(rename = "node", default)]
    pub nodes: Vec<Node>,
    #[serde(rename = "way", default)]
    pub ways: Vec<Way>,
    #[serde(rename = "relation", default)]
    pub relations: Vec<Relation>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
#[serde(rename = "osmChange")]
pub struct ChangesetChanges {
    #[serde(rename = "modify", default)]
    pub modifications: Vec<Modification>,
    #[serde(rename = "create", default)]
    pub creations: Vec<Creation>,
    #[serde(rename = "delete", default)]
    pub deletions: Vec<Deletion>,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
#[serde(rename = "node")]
pub struct DiffNode {
    pub old_id: u64,
    pub new_id: u64,
    pub new_version: u64,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
#[serde(rename = "node")]
pub struct DiffWay {
    pub old_id: u64,
    pub new_id: u64,
    pub new_version: u64,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
#[serde(rename = "node")]
pub struct DiffRelation {
    pub old_id: u64,
    pub new_id: u64,
    pub new_version: u64,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
#[serde(rename = "diffResult")]
pub struct DiffResult {
    #[serde(rename = "node", default)]
    pub nodes: Vec<DiffNode>,
    #[serde(rename = "way", default)]
    pub ways: Vec<DiffWay>,
    #[serde(rename = "relation", default)]
    pub relations: Vec<DiffRelation>,
}

#[derive(Debug, Default, PartialEq)]
pub struct ChangesetQueryParams {
    pub bbox: Option<BoundingBox>,
    pub user_id: Option<u64>,
    pub display_name: Option<String>,
    pub closed_after: Option<String>,
    pub created_before: Option<String>,
    pub open: Option<bool>,
    pub closed: Option<bool>,
    pub changeset_ids: Option<Vec<u64>>,
}

#[derive(Debug, Default, PartialEq, Eq)]
pub struct ElementIdParam {
    pub id: u64,
    pub version: Option<u64>,
}

impl ElementIdParam {
    pub fn new(id: u64, version: Option<u64>) -> Self {
        Self { id, version }
    }
}

impl fmt::Display for ElementIdParam {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}{}",
            self.id,
            self.version.map_or("".to_string(), |v| format!("v{}", v))
        )
    }
}

#[derive(Debug, PartialEq, Deserialize)]
pub struct WayFull {
    pub way: Way,
    #[serde(rename = "node", default)]
    pub nodes: Vec<Node>,
}

#[derive(Debug, PartialEq, Deserialize)]
pub struct RelationFull {
    pub relation: Relation,
    #[serde(rename = "way", default)]
    pub ways: Vec<Way>,
    #[serde(rename = "node", default)]
    pub nodes: Vec<Node>,
}

#[derive(Debug, Default, PartialEq, Eq, Deserialize)]
pub struct ContributorTerms {
    pub agreed: bool,
    #[serde(rename = "pd", default)]
    pub public_domain: bool,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct Image {
    #[serde(rename = "href")]
    pub url: String,
}

#[derive(Debug, Default, PartialEq, Eq, Deserialize)]
pub struct UserChangesets {
    pub count: u64,
}

#[derive(Debug, Default, PartialEq, Eq, Deserialize)]
pub struct Traces {
    pub count: u64,
}

#[derive(Debug, Default, PartialEq, Eq, Deserialize)]
pub struct Block {
    pub count: u64,
    pub active: u64,
}

#[derive(Debug, PartialEq, Deserialize)]
pub struct CoordsView {
    pub lat: f64,
    pub lon: f64,
    pub zoom: u8,
}

#[derive(Debug, Default, PartialEq, Eq)]
pub struct Messages {
    pub received: u64,
    pub unread: u64,
    pub sent: u64,
}

#[derive(Debug, Default, PartialEq)]
pub struct User {
    pub id: u64,
    pub display_name: String,
    pub account_created: String,
    pub description: Option<String>,
    pub contributor_terms: ContributorTerms,
    pub image: Option<Image>,
    pub changesets: UserChangesets,
    pub traces: Traces,
    pub blocks: Vec<Block>,
    pub home: Option<CoordsView>,
    pub languages: Vec<String>,
    pub messages: Messages,
}

pub type UserPreferences = HashMap<String, String>;

#[derive(Debug, Default, PartialEq, Eq, Deserialize)]
pub struct Comment {
    #[serde(rename = "uid")]
    pub id: u64,
    pub date: String,
    pub user: String,
    pub user_url: String,
    pub action: String,
    pub text: String,
    pub html: String,
}

#[derive(Debug, Default, PartialEq)]
pub struct Note {
    pub id: u64,
    pub lon: f64,
    pub lat: f64,
    pub url: String,
    pub comment_url: String,
    pub close_url: String,
    pub created_at: String,
    pub status: String,
    pub comments: Vec<Comment>,
}

#[derive(Debug, Default, PartialEq, Serialize)]
pub struct NoteContent {
    pub lat: f64,
    pub lon: f64,
    pub text: String,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum NoteSearchSortOption {
    CreatedAt,
    UpdatedAt,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum NoteSearchOrderOption {
    Oldest,
    Newest,
}

#[derive(Debug, Default, Serialize)]
pub struct NoteSearchOptions {
    pub q: String,
    pub limit: Option<u16>,
    pub closed: Option<i64>,
    pub display_name: Option<String>,
    pub user: Option<u64>,
    pub from: Option<String>,
    pub to: Option<String>,
    pub sort: Option<NoteSearchSortOption>,
    pub order: Option<NoteSearchOrderOption>,
}