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
use {
    crate::{request, Meta, KAKAO_LOCAL_API_BASE_URL},
    futures::prelude::*,
    serde::Deserialize,
};

#[derive(Debug, Clone)]
pub struct Address {
    pub address: Option<String>,
    pub land_lot: Option<LandLotAddress>,
    pub road: Option<RoadAddress>,
}

#[derive(Debug, Clone)]
pub struct LandLotAddress {
    pub address: String,
    pub province: String,
    pub city: String,
    pub town: String,
    pub neighborhood: Option<String>,
    pub h_code: Option<usize>,
    pub b_code: Option<usize>,
    pub is_mountain: Option<bool>,
    pub main_address_number: Option<usize>,
    pub sub_address_number: Option<usize>,
    pub zip_code: Option<usize>,
    pub longitude: Option<f32>,
    pub latitude: Option<f32>,
}

#[derive(Debug, Clone)]
pub struct RoadAddress {
    pub address: String,
    pub province: String,
    pub city: String,
    pub town: String,
    pub road_name: String,
    pub is_underground: bool,
    pub main_building_number: Option<usize>,
    pub sub_building_number: Option<usize>,
    pub building_name: String,
    pub post_code: Option<usize>,
    pub longitude: Option<f32>,
    pub latitude: Option<f32>,
}

#[derive(Debug, Clone)]
pub struct AddressResponse {
    pub addresses: Vec<Address>,
    pub total_count: usize,
    pub pageable_count: usize,
    pub is_end: bool,
}

#[derive(Debug, Clone)]
pub struct AddressRequest {
    base_url: String,
    app_key: String,
    query: String,
    page: usize,
    size: usize,
}

impl AddressRequest {
    pub fn new(app_key: &str, query: &str) -> Self {
        AddressRequest {
            base_url: KAKAO_LOCAL_API_BASE_URL.to_string(),
            app_key: app_key.to_string(),
            query: query.to_string(),
            page: 1,
            size: 15,
        }
    }

    pub fn base_url(&mut self, base_url: &str) -> &mut Self {
        self.base_url = base_url.to_string();
        self
    }

    pub fn page(&mut self, page: usize) -> &mut Self {
        self.page = page;
        self
    }

    pub fn size(&mut self, size: usize) -> &mut Self {
        self.size = size;
        self
    }

    pub fn get(&self) -> impl Future<Item = AddressResponse, Error = failure::Error> {
        static API_PATH: &'static str = "/search/address.json";

        request::<RawResponse>(
            &self.base_url,
            API_PATH,
            &[
                ("query", self.query.clone()),
                ("page", self.page.to_string()),
                ("size", self.size.to_string()),
            ],
            &self.app_key,
        )
        .map(|resp| {
            let addresses = resp
                .documents
                .into_iter()
                .map(|document| Address {
                    address: document.address_name,
                    land_lot: document.address.map(Into::into),
                    road: document.road_address.map(Into::into),
                })
                .filter(|addr| addr.land_lot.is_some() || addr.road.is_some())
                .collect();

            AddressResponse {
                addresses,
                total_count: resp.meta.total_count,
                pageable_count: resp.meta.pageable_count,
                is_end: resp.meta.is_end,
            }
        })
    }
}

#[derive(Debug, Deserialize)]
struct RawResponse {
    documents: Vec<Document>,
    meta: Meta,
}

#[derive(Debug, Deserialize)]
struct Document {
    address_name: Option<String>,
    address: Option<RawLandLotAddress>,
    road_address: Option<RawRoadAddress>,
}

#[derive(Debug, Deserialize)]
struct RawLandLotAddress {
    address_name: String,
    region_1depth_name: String,
    region_2depth_name: String,
    region_3depth_name: String,
    region_3depth_h_name: String,
    h_code: String,
    b_code: String,
    mountain_yn: String,
    main_address_no: String,
    sub_address_no: String,
    zip_code: String,
    x: String,
    y: String,
}

#[derive(Debug, Deserialize)]
struct RawRoadAddress {
    address_name: String,
    region_1depth_name: String,
    region_2depth_name: String,
    region_3depth_name: String,
    road_name: String,
    underground_yn: String,
    main_building_no: String,
    sub_building_no: String,
    building_name: String,
    zone_no: String,
    x: String,
    y: String,
}

impl From<RawLandLotAddress> for LandLotAddress {
    fn from(raddr: RawLandLotAddress) -> Self {
        LandLotAddress {
            address: raddr.address_name,
            province: raddr.region_1depth_name,
            city: raddr.region_2depth_name,
            town: raddr.region_3depth_name,
            neighborhood: if raddr.region_3depth_h_name.is_empty() {
                None
            } else {
                Some(raddr.region_3depth_h_name)
            },
            h_code: raddr.h_code.parse::<usize>().ok(),
            b_code: raddr.b_code.parse::<usize>().ok(),
            is_mountain: if raddr.mountain_yn.is_empty() {
                None
            } else {
                Some(raddr.mountain_yn == "Y")
            },
            main_address_number: raddr.main_address_no.parse::<usize>().ok(),
            sub_address_number: raddr.sub_address_no.parse::<usize>().ok(),
            zip_code: raddr.zip_code.parse::<usize>().ok(),
            longitude: raddr.x.parse::<f32>().ok(),
            latitude: raddr.y.parse::<f32>().ok(),
        }
    }
}

impl From<RawRoadAddress> for RoadAddress {
    fn from(raddr: RawRoadAddress) -> Self {
        RoadAddress {
            address: raddr.address_name,
            province: raddr.region_1depth_name,
            city: raddr.region_2depth_name,
            town: raddr.region_3depth_name,
            road_name: raddr.road_name,
            is_underground: raddr.underground_yn == "Y",
            main_building_number: raddr.main_building_no.parse::<usize>().ok(),
            sub_building_number: raddr.sub_building_no.parse::<usize>().ok(),
            building_name: raddr.building_name,
            post_code: raddr.zone_no.parse::<usize>().ok(),
            longitude: raddr.x.parse::<f32>().ok(),
            latitude: raddr.y.parse::<f32>().ok(),
        }
    }
}