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
//! OSM elements.
//!
//! See: https://wiki.openstreetmap.org/wiki/Elements

use crate::geo::Coordinate;

type RelationRole = String;
type TimeStamp = i64;

/// A coordinate with meta data. See OSM docs for [`Node`].
///
/// [`Node`]: https://wiki.openstreetmap.org/wiki/Node
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Node {
    pub id: i64,
    pub coordinate: Coordinate,
    pub meta: Meta,
}

/// Group of nodes and meta data. See OSM docs for [`Way`].
///
/// [`Way`]: https://wiki.openstreetmap.org/wiki/Way
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Way {
    pub id: i64,
    pub refs: Vec<i64>,
    pub meta: Meta,
}

/// Group of elements (node, way or relation). See OSM docs for [`Relation`].
///
/// [`Relation`]: https://wiki.openstreetmap.org/wiki/Relation
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Relation {
    pub id: i64,
    pub members: Vec<RelationMember>,
    pub meta: Meta,
}

/// Key value pairs. See OSM docs for [`Tags`].
///
/// [`Tags`]: https://wiki.openstreetmap.org/wiki/Tags
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Tag {
    pub key: String,
    pub value: String,
}

/// Common meta data used by multiple entities.
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Meta {
    pub tags: Vec<Tag>,
    pub version: Option<u32>,
    pub author: Option<AuthorInformation>,
}

/// Author information is used to identify what nodes, ways and relation a specific user has
/// added. When working on non osm maps, this data is irrelevant.
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct AuthorInformation {
    pub created: TimeStamp,
    pub change_set: u64,
    pub uid: u64,
    pub user: String,
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub enum RelationMember {
    Node(i64, RelationRole),
    Way(i64, RelationRole),
    Relation(i64, RelationRole),
}

impl From<(String, String)> for Tag {
    fn from((key, value): (String, String)) -> Self {
        Tag { key, value }
    }
}

impl From<(&str, &str)> for Tag {
    fn from((key, value): (&str, &str)) -> Self {
        Tag {
            key: key.to_owned(),
            value: value.to_owned(),
        }
    }
}

impl RelationMember {
    pub fn ref_id(&self) -> i64 {
        match self {
            RelationMember::Node(id, _) => *id,
            RelationMember::Way(id, _) => *id,
            RelationMember::Relation(id, _) => *id,
        }
    }

    pub fn role(&self) -> &str {
        match self {
            RelationMember::Node(_, role) => role,
            RelationMember::Way(_, role) => role,
            RelationMember::Relation(_, role) => role,
        }
    }
}

impl Default for Node {
    fn default() -> Self {
        Node {
            id: 0,
            coordinate: Coordinate { lat: 0, lon: 0 },
            meta: Default::default(),
        }
    }
}

impl Default for Way {
    fn default() -> Self {
        Way {
            id: 0,
            refs: vec![],
            meta: Default::default(),
        }
    }
}

impl Default for Relation {
    fn default() -> Self {
        Relation {
            id: 0,
            members: vec![],
            meta: Default::default(),
        }
    }
}

impl Default for Meta {
    fn default() -> Self {
        Meta {
            tags: vec![],
            version: None,
            author: None,
        }
    }
}