Skip to content

Commit

Permalink
Merge pull request #1 from YuukiToriyama/feature/round-up-if-address-…
Browse files Browse the repository at this point in the history
…cannot-be-parsed

一致する地名が存在しない場合クラッシュさせないようにした
  • Loading branch information
YuukiToriyama authored Nov 23, 2023
2 parents 485bfdf + 56c7a99 commit f826552
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
11 changes: 11 additions & 0 deletions src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ pub struct ParsedAddress {
pub town: String,
pub rest: String,
}

impl ParsedAddress {
pub fn new(prefecture_name: &str, city_name: &str, town_name: &str, rest_name: &str) -> Self {
ParsedAddress {
prefecture: prefecture_name.to_string(),
city: city_name.to_string(),
town: town_name.to_string(),
rest: rest_name.to_string(),
}
}
}
52 changes: 43 additions & 9 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,29 @@ mod read_town;

pub async fn parse<T: Api>(api: T, input: &str) -> ParsedAddress {
// 都道府県を特定
let (rest, prefecture_name) = read_prefecture(input).unwrap();
let (rest, prefecture_name) = match read_prefecture(input) {
None => return ParsedAddress::new("", "", "", input),
Some(result) => result,
};
// その都道府県の市町村名リストを取得
let prefecture = api.get_prefecture_master(prefecture_name).await.unwrap();
// 市町村名を特定
let (rest, city_name) = read_city(rest, prefecture).unwrap();
let (rest, city_name) = match read_city(rest, prefecture) {
None => return ParsedAddress::new(prefecture_name, "", "", rest),
Some(result) => result,
};
// その市町村の町名リストを取得
let city = api
.get_city_master(prefecture_name, city_name)
.await
.unwrap();
// 町名を特定
let (rest, town_name) = read_town(rest, city).unwrap();
let (rest, town_name) = match read_town(rest, city) {
None => return ParsedAddress::new(prefecture_name, city_name, "", rest),
Some(result) => result,
};

ParsedAddress {
prefecture: prefecture_name.to_string(),
city: city_name.to_string(),
town: town_name.to_string(),
rest: rest.to_string(),
}
ParsedAddress::new(prefecture_name, city_name, town_name, rest)
}

#[cfg(test)]
Expand Down Expand Up @@ -58,6 +62,36 @@ mod parser_tests {
assert_eq!(address.rest, "100-1".to_string());
}

#[tokio::test]
async fn parse_mocked_fail_都道府県名が間違っている場合() {
let api = ApiMock { should_fail: false };
let address = parse(api, "神奈側県平塚市桜ケ丘100-1").await;
assert_eq!(address.prefecture, "".to_string());
assert_eq!(address.city, "".to_string());
assert_eq!(address.town, "".to_string());
assert_eq!(address.rest, "神奈側県平塚市桜ケ丘100-1".to_string());
}

#[tokio::test]
async fn parse_mocked_fail_市町村名が間違っている場合() {
let api = ApiMock { should_fail: false };
let address = parse(api, "神奈川県平束市桜ケ丘100-1").await;
assert_eq!(address.prefecture, "神奈川県".to_string());
assert_eq!(address.city, "".to_string());
assert_eq!(address.town, "".to_string());
assert_eq!(address.rest, "平束市桜ケ丘100-1".to_string());
}

#[tokio::test]
async fn parse_mocked_fail_町名が間違っている場合() {
let api = ApiMock { should_fail: false };
let address = parse(api, "神奈川県平塚市新百合ヶ丘100-1").await;
assert_eq!(address.prefecture, "神奈川県".to_string());
assert_eq!(address.city, "平塚市".to_string());
assert_eq!(address.town, "".to_string());
assert_eq!(address.rest, "新百合ヶ丘100-1".to_string());
}

wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
Expand Down

0 comments on commit f826552

Please sign in to comment.