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
use crate::errors::*;
use crate::types::*;
use uuid::Uuid;

/// Describes an address
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Address {
    #[doc(hidden)]
    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
    extra: Option<String>,
    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
    client_id: Option<i32>,
    /// A two-letter ISO 3166-1 alpha-2 country code
    country_code: String,
    /// State, if applicable
    state: String,
    /// City
    city: String,
    /// First line of the address
    street_line1: String,
    /// Second line of the address
    street_line2: String,
    /// Address postal code
    postal_code: String,
}

impl RObject for Address {
    #[doc(hidden)]
    fn extra(&self) -> Option<&str> {
        self.extra.as_deref()
    }
    #[doc(hidden)]
    fn client_id(&self) -> Option<i32> {
        self.client_id
    }
}

impl Address {
    pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> {
        Ok(serde_json::from_str(json.as_ref())?)
    }
    pub fn builder() -> RTDAddressBuilder {
        let mut inner = Address::default();
        inner.extra = Some(Uuid::new_v4().to_string());

        RTDAddressBuilder { inner }
    }

    pub fn country_code(&self) -> &String {
        &self.country_code
    }

    pub fn state(&self) -> &String {
        &self.state
    }

    pub fn city(&self) -> &String {
        &self.city
    }

    pub fn street_line1(&self) -> &String {
        &self.street_line1
    }

    pub fn street_line2(&self) -> &String {
        &self.street_line2
    }

    pub fn postal_code(&self) -> &String {
        &self.postal_code
    }
}

#[doc(hidden)]
pub struct RTDAddressBuilder {
    inner: Address,
}

impl RTDAddressBuilder {
    pub fn build(&self) -> Address {
        self.inner.clone()
    }

    pub fn country_code<T: AsRef<str>>(&mut self, country_code: T) -> &mut Self {
        self.inner.country_code = country_code.as_ref().to_string();
        self
    }

    pub fn state<T: AsRef<str>>(&mut self, state: T) -> &mut Self {
        self.inner.state = state.as_ref().to_string();
        self
    }

    pub fn city<T: AsRef<str>>(&mut self, city: T) -> &mut Self {
        self.inner.city = city.as_ref().to_string();
        self
    }

    pub fn street_line1<T: AsRef<str>>(&mut self, street_line1: T) -> &mut Self {
        self.inner.street_line1 = street_line1.as_ref().to_string();
        self
    }

    pub fn street_line2<T: AsRef<str>>(&mut self, street_line2: T) -> &mut Self {
        self.inner.street_line2 = street_line2.as_ref().to_string();
        self
    }

    pub fn postal_code<T: AsRef<str>>(&mut self, postal_code: T) -> &mut Self {
        self.inner.postal_code = postal_code.as_ref().to_string();
        self
    }
}

impl AsRef<Address> for Address {
    fn as_ref(&self) -> &Address {
        self
    }
}

impl AsRef<Address> for RTDAddressBuilder {
    fn as_ref(&self) -> &Address {
        &self.inner
    }
}