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
//! # Address
//!
//! This modules contains structures to do with addresses, positions and
//! suchlike.

#[cfg(feature = "diesel")]
use crate::DieselConversionError;

#[derive(PartialEq)]
#[non_exhaustive]
/// A simple structure to hold address information
pub struct Address {
    /// This is the street where the address points to.
    pub street: Option<String>,

    /// This is the locality/city where the address points to.
    pub locality: Option<String>,

    /// This is the region/county where the address points to.
    pub region: Option<String>,

    /// This is the code where the address is. This may be a zip/postal code.
    pub code: Option<String>,

    /// This is the country where ths address points to.
    pub country: Option<String>,

    /// This is the longitude and latitude of the address.
    pub geo: Option<Geo>,
}

#[derive(PartialOrd, PartialEq)]
#[non_exhaustive]
/// A simple structure to hold Geo location.
pub struct Geo {
    /// This is the longitude for the geo-location.
    pub longitude: f64,

    /// This is the latitude for the geo-location.
    pub latitude: f64,
}

impl Address {
    /// Creates a new address
    #[must_use]
    pub const fn new(
        street: Option<String>,
        locality: Option<String>,
        region: Option<String>,
        code: Option<String>,
        country: Option<String>,
    ) -> Self {
        Self {
            street,
            locality,
            region,
            code,
            country,
            geo: None,
        }
    }
}

impl Geo {
    #[must_use]
    pub const fn new(longitude: f64, latitude: f64) -> Self {
        Self {
            longitude,
            latitude,
        }
    }
}

#[cfg(feature = "diesel_support")]
pub(crate) struct AddressRaw {
    pub street: Option<String>,
    pub locality: Option<String>,
    pub region: Option<String>,
    pub code: Option<String>,
    pub country: Option<String>,
    pub geo_long: Option<f64>,
    pub geo_lat: Option<f64>,
}

#[cfg(feature = "diesel_support")]
impl AddressRaw {
    /// Creates a new `AddressRaw`
    pub(crate) fn new(
        street: Option<String>,
        locality: Option<String>,
        region: Option<String>,
        code: Option<String>,
        country: Option<String>,
        geo_long: Option<f64>,
        geo_lat: Option<f64>,
    ) -> Self {
        Self {
            street,
            locality,
            region,
            code,
            country,
            geo_long,
            geo_lat,
        }
    }
}

#[cfg(feature = "diesel_support")]
impl Address {
    /// Converts an address to something that can be used by a DieselContact.
    pub(crate) fn to_sql_raw(
        this: Option<Self>,
    ) -> (
        Option<String>,
        Option<String>,
        Option<String>,
        Option<String>,
        Option<String>,
        (Option<f64>, Option<f64>),
    ) {
        match this {
            None => (None, None, None, None, None, (None, None)),
            Some(adr) => (
                adr.street,
                adr.locality,
                adr.region,
                adr.code,
                adr.country,
                match adr.geo {
                    None => (None, None),
                    Some(geo) => (Some(geo.longitude), Some(geo.latitude)),
                },
            ),
        }
    }
}

#[cfg(feature = "diesel_support")]
impl AddressRaw {
    /// This should only be used internally.
    pub(crate) fn try_from_sql_raw(
        self,
        field_name: &'static str,
    ) -> Result<Option<Address>, DieselConversionError> {
        let address = Address {
            // Set up the basic fields
            street: self.street,
            locality: self.locality,
            region: self.region,
            code: self.code,
            country: self.country,

            // Set up the Geo
            geo: {
                // Checks if the Geo is none
                if self.geo_long.is_none() && self.geo_lat.is_none() {
                    None
                } else {
                    // Otherwise make sure that it is some.
                    if self.geo_long.is_some() {
                        if self.geo_lat.is_some() {
                            // Creates the Geo
                            Some(Geo::new(
                                self.geo_long.unwrap(),
                                self.geo_lat.unwrap(),
                            ))
                        } else {
                            return Err(
                                DieselConversionError::InvalidProperty(
                                    String::from(format!(
                                        "Incomplete Geo in {}, \
                                            missing latitude",
                                        field_name
                                    )),
                                ),
                            );
                        }
                    } else {
                        if self.geo_lat.is_some() {
                            return Err(
                                DieselConversionError::InvalidProperty(
                                    String::from(format!(
                                        "Incomplete Geo in {}, \
                                            missing longitude",
                                        field_name
                                    )),
                                ),
                            );
                        } else {
                            None
                        }
                    }
                }
            },
        };

        // Check if the address has all fields.
        if address.street.is_none()
            && address.locality.is_none()
            && address.region.is_none()
            && address.code.is_none()
            && address.country.is_none()
            && address.geo.is_none()
        {
            Ok(None)
        } else {
            Ok(Some(address))
        }
    }
}