From c8f2b28c07b54fe0839edab11c5c08712ab0eeda Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Sun, 7 Jan 2024 21:04:15 +0900 Subject: [PATCH 01/24] =?UTF-8?q?add:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`PrefectureMasterApi`=E3=82=92=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 都道府県マスタを取得するためのモジュール --- src/api.rs | 1 + src/api/prefecture_master_api.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 src/api/prefecture_master_api.rs diff --git a/src/api.rs b/src/api.rs index bd115c67..cb581b77 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,6 +1,7 @@ #[cfg(not(target_arch = "wasm32"))] pub mod blocking; pub mod client; +mod prefecture_master_api; use crate::entity::{City, Prefecture}; use crate::err::Error; diff --git a/src/api/prefecture_master_api.rs b/src/api/prefecture_master_api.rs new file mode 100644 index 00000000..2385f6ef --- /dev/null +++ b/src/api/prefecture_master_api.rs @@ -0,0 +1,14 @@ +use crate::entity::Prefecture; +use crate::err::Error; + +pub struct PrefectureMasterApi {} + +impl PrefectureMasterApi { + async fn get(&self, prefecture_name: &str) -> Result { + todo!() + } + #[cfg(not(target_arch = "wasm32"))] + fn get_blocking(&self, prefecture_name: &str) -> Result { + todo!() + } +} From ab5a4533c3c3b249663b0b5a002d690f81e24e5e Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Sun, 7 Jan 2024 21:11:36 +0900 Subject: [PATCH 02/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`PrefectureMasterApi.get()`=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/prefecture_master_api.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/api/prefecture_master_api.rs b/src/api/prefecture_master_api.rs index 2385f6ef..c8461ea2 100644 --- a/src/api/prefecture_master_api.rs +++ b/src/api/prefecture_master_api.rs @@ -1,11 +1,25 @@ use crate::entity::Prefecture; -use crate::err::Error; +use crate::err::{ApiErrorKind, Error}; -pub struct PrefectureMasterApi {} +pub struct PrefectureMasterApi { + server_url: &'static str, +} impl PrefectureMasterApi { async fn get(&self, prefecture_name: &str) -> Result { - todo!() + let endpoint = format!("{}/{}/master.json", self.server_url, prefecture_name); + let response = match reqwest::get(&endpoint).await { + Ok(result) => result, + Err(_) => return Err(Error::new_api_error(ApiErrorKind::Fetch(endpoint))), + }; + if response.status() == 200 { + match response.json::().await { + Ok(result) => Ok(result), + Err(_) => Err(Error::new_api_error(ApiErrorKind::Deserialize(endpoint))), + } + } else { + Err(Error::new_api_error(ApiErrorKind::Fetch(endpoint))) + } } #[cfg(not(target_arch = "wasm32"))] fn get_blocking(&self, prefecture_name: &str) -> Result { From ed6e5a222a058fac9aa8f1c8b296111e7e9a2894 Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Sun, 7 Jan 2024 21:12:37 +0900 Subject: [PATCH 03/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`PrefectureMasterApi.get=5Fblocking()`=E3=82=92=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/prefecture_master_api.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/api/prefecture_master_api.rs b/src/api/prefecture_master_api.rs index c8461ea2..c55e5a74 100644 --- a/src/api/prefecture_master_api.rs +++ b/src/api/prefecture_master_api.rs @@ -23,6 +23,18 @@ impl PrefectureMasterApi { } #[cfg(not(target_arch = "wasm32"))] fn get_blocking(&self, prefecture_name: &str) -> Result { - todo!() + let endpoint = format!("{}/{}/master.json", self.server_url, prefecture_name); + let response = match reqwest::blocking::get(&endpoint) { + Ok(result) => result, + Err(_) => return Err(Error::new_api_error(ApiErrorKind::Fetch(endpoint))), + }; + if response.status() == 200 { + match response.json::() { + Ok(result) => Ok(result), + Err(_) => Err(Error::new_api_error(ApiErrorKind::Deserialize(endpoint))), + } + } else { + Err(Error::new_api_error(ApiErrorKind::Fetch(endpoint))) + } } } From 518616786ddb4b50becaa48c1ca8b89479b712e1 Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Sun, 7 Jan 2024 21:17:56 +0900 Subject: [PATCH 04/24] =?UTF-8?q?add:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`CityMasterApi`=E3=82=92=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 市区町村マスタを取得するためのモジュール --- src/api.rs | 1 + src/api/city_master_api.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 src/api/city_master_api.rs diff --git a/src/api.rs b/src/api.rs index cb581b77..3add6da4 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,5 +1,6 @@ #[cfg(not(target_arch = "wasm32"))] pub mod blocking; +mod city_master_api; pub mod client; mod prefecture_master_api; diff --git a/src/api/city_master_api.rs b/src/api/city_master_api.rs new file mode 100644 index 00000000..c4cd410a --- /dev/null +++ b/src/api/city_master_api.rs @@ -0,0 +1,16 @@ +use crate::entity::City; +use crate::err::Error; + +pub struct CityMasterApi { + server_url: &'static str, +} + +impl CityMasterApi { + async fn get(&self, prefecture_name: &str, city_name: &str) -> Result { + todo!() + } + #[cfg(not(target_arch = "wasm32"))] + fn get_blocking(&self, prefecture_name: &str, city_name: &str) -> Result { + todo!() + } +} From 8a9f54975b39756de78d4a09c3d8e485e43ccf9d Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Sun, 7 Jan 2024 21:22:37 +0900 Subject: [PATCH 05/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`CityMasterApi.get()`=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/city_master_api.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/api/city_master_api.rs b/src/api/city_master_api.rs index c4cd410a..d88b0028 100644 --- a/src/api/city_master_api.rs +++ b/src/api/city_master_api.rs @@ -1,5 +1,5 @@ -use crate::entity::City; -use crate::err::Error; +use crate::entity::{City, Town}; +use crate::err::{ApiErrorKind, Error}; pub struct CityMasterApi { server_url: &'static str, @@ -7,7 +7,22 @@ pub struct CityMasterApi { impl CityMasterApi { async fn get(&self, prefecture_name: &str, city_name: &str) -> Result { - todo!() + let endpoint = format!("{}/{}/{}.json", self.server_url, prefecture_name, city_name); + let response = match reqwest::get(&endpoint).await { + Ok(result) => result, + Err(_) => return Err(Error::new_api_error(ApiErrorKind::Deserialize(endpoint))), + }; + if response.status() == 200 { + match response.json::>().await { + Ok(result) => Ok(City { + name: city_name.to_string(), + towns: result, + }), + Err(_) => Err(Error::new_api_error(ApiErrorKind::Deserialize(endpoint))), + } + } else { + Err(Error::new_api_error(ApiErrorKind::Fetch(endpoint))) + } } #[cfg(not(target_arch = "wasm32"))] fn get_blocking(&self, prefecture_name: &str, city_name: &str) -> Result { From 1720a2129ec0b84c20daf803e38d4f12eb6c6dbd Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Sun, 7 Jan 2024 21:23:40 +0900 Subject: [PATCH 06/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`CityMasterApi.get=5Fblocking()`=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/city_master_api.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/api/city_master_api.rs b/src/api/city_master_api.rs index d88b0028..8f9e8670 100644 --- a/src/api/city_master_api.rs +++ b/src/api/city_master_api.rs @@ -26,6 +26,21 @@ impl CityMasterApi { } #[cfg(not(target_arch = "wasm32"))] fn get_blocking(&self, prefecture_name: &str, city_name: &str) -> Result { - todo!() + let endpoint = format!("{}/{}/{}.json", self.server_url, prefecture_name, city_name); + let response = match reqwest::blocking::get(&endpoint) { + Ok(result) => result, + Err(_) => return Err(Error::new_api_error(ApiErrorKind::Fetch(endpoint))), + }; + if response.status() == 200 { + match response.json::>() { + Ok(result) => Ok(City { + name: city_name.to_string(), + towns: result, + }), + Err(_) => Err(Error::new_api_error(ApiErrorKind::Deserialize(endpoint))), + } + } else { + Err(Error::new_api_error(ApiErrorKind::Fetch(endpoint))) + } } } From 77d9f962f306d0ba80b9f9c0bc9f5bc380570713 Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Sun, 7 Jan 2024 21:44:48 +0900 Subject: [PATCH 07/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`PrefectureMasterApi`=E3=81=AB=E5=AF=BE=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `client.rs`, `blocking.rs`にそれぞれ書いていたものを移植している --- src/api/prefecture_master_api.rs | 99 ++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/src/api/prefecture_master_api.rs b/src/api/prefecture_master_api.rs index c55e5a74..7ae5fa7c 100644 --- a/src/api/prefecture_master_api.rs +++ b/src/api/prefecture_master_api.rs @@ -38,3 +38,102 @@ impl PrefectureMasterApi { } } } + +#[cfg(test)] +mod tests { + use crate::api::prefecture_master_api::PrefectureMasterApi; + + #[tokio::test] + async fn 非同期_富山県_成功() { + let prefecture_master_api = PrefectureMasterApi { + server_url: "https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist", + }; + let result = prefecture_master_api.get("富山県").await; + let prefecture = result.unwrap(); + assert_eq!(prefecture.name, "富山県"); + let cities = vec![ + "富山市", + "高岡市", + "魚津市", + "氷見市", + "滑川市", + "黒部市", + "砺波市", + "小矢部市", + "南砺市", + "射水市", + "中新川郡舟橋村", + "中新川郡上市町", + "中新川郡立山町", + "下新川郡入善町", + "下新川郡朝日町", + ]; + for city in cities { + assert!(prefecture.cities.contains(&city.to_string())); + } + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn 同期_富山県_成功() { + let prefecture_master_api = PrefectureMasterApi { + server_url: "https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist", + }; + let result = prefecture_master_api.get_blocking("富山県"); + let prefecture = result.unwrap(); + assert_eq!(prefecture.name, "富山県"); + let cities = vec![ + "富山市", + "高岡市", + "魚津市", + "氷見市", + "滑川市", + "黒部市", + "砺波市", + "小矢部市", + "南砺市", + "射水市", + "中新川郡舟橋村", + "中新川郡上市町", + "中新川郡立山町", + "下新川郡入善町", + "下新川郡朝日町", + ]; + for city in cities { + assert!(prefecture.cities.contains(&city.to_string())); + } + } + + #[tokio::test] + async fn 非同期_誤った都道府県名_失敗() { + let prefecture_master_api = PrefectureMasterApi { + server_url: "https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist", + }; + let result = prefecture_master_api.get("大阪都").await; + assert!(result.is_err()); + assert_eq!( + result.err().unwrap().error_message, + format!( + "{}/大阪都/master.jsonを取得できませんでした", + prefecture_master_api.server_url + ) + ); + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn 同期_誤った都道府県名_失敗() { + let prefecture_master_api = PrefectureMasterApi { + server_url: "https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist", + }; + let result = prefecture_master_api.get_blocking("大阪都"); + assert!(result.is_err()); + assert_eq!( + result.err().unwrap().error_message, + format!( + "{}/大阪都/master.jsonを取得できませんでした", + prefecture_master_api.server_url + ) + ); + } +} From cd452fb3f007dbc6eaae599db0f50c5064f5445f Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Sun, 7 Jan 2024 21:56:30 +0900 Subject: [PATCH 08/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`CityMasterApi`=E3=81=AB=E5=AF=BE=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `client.rs`, `blocking.rs`にそれぞれ書いていたものを移植している --- src/api/city_master_api.rs | 74 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/api/city_master_api.rs b/src/api/city_master_api.rs index 8f9e8670..7abe38c9 100644 --- a/src/api/city_master_api.rs +++ b/src/api/city_master_api.rs @@ -44,3 +44,77 @@ impl CityMasterApi { } } } + +#[cfg(test)] +mod tests { + use crate::api::city_master_api::CityMasterApi; + use crate::entity::Town; + + #[tokio::test] + async fn 非同期_石川県羽咋郡志賀町_成功() { + let city_master_api = CityMasterApi { + server_url: "https://geolonia.github.io/japanese-addresses/api/ja", + }; + let result = city_master_api.get("石川県", "羽咋郡志賀町").await; + let city = result.unwrap(); + assert_eq!(city.name, "羽咋郡志賀町"); + let town = Town { + name: "末吉".to_string(), + koaza: "千古".to_string(), + lat: Some(37.006235), + lng: Some(136.779155), + }; + assert!(city.towns.contains(&town)); + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn 同期_石川県羽咋郡志賀町_成功() { + let city_master_api = CityMasterApi { + server_url: "https://geolonia.github.io/japanese-addresses/api/ja", + }; + let result = city_master_api.get_blocking("石川県", "羽咋郡志賀町"); + let city = result.unwrap(); + assert_eq!(city.name, "羽咋郡志賀町"); + let town = Town { + name: "末吉".to_string(), + koaza: "千古".to_string(), + lat: Some(37.006235), + lng: Some(136.779155), + }; + assert!(city.towns.contains(&town)); + } + + #[tokio::test] + async fn 非同期_誤った市区町村名_失敗() { + let city_master_api = CityMasterApi { + server_url: "https://geolonia.github.io/japanese-addresses/api/ja", + }; + let result = city_master_api.get("石川県", "敦賀市").await; + assert!(result.is_err()); + assert_eq!( + result.err().unwrap().error_message, + format!( + "{}/石川県/敦賀市.jsonを取得できませんでした", + city_master_api.server_url + ) + ); + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn 同期_誤った市区町村名_失敗() { + let city_master_api = CityMasterApi { + server_url: "https://geolonia.github.io/japanese-addresses/api/ja", + }; + let result = city_master_api.get_blocking("石川県", "敦賀市"); + assert!(result.is_err()); + assert_eq!( + result.err().unwrap().error_message, + format!( + "{}/石川県/敦賀市.jsonを取得できませんでした", + city_master_api.server_url + ) + ); + } +} From 48df1dbef3e07c059a92da4c925fa1fd2412c0df Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Sun, 7 Jan 2024 22:15:33 +0900 Subject: [PATCH 09/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`prefecture=5Fmaster=5Fapi`=E3=81=A8`city=5Fmaster=5Fapi`?= =?UTF-8?q?=E3=82=92=E3=81=9D=E3=82=8C=E3=81=9E=E3=82=8Cpublic=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api.rs b/src/api.rs index 3add6da4..022dca3e 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,8 +1,8 @@ #[cfg(not(target_arch = "wasm32"))] pub mod blocking; -mod city_master_api; +pub mod city_master_api; pub mod client; -mod prefecture_master_api; +pub mod prefecture_master_api; use crate::entity::{City, Prefecture}; use crate::err::Error; From 870d91512ef1744b970039bb75f169e791f8ee3f Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Sun, 7 Jan 2024 22:20:39 +0900 Subject: [PATCH 10/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`PrefectureMasterApi`,`CityMasterApi`=E3=81=AE=E3=81=9D?= =?UTF-8?q?=E3=82=8C=E3=81=9E=E3=82=8C=E3=81=AB=E5=AE=9A=E7=BE=A9=E3=81=97?= =?UTF-8?q?=E3=81=9F=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=82=92public?= =?UTF-8?q?=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/city_master_api.rs | 4 ++-- src/api/prefecture_master_api.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/api/city_master_api.rs b/src/api/city_master_api.rs index 7abe38c9..27102f51 100644 --- a/src/api/city_master_api.rs +++ b/src/api/city_master_api.rs @@ -6,7 +6,7 @@ pub struct CityMasterApi { } impl CityMasterApi { - async fn get(&self, prefecture_name: &str, city_name: &str) -> Result { + pub async fn get(&self, prefecture_name: &str, city_name: &str) -> Result { let endpoint = format!("{}/{}/{}.json", self.server_url, prefecture_name, city_name); let response = match reqwest::get(&endpoint).await { Ok(result) => result, @@ -25,7 +25,7 @@ impl CityMasterApi { } } #[cfg(not(target_arch = "wasm32"))] - fn get_blocking(&self, prefecture_name: &str, city_name: &str) -> Result { + pub fn get_blocking(&self, prefecture_name: &str, city_name: &str) -> Result { let endpoint = format!("{}/{}/{}.json", self.server_url, prefecture_name, city_name); let response = match reqwest::blocking::get(&endpoint) { Ok(result) => result, diff --git a/src/api/prefecture_master_api.rs b/src/api/prefecture_master_api.rs index 7ae5fa7c..1d791486 100644 --- a/src/api/prefecture_master_api.rs +++ b/src/api/prefecture_master_api.rs @@ -6,7 +6,7 @@ pub struct PrefectureMasterApi { } impl PrefectureMasterApi { - async fn get(&self, prefecture_name: &str) -> Result { + pub async fn get(&self, prefecture_name: &str) -> Result { let endpoint = format!("{}/{}/master.json", self.server_url, prefecture_name); let response = match reqwest::get(&endpoint).await { Ok(result) => result, @@ -22,7 +22,7 @@ impl PrefectureMasterApi { } } #[cfg(not(target_arch = "wasm32"))] - fn get_blocking(&self, prefecture_name: &str) -> Result { + pub fn get_blocking(&self, prefecture_name: &str) -> Result { let endpoint = format!("{}/{}/master.json", self.server_url, prefecture_name); let response = match reqwest::blocking::get(&endpoint) { Ok(result) => result, From 7c622326817e9c0836c735cfc2d9b60a5868a475 Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Thu, 11 Jan 2024 00:22:41 +0900 Subject: [PATCH 11/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`CityMasterApi.server=5Furl`=E3=82=92public=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/city_master_api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/city_master_api.rs b/src/api/city_master_api.rs index 27102f51..aeb1f477 100644 --- a/src/api/city_master_api.rs +++ b/src/api/city_master_api.rs @@ -2,7 +2,7 @@ use crate::entity::{City, Town}; use crate::err::{ApiErrorKind, Error}; pub struct CityMasterApi { - server_url: &'static str, + pub server_url: &'static str, } impl CityMasterApi { From c4c762fe9553fee5bde54e2ebb009139b4a8cacd Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Thu, 11 Jan 2024 00:23:07 +0900 Subject: [PATCH 12/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`PrefectureMasterApi.server=5Furl`=E3=82=92public=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/prefecture_master_api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/prefecture_master_api.rs b/src/api/prefecture_master_api.rs index 1d791486..b4b9821c 100644 --- a/src/api/prefecture_master_api.rs +++ b/src/api/prefecture_master_api.rs @@ -2,7 +2,7 @@ use crate::entity::Prefecture; use crate::err::{ApiErrorKind, Error}; pub struct PrefectureMasterApi { - server_url: &'static str, + pub server_url: &'static str, } impl PrefectureMasterApi { From ad12d5e1a5b76c5fcef0d743adc5f43fb3c067b9 Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Thu, 11 Jan 2024 00:24:26 +0900 Subject: [PATCH 13/24] =?UTF-8?q?add:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`api::ApiImpl`=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/api.rs b/src/api.rs index 022dca3e..dc2bdb33 100644 --- a/src/api.rs +++ b/src/api.rs @@ -4,6 +4,8 @@ pub mod city_master_api; pub mod client; pub mod prefecture_master_api; +use crate::api::city_master_api::CityMasterApi; +use crate::api::prefecture_master_api::PrefectureMasterApi; use crate::entity::{City, Prefecture}; use crate::err::Error; use std::future::Future; @@ -20,6 +22,31 @@ pub trait Api { ) -> impl Future>; } +pub struct ApiImpl {} + +impl Api for ApiImpl { + fn get_prefecture_master( + &self, + prefecture_name: &str, + ) -> impl Future> { + let prefecture_master_api = PrefectureMasterApi { + server_url: "https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist", + }; + prefecture_master_api.get(prefecture_name) + } + + fn get_city_master( + &self, + prefecture_name: &str, + city_name: &str, + ) -> impl Future> { + let city_master_api = CityMasterApi { + server_url: "https://geolonia.github.io/japanese-addresses/api/ja", + }; + city_master_api.get(prefecture_name, city_name) + } +} + pub trait BlockingApi { fn get_prefecture_master(&self, prefecture_name: &str) -> Result; fn get_city_master(&self, prefecture_name: &str, city_name: &str) -> Result; From 10d9aa720bdc35dda6c75730d6326d2504cbedd7 Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Thu, 11 Jan 2024 00:27:25 +0900 Subject: [PATCH 14/24] =?UTF-8?q?add:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`api::BlockingApiImpl`=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/api.rs b/src/api.rs index dc2bdb33..2e5df99e 100644 --- a/src/api.rs +++ b/src/api.rs @@ -47,7 +47,28 @@ impl Api for ApiImpl { } } +#[cfg(not(target_arch = "wasm32"))] pub trait BlockingApi { fn get_prefecture_master(&self, prefecture_name: &str) -> Result; fn get_city_master(&self, prefecture_name: &str, city_name: &str) -> Result; } + +#[cfg(not(target_arch = "wasm32"))] +pub struct BlockingApiImpl {} + +#[cfg(not(target_arch = "wasm32"))] +impl BlockingApi for BlockingApiImpl { + fn get_prefecture_master(&self, prefecture_name: &str) -> Result { + let prefecture_master_api = PrefectureMasterApi { + server_url: "https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist", + }; + prefecture_master_api.get_blocking(prefecture_name) + } + + fn get_city_master(&self, prefecture_name: &str, city_name: &str) -> Result { + let city_master_api = CityMasterApi { + server_url: "https://geolonia.github.io/japanese-addresses/api/ja", + }; + city_master_api.get_blocking(prefecture_name, city_name) + } +} From 09b1807f100ab8d89d73b9b50e957688c022edf2 Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Thu, 11 Jan 2024 01:11:47 +0900 Subject: [PATCH 15/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`api::ApiImpl`=E3=81=AE=E3=83=A1=E3=83=B3=E3=83=90=E3=81=A8?= =?UTF-8?q?=E3=81=97=E3=81=A6PrefectureMasterApi=E3=81=A8CityMasterApi?= =?UTF-8?q?=E3=82=92=E6=8C=81=E3=81=A4=E3=82=88=E3=81=86=E3=81=AA=E5=AE=9F?= =?UTF-8?q?=E8=A3=85=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ライフタイムの関係でコンパイルができないため --- src/api.rs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/api.rs b/src/api.rs index 2e5df99e..63d75791 100644 --- a/src/api.rs +++ b/src/api.rs @@ -11,6 +11,7 @@ use crate::err::Error; use std::future::Future; pub trait Api { + fn new() -> Self; fn get_prefecture_master( &self, prefecture_name: &str, @@ -22,17 +23,29 @@ pub trait Api { ) -> impl Future>; } -pub struct ApiImpl {} +pub struct ApiImpl { + prefecture_master_api: PrefectureMasterApi, + city_master_api: CityMasterApi, +} impl Api for ApiImpl { + fn new() -> Self { + ApiImpl { + prefecture_master_api: PrefectureMasterApi { + server_url: + "https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist", + }, + city_master_api: CityMasterApi { + server_url: "https://geolonia.github.io/japanese-addresses/api/ja", + }, + } + } + fn get_prefecture_master( &self, prefecture_name: &str, ) -> impl Future> { - let prefecture_master_api = PrefectureMasterApi { - server_url: "https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist", - }; - prefecture_master_api.get(prefecture_name) + self.prefecture_master_api.get(prefecture_name) } fn get_city_master( @@ -40,10 +53,7 @@ impl Api for ApiImpl { prefecture_name: &str, city_name: &str, ) -> impl Future> { - let city_master_api = CityMasterApi { - server_url: "https://geolonia.github.io/japanese-addresses/api/ja", - }; - city_master_api.get(prefecture_name, city_name) + self.city_master_api.get(prefecture_name, city_name) } } From 60004936bcd410c52c518abd2621ff518a79d661 Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Thu, 11 Jan 2024 01:15:21 +0900 Subject: [PATCH 16/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`api::BlockingApiImpl`=E3=81=AE=E3=83=A1=E3=83=B3=E3=83=90?= =?UTF-8?q?=E3=81=A8=E3=81=97=E3=81=A6PrefectureMasterApi=E3=81=A8CityMast?= =?UTF-8?q?erApi=E3=82=92=E6=8C=81=E3=81=A4=E3=82=88=E3=81=86=E3=81=AA?= =?UTF-8?q?=E5=AE=9F=E8=A3=85=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ライフタイムの関係でコンパイルができないため --- src/api.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/api.rs b/src/api.rs index 63d75791..ea45cfd1 100644 --- a/src/api.rs +++ b/src/api.rs @@ -59,26 +59,35 @@ impl Api for ApiImpl { #[cfg(not(target_arch = "wasm32"))] pub trait BlockingApi { + fn new() -> Self; fn get_prefecture_master(&self, prefecture_name: &str) -> Result; fn get_city_master(&self, prefecture_name: &str, city_name: &str) -> Result; } #[cfg(not(target_arch = "wasm32"))] -pub struct BlockingApiImpl {} +pub struct BlockingApiImpl { + prefecture_master_api: PrefectureMasterApi, + city_master_api: CityMasterApi, +} #[cfg(not(target_arch = "wasm32"))] impl BlockingApi for BlockingApiImpl { + fn new() -> Self { + BlockingApiImpl { + prefecture_master_api: PrefectureMasterApi { + server_url: "https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist", + }, + city_master_api: CityMasterApi { + server_url: "https://geolonia.github.io/japanese-addresses/api/ja", + }, + } + } + fn get_prefecture_master(&self, prefecture_name: &str) -> Result { - let prefecture_master_api = PrefectureMasterApi { - server_url: "https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist", - }; - prefecture_master_api.get_blocking(prefecture_name) + self.prefecture_master_api.get_blocking(prefecture_name) } fn get_city_master(&self, prefecture_name: &str, city_name: &str) -> Result { - let city_master_api = CityMasterApi { - server_url: "https://geolonia.github.io/japanese-addresses/api/ja", - }; - city_master_api.get_blocking(prefecture_name, city_name) + self.city_master_api.get_blocking(prefecture_name, city_name) } } From 57888be0ee9f7af10557fc13a1393447be7712ea Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Thu, 11 Jan 2024 01:27:22 +0900 Subject: [PATCH 17/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`api::client::ApiImpl`=E3=82=92=E4=BD=BF=E7=94=A8=E3=81=97?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=81=9F=E7=AE=87=E6=89=80=E3=82=92`api::Api?= =?UTF-8?q?Impl`=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib.rs | 4 ++-- src/parser.rs | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f134afc6..e6f8692b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -use crate::api::client::ApiImpl; +use crate::api::{Api, ApiImpl}; use crate::entity::ParseResult; use wasm_bindgen::prelude::wasm_bindgen; use wasm_bindgen::JsValue; @@ -22,7 +22,7 @@ impl Parser { pub async fn parse(&self, address: &str) -> ParseResult { #[cfg(feature = "debug")] console_error_panic_hook::set_once(); - let api = ApiImpl {}; + let api = ApiImpl::new(); parser::parse(api, address).await } } diff --git a/src/parser.rs b/src/parser.rs index f1739f45..11eb6749 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -71,14 +71,14 @@ pub async fn parse(api: T, input: &str) -> ParseResult { #[cfg(test)] mod non_blocking_tests { - use crate::api::client::ApiImpl; + use crate::api::{Api, ApiImpl}; use crate::err::ParseErrorKind; use crate::parser::parse; use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure}; #[tokio::test] async fn 都道府県名が誤っている場合() { - let api = ApiImpl {}; + let api = ApiImpl::new(); let result = parse(api, "青盛県青森市長島1丁目1−1").await; assert_eq!(result.address.prefecture, ""); assert_eq!(result.address.city, ""); @@ -93,7 +93,7 @@ mod non_blocking_tests { #[tokio::test] async fn 市区町村名が誤っている場合() { - let api = ApiImpl {}; + let api = ApiImpl::new(); let result = parse(api, "青森県青盛市長島1丁目1−1").await; assert_eq!(result.address.prefecture, "青森県"); assert_eq!(result.address.city, ""); @@ -108,7 +108,7 @@ mod non_blocking_tests { #[tokio::test] async fn 町名が誤っている場合() { - let api = ApiImpl {}; + let api = ApiImpl::new(); let result = parse(api, "青森県青森市永嶋1丁目1−1").await; assert_eq!(result.address.prefecture, "青森県"); assert_eq!(result.address.city, "青森市"); @@ -125,7 +125,7 @@ mod non_blocking_tests { #[wasm_bindgen_test] async fn parse_wasm_success() { - let api = ApiImpl {}; + let api = ApiImpl::new(); let result = parse(api, "兵庫県淡路市生穂新島8番地").await; assert_eq!(result.address.prefecture, "兵庫県".to_string()); assert_eq!(result.address.city, "淡路市".to_string()); From 6ddca0200480d1aac72e96ba0280f4709471754f Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Thu, 11 Jan 2024 01:28:40 +0900 Subject: [PATCH 18/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`api::blocking::Client`=E3=82=92=E4=BD=BF=E7=94=A8=E3=81=97?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=81=9F=E7=AE=87=E6=89=80=E3=82=92`api::Blo?= =?UTF-8?q?ckingApiImpl`=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/parser.rs | 6 +++--- src/parser/read_city.rs | 5 ++--- src/parser/read_town.rs | 5 ++--- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 11eb6749..0d52cdc9 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -191,13 +191,13 @@ pub fn parse_blocking(api: T, input: &str) -> ParseResult { #[cfg(all(test, not(target_arch = "wasm32")))] mod blocking_tests { - use crate::api; + use crate::api::{BlockingApi, BlockingApiImpl}; use crate::err::ParseErrorKind; use crate::parser::parse_blocking; #[test] fn parse_blocking_success_埼玉県秩父市熊木町8番15号() { - let client = api::blocking::Client {}; + let client = BlockingApiImpl::new(); let result = parse_blocking(client, "埼玉県秩父市熊木町8番15号"); assert_eq!(result.address.prefecture, "埼玉県"); assert_eq!(result.address.city, "秩父市"); @@ -208,7 +208,7 @@ mod blocking_tests { #[test] fn parse_blocking_fail_市町村名が間違っている場合() { - let client = api::blocking::Client {}; + let client = BlockingApiImpl::new(); let result = parse_blocking(client, "埼玉県秩父柿熊木町8番15号"); assert_eq!(result.address.prefecture, "埼玉県"); assert_eq!(result.address.city, ""); diff --git a/src/parser/read_city.rs b/src/parser/read_city.rs index f3316a34..a5903bf4 100644 --- a/src/parser/read_city.rs +++ b/src/parser/read_city.rs @@ -23,8 +23,7 @@ pub fn read_city(input: &str, prefecture: Prefecture) -> Option<(String, String) #[cfg(all(test, not(target_arch = "wasm32")))] mod tests { - use crate::api::blocking::Client; - use crate::api::BlockingApi; + use crate::api::{BlockingApi, BlockingApiImpl}; use crate::parser::read_city::read_city; use test_case::test_case; @@ -40,7 +39,7 @@ mod tests { #[test_case("茨城県", "竜ヶ崎市佐貫町647", "龍ヶ崎市"; "success_竜ヶ崎市_表記ゆれ")] #[test_case("茨城県", "竜ケ崎市佐貫町647", "龍ヶ崎市"; "success_竜ケ崎市_表記ゆれ")] fn test_read_city(prefecture_name: &str, input: &str, expected: &str) { - let api = Client {}; + let api = BlockingApiImpl::new(); let prefecture = api.get_prefecture_master(prefecture_name).unwrap(); let (_, city_name) = read_city(input, prefecture).unwrap(); assert_eq!(city_name, expected); diff --git a/src/parser/read_town.rs b/src/parser/read_town.rs index 05d4d953..a903deb3 100644 --- a/src/parser/read_town.rs +++ b/src/parser/read_town.rs @@ -51,8 +51,7 @@ fn find_town(input: &String, city: &City) -> Option<(String, String)> { #[cfg(test)] #[cfg(not(target_arch = "wasm32"))] mod parser_tests { - use crate::api::blocking::Client; - use crate::api::BlockingApi; + use crate::api::{BlockingApi, BlockingApiImpl}; use crate::entity::{City, Town}; use crate::parser::read_town::read_town; @@ -147,7 +146,7 @@ mod parser_tests { #[test] fn read_town_丁目が算用数字の場合_京都府京都市東山区n丁目() { - let client = Client {}; + let client = BlockingApiImpl::new(); let city = client.get_city_master("京都府", "京都市東山区").unwrap(); let test_cases = vec![ ("本町1丁目45番", "本町一丁目"), From 16f4ce1e174ff42fab914fb7f8e0403f1a4d6338 Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Thu, 11 Jan 2024 01:20:00 +0900 Subject: [PATCH 19/24] =?UTF-8?q?delete:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`api/client.rs`,=20`api/blocking.rs`=E3=82=92=E5=89=8A?= =?UTF-8?q?=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用しないため --- src/api.rs | 3 -- src/api/blocking.rs | 115 ------------------------------------------ src/api/client.rs | 118 -------------------------------------------- 3 files changed, 236 deletions(-) delete mode 100644 src/api/blocking.rs delete mode 100644 src/api/client.rs diff --git a/src/api.rs b/src/api.rs index ea45cfd1..50587a30 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,7 +1,4 @@ -#[cfg(not(target_arch = "wasm32"))] -pub mod blocking; pub mod city_master_api; -pub mod client; pub mod prefecture_master_api; use crate::api::city_master_api::CityMasterApi; diff --git a/src/api/blocking.rs b/src/api/blocking.rs deleted file mode 100644 index 374d199a..00000000 --- a/src/api/blocking.rs +++ /dev/null @@ -1,115 +0,0 @@ -use crate::api::BlockingApi; -use crate::entity::{City, Prefecture, Town}; -use crate::err::{ApiErrorKind, Error}; - -pub struct Client {} - -impl BlockingApi for Client { - fn get_prefecture_master(&self, prefecture_name: &str) -> Result { - let endpoint = format!( - "https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist/{}/master.json", - prefecture_name - ); - let response = match reqwest::blocking::get(&endpoint) { - Ok(result) => result, - Err(_) => return Err(Error::new_api_error(ApiErrorKind::Fetch(endpoint))), - }; - if response.status() == 200 { - match response.json::() { - Ok(result) => Ok(result), - Err(_) => Err(Error::new_api_error(ApiErrorKind::Deserialize(endpoint))), - } - } else { - Err(Error::new_api_error(ApiErrorKind::Fetch(endpoint))) - } - } - - fn get_city_master(&self, prefecture_name: &str, city_name: &str) -> Result { - let endpoint = format!( - "https://geolonia.github.io/japanese-addresses/api/ja/{}/{}.json", - prefecture_name, city_name - ); - let response = match reqwest::blocking::get(&endpoint) { - Ok(result) => result, - Err(_) => return Err(Error::new_api_error(ApiErrorKind::Fetch(endpoint))), - }; - if response.status() == 200 { - match response.json::>() { - Ok(result) => Ok(City { - name: city_name.to_string(), - towns: result, - }), - Err(_) => Err(Error::new_api_error(ApiErrorKind::Deserialize(endpoint))), - } - } else { - Err(Error::new_api_error(ApiErrorKind::Fetch(endpoint))) - } - } -} - -#[cfg(test)] -mod blocking_client_tests { - use crate::api::blocking::Client; - use crate::api::BlockingApi; - use crate::entity::{Prefecture, Town}; - - #[test] - fn get_prefecture_master_成功_香川県() { - let client = Client {}; - let result = client.get_prefecture_master("香川県"); - assert!(result.is_ok()); - let prefecture = result.unwrap(); - assert_eq!( - prefecture, - Prefecture::new( - "香川県", - vec![ - "高松市", - "丸亀市", - "坂出市", - "善通寺市", - "観音寺市", - "さぬき市", - "東かがわ市", - "三豊市", - "小豆郡土庄町", - "小豆郡小豆島町", - "木田郡三木町", - "香川郡直島町", - "綾歌郡宇多津町", - "綾歌郡綾川町", - "仲多度郡琴平町", - "仲多度郡多度津町", - "仲多度郡まんのう町", - ], - ) - ); - } - - #[test] - fn get_prefecture_master_失敗_都道府県名が誤っている() { - let client = Client {}; - let result = client.get_prefecture_master("東京県"); - assert!(result.is_err()); - assert_eq!(result.unwrap_err().error_message, "https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist/東京県/master.jsonを取得できませんでした"); - } - - #[test] - fn get_city_master_成功_香川県坂出市() { - let client = Client {}; - let result = client.get_city_master("香川県", "坂出市"); - assert!(result.is_ok()); - let city = result.unwrap(); - assert_eq!(city.name, "坂出市"); - let town = Town::new("青葉町", "", 34.307609, 133.85252); - assert!(city.towns.contains(&town)); - } - - #[test] - fn get_city_master_失敗_市町村名が誤っている() { - let client = Client {}; - let result = client.get_city_master("東京都", "東京市"); - assert!(result.is_err()); - assert_eq!(result.unwrap_err().error_message, "https://geolonia.github.io/japanese-addresses/api/ja/東京都/東京市.jsonを取得できませんでした") - } -} diff --git a/src/api/client.rs b/src/api/client.rs deleted file mode 100644 index e1cca1d5..00000000 --- a/src/api/client.rs +++ /dev/null @@ -1,118 +0,0 @@ -use crate::api::Api; -use crate::entity::{City, Prefecture, Town}; -use crate::err::{ApiErrorKind, Error}; - -pub struct ApiImpl {} - -impl Api for ApiImpl { - async fn get_prefecture_master(&self, prefecture_name: &str) -> Result { - let endpoint = format!( - "https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist/{}/master.json", - prefecture_name - ); - let response = match reqwest::get(&endpoint).await { - Ok(result) => result, - Err(_) => return Err(Error::new_api_error(ApiErrorKind::Fetch(endpoint))), - }; - if response.status() == 200 { - match response.json::().await { - Ok(result) => Ok(result), - Err(_) => Err(Error::new_api_error(ApiErrorKind::Deserialize(endpoint))), - } - } else { - Err(Error::new_api_error(ApiErrorKind::Fetch(endpoint))) - } - } - - async fn get_city_master(&self, prefecture_name: &str, city_name: &str) -> Result { - let endpoint = format!( - "https://geolonia.github.io/japanese-addresses/api/ja/{}/{}.json", - prefecture_name, city_name - ); - let response = match reqwest::get(&endpoint).await { - Ok(result) => result, - Err(_) => return Err(Error::new_api_error(ApiErrorKind::Deserialize(endpoint))), - }; - if response.status() == 200 { - match response.json::>().await { - Ok(result) => Ok(City { - name: city_name.to_string(), - towns: result, - }), - Err(_) => Err(Error::new_api_error(ApiErrorKind::Deserialize(endpoint))), - } - } else { - Err(Error::new_api_error(ApiErrorKind::Fetch(endpoint))) - } - } -} - -#[cfg(test)] -mod api_tests { - use crate::api::client::ApiImpl; - use crate::api::Api; - use crate::entity::Town; - - #[tokio::test] - async fn get_prefecture_master_success() { - let api = ApiImpl {}; - let prefecture = api.get_prefecture_master("富山県").await.unwrap(); - assert_eq!(prefecture.name, "富山県".to_string()); - let cities = vec![ - "富山市".to_string(), - "高岡市".to_string(), - "魚津市".to_string(), - "氷見市".to_string(), - "滑川市".to_string(), - "黒部市".to_string(), - "砺波市".to_string(), - "小矢部市".to_string(), - "南砺市".to_string(), - "射水市".to_string(), - "中新川郡舟橋村".to_string(), - "中新川郡上市町".to_string(), - "中新川郡立山町".to_string(), - "下新川郡入善町".to_string(), - "下新川郡朝日町".to_string(), - ]; - for city in cities { - assert!(prefecture.cities.contains(&city)); - } - } - - #[tokio::test] - async fn get_prefecture_master_fail() { - let api = ApiImpl {}; - let result = api.get_prefecture_master("大阪都").await; - assert!(result.is_err()); - assert_eq!( - result.err().unwrap().error_message, - "https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist/大阪都/master.jsonを取得できませんでした".to_string() - ); - } - - #[tokio::test] - async fn get_city_master_success() { - let api = ApiImpl {}; - let city = api.get_city_master("石川県", "羽咋郡志賀町").await.unwrap(); - assert_eq!(city.name, "羽咋郡志賀町".to_string()); - let town = Town { - name: "末吉".to_string(), - koaza: "千古".to_string(), - lat: Some(37.006235), - lng: Some(136.779155), - }; - assert!(city.towns.contains(&town)); - } - - #[tokio::test] - async fn get_city_master_fail() { - let api = ApiImpl {}; - let result = api.get_city_master("石川県", "敦賀市").await; - assert!(result.is_err()); - assert_eq!( - result.err().unwrap().error_message, - "https://geolonia.github.io/japanese-addresses/api/ja/石川県/敦賀市.jsonを取得できませんでした".to_string() - ); - } -} From 2b83939fda48c8f906bf1a8fd379d4bc799a88d3 Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Fri, 12 Jan 2024 18:56:27 +0900 Subject: [PATCH 20/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20`prefecture=5Fmaster=5Fapi`,=20`city=5Fmaster=5Fapi`?= =?UTF-8?q?=E3=82=92public=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/api.rs b/src/api.rs index 50587a30..71e7df51 100644 --- a/src/api.rs +++ b/src/api.rs @@ -21,8 +21,8 @@ pub trait Api { } pub struct ApiImpl { - prefecture_master_api: PrefectureMasterApi, - city_master_api: CityMasterApi, + pub prefecture_master_api: PrefectureMasterApi, + pub city_master_api: CityMasterApi, } impl Api for ApiImpl { @@ -72,7 +72,8 @@ impl BlockingApi for BlockingApiImpl { fn new() -> Self { BlockingApiImpl { prefecture_master_api: PrefectureMasterApi { - server_url: "https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist", + server_url: + "https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist", }, city_master_api: CityMasterApi { server_url: "https://geolonia.github.io/japanese-addresses/api/ja", @@ -85,6 +86,7 @@ impl BlockingApi for BlockingApiImpl { } fn get_city_master(&self, prefecture_name: &str, city_name: &str) -> Result { - self.city_master_api.get_blocking(prefecture_name, city_name) + self.city_master_api + .get_blocking(prefecture_name, city_name) } } From b7b936a3daeb0a179fa512270ceae4cd50a23494 Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Fri, 12 Jan 2024 18:56:53 +0900 Subject: [PATCH 21/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20=E9=83=BD=E9=81=93=E5=BA=9C=E7=9C=8C=E3=83=9E=E3=82=B9?= =?UTF-8?q?=E3=82=BF=E3=81=8C=E5=8F=96=E5=BE=97=E3=81=A7=E3=81=8D=E3=81=AA?= =?UTF-8?q?=E3=81=84=E5=A0=B4=E5=90=88=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88?= =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/parser.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/parser.rs b/src/parser.rs index 0d52cdc9..fd583134 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -71,6 +71,7 @@ pub async fn parse(api: T, input: &str) -> ParseResult { #[cfg(test)] mod non_blocking_tests { + use crate::api::prefecture_master_api::PrefectureMasterApi; use crate::api::{Api, ApiImpl}; use crate::err::ParseErrorKind; use crate::parser::parse; @@ -91,6 +92,21 @@ mod non_blocking_tests { ); } + #[tokio::test] + async fn 都道府県マスタが取得できない場合() { + let mut api = ApiImpl::new(); + api.prefecture_master_api = PrefectureMasterApi { + server_url: "https://example.com/invalid_url/api/", + }; + + let result = parse(api, "青森県青森市長島1丁目1−1").await; + assert_eq!(result.error.is_some(), true); + assert_eq!(result.address.prefecture, "青森県"); + assert_eq!(result.address.city, ""); + assert_eq!(result.address.town, ""); + assert_eq!(result.address.rest, "青森市長島1丁目1−1"); + } + #[tokio::test] async fn 市区町村名が誤っている場合() { let api = ApiImpl::new(); From a780a12fb2ac66fb4d370dbc83010d33abab4056 Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Fri, 12 Jan 2024 18:59:53 +0900 Subject: [PATCH 22/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20=E5=B8=82=E5=8C=BA=E7=94=BA=E6=9D=91=E3=83=9E=E3=82=B9?= =?UTF-8?q?=E3=82=BF=E3=81=8C=E5=8F=96=E5=BE=97=E3=81=A7=E3=81=8D=E3=81=AA?= =?UTF-8?q?=E3=81=84=E5=A0=B4=E5=90=88=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88?= =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/parser.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/parser.rs b/src/parser.rs index fd583134..fade4809 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -71,6 +71,7 @@ pub async fn parse(api: T, input: &str) -> ParseResult { #[cfg(test)] mod non_blocking_tests { + use crate::api::city_master_api::CityMasterApi; use crate::api::prefecture_master_api::PrefectureMasterApi; use crate::api::{Api, ApiImpl}; use crate::err::ParseErrorKind; @@ -122,6 +123,21 @@ mod non_blocking_tests { ); } + #[tokio::test] + async fn 市区町村マスタが取得できない場合() { + let mut api = ApiImpl::new(); + api.city_master_api = CityMasterApi { + server_url: "https://example.com/invalid_url/api/", + }; + + let result = parse(api, "青森県青森市長島1丁目1−1").await; + assert_eq!(result.error.is_some(), true); + assert_eq!(result.address.prefecture, "青森県"); + assert_eq!(result.address.city, "青森市"); + assert_eq!(result.address.town, ""); + assert_eq!(result.address.rest, "長島1丁目1−1"); + } + #[tokio::test] async fn 町名が誤っている場合() { let api = ApiImpl::new(); From 2fe1c9c9fa10a1bdbfbf501f3423b29f68db67c5 Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Fri, 12 Jan 2024 19:15:38 +0900 Subject: [PATCH 23/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20=E9=9D=9E=E5=90=8C=E6=9C=9F=E7=89=88=E3=81=AE=E3=82=B5?= =?UTF-8?q?=E3=83=B3=E3=83=97=E3=83=AB=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 729d6e44..cb98b8ae 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,12 @@ cargo add japanese-address-parser ### Async Api ```rust -use japanese_address_parser::api::client::ApiImpl; +use japanese_address_parser::api::{Api, ApiImpl}; use japanese_address_parser::parser::parse; #[tokio::main] async fn main() { - let async_api = ApiImpl {}; + let async_api = ApiImpl::new(); let parse_result = parse(async_api, "東京都千代田区丸の内1-1-1").await; println!("{:?}", parse_result); } From b4997239a30e1cce4e1dd972cdaf25a322c2651b Mon Sep 17 00:00:00 2001 From: Yuuki Toriyama Date: Fri, 12 Jan 2024 19:15:58 +0900 Subject: [PATCH 24/24] =?UTF-8?q?update:=20API=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?:=20=E5=90=8C=E6=9C=9F=E7=89=88=E3=81=AE=E3=82=B5=E3=83=B3?= =?UTF-8?q?=E3=83=97=E3=83=AB=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cb98b8ae..e2d48924 100644 --- a/README.md +++ b/README.md @@ -32,11 +32,11 @@ async fn main() { ### Blocking Api ```rust -use japanese_address_parser::api::blocking::Client; +use japanese_address_parser::api::{BlockingApi, BlockingApiImpl}; use japanese_address_parser::parser::parse_blocking; fn main() { - let blocking_api = Client {}; + let blocking_api = BlockingApiImpl::new(); let parse_result = parse_blocking(blocking_api, "東京都千代田区丸の内1-1-1"); println!("{:?}", parse_result); }