Skip to content

Commit

Permalink
Merge pull request #346 from YuukiToriyama/release/v0.1.5
Browse files Browse the repository at this point in the history
release/v0.1.5をmainブランチにマージ
  • Loading branch information
YuukiToriyama authored Jul 13, 2024
2 parents b7e2a45 + b762575 commit fb4a3ed
Show file tree
Hide file tree
Showing 13 changed files with 659 additions and 417 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ members = [
resolver = "2"

[workspace.package]
version = "0.1.4"
version = "0.1.5"
edition = "2021"
description = "A Rust Library to parse japanese addresses."
repository = "https://github.com/YuukiToriyama/japanese-address-parser"
Expand Down
1 change: 0 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ reqwest = { version = "0.12.3", default-features = false, features = ["json", "r
serde = { version = "1.0.192", features = ["derive"] }

[dev-dependencies]
test-case = "3.3.1"
tokio = { version = "1.38.0", features = ["rt", "macros"] }
wasm-bindgen-test = { workspace = true }
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ pub mod api;
pub mod entity;
mod err;
pub mod parser;
mod tokenizer;
mod util;
115 changes: 61 additions & 54 deletions core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ use crate::api::AsyncApi;
use crate::api::BlockingApi;
use crate::entity::{Address, ParseResult};
use crate::err::{Error, ParseErrorKind};
use crate::parser::read_city::read_city;
use crate::parser::read_prefecture::read_prefecture;
use crate::parser::read_town::read_town;
use crate::tokenizer::Tokenizer;

mod adapter;
mod filter;
mod read_city;
pub(crate) mod adapter;
pub(crate) mod filter;
mod read_house_number;
mod read_prefecture;
mod read_town;

impl<T> From<Tokenizer<T>> for Address {
fn from(value: Tokenizer<T>) -> Self {
Self {
prefecture: value.prefecture_name.unwrap_or("".to_string()),
city: value.city_name.unwrap_or("".to_string()),
town: value.town_name.unwrap_or("".to_string()),
rest: value.rest,
}
}
}

/// An asynchronous `Parser` to process addresses.
///
Expand Down Expand Up @@ -69,56 +75,61 @@ impl Parser {
///
/// publicにしていますが、直接の使用は推奨されません。[Parser]の利用を検討してください。
pub async fn parse(api: Arc<AsyncApi>, input: &str) -> ParseResult {
let tokenizer = Tokenizer::new(input);
// 都道府県を特定
let (rest, prefecture_name) = if let Some(result) = read_prefecture(input) {
result
} else {
let Ok(tokenizer) = tokenizer.read_prefecture() else {
return ParseResult {
address: Address::new("", "", "", input),
address: Address::from(tokenizer),
error: Some(Error::new_parse_error(ParseErrorKind::Prefecture)),
};
};
// その都道府県の市町村名リストを取得
let prefecture = match api.get_prefecture_master(prefecture_name).await {
let prefecture = match api
.get_prefecture_master(tokenizer.prefecture_name.as_ref().unwrap())
.await
{
Err(error) => {
return ParseResult {
address: Address::new(prefecture_name, "", "", rest),
address: Address::from(tokenizer),
error: Some(error),
};
}
Ok(result) => result,
};
// 市町村名を特定
let (rest, city_name) = if let Some(result) = read_city(rest, prefecture) {
result
} else {
let Ok(tokenizer) = tokenizer.read_city(prefecture.cities) else {
return ParseResult {
address: Address::new(prefecture_name, "", "", rest),
address: Address::from(tokenizer),
error: Some(Error::new_parse_error(ParseErrorKind::City)),
};
};
// その市町村の町名リストを取得
let city = match api.get_city_master(prefecture_name, &city_name).await {
let city = match api
.get_city_master(
tokenizer.prefecture_name.as_ref().unwrap(),
tokenizer.city_name.as_ref().unwrap(),
)
.await
{
Err(error) => {
return ParseResult {
address: Address::new(prefecture_name, &city_name, "", &rest),
address: Address::from(tokenizer),
error: Some(error),
};
}
Ok(result) => result,
};
// 町名を特定
let (rest, town_name) = if let Some(result) = read_town(&rest, &city) {
result
} else {
let Ok(tokenizer) = tokenizer.read_town(city.towns.iter().map(|x| x.name.clone()).collect())
else {
return ParseResult {
address: Address::new(prefecture_name, &city_name, "", &rest),
address: Address::from(tokenizer),
error: Some(Error::new_parse_error(ParseErrorKind::Town)),
};
};

ParseResult {
address: Address::new(prefecture_name, &city_name, &town_name, &rest),
address: Address::from(tokenizer),
error: None,
}
}
Expand Down Expand Up @@ -226,54 +237,50 @@ mod tests {
/// publicにしていますが、直接の使用は推奨されません。[Parser]の利用を検討してください。
#[cfg(feature = "blocking")]
pub fn parse_blocking(api: Arc<BlockingApi>, input: &str) -> ParseResult {
let (rest, prefecture_name) = match read_prefecture(input) {
None => {
return ParseResult {
address: Address::new("", "", "", input),
error: Some(Error::new_parse_error(ParseErrorKind::Prefecture)),
};
}
Some(result) => result,
let tokenizer = Tokenizer::new(input);
let Ok(tokenizer) = tokenizer.read_prefecture() else {
return ParseResult {
address: Address::from(tokenizer),
error: Some(Error::new_parse_error(ParseErrorKind::Prefecture)),
};
};
let prefecture = match api.get_prefecture_master(prefecture_name) {
let prefecture = match api.get_prefecture_master(tokenizer.prefecture_name.as_ref().unwrap()) {
Err(error) => {
return ParseResult {
address: Address::new(prefecture_name, "", "", rest),
address: Address::from(tokenizer),
error: Some(error),
};
}
Ok(result) => result,
};
let (rest, city_name) = match read_city(rest, prefecture) {
None => {
return ParseResult {
address: Address::new(prefecture_name, "", "", rest),
error: Some(Error::new_parse_error(ParseErrorKind::City)),
};
}
Some(result) => result,
let Ok(tokenizer) = tokenizer.read_city(prefecture.cities) else {
return ParseResult {
address: Address::from(tokenizer),
error: Some(Error::new_parse_error(ParseErrorKind::City)),
};
};
let city = match api.get_city_master(prefecture_name, &city_name) {
let city = match api.get_city_master(
tokenizer.prefecture_name.as_ref().unwrap(),
tokenizer.city_name.as_ref().unwrap(),
) {
Err(error) => {
return ParseResult {
address: Address::new(prefecture_name, &city_name, "", &rest),
address: Address::from(tokenizer),
error: Some(error),
};
}
Ok(result) => result,
};
let (rest, town_name) = match read_town(&rest, &city) {
None => {
return ParseResult {
address: Address::new(prefecture_name, &city_name, "", &rest),
error: Some(Error::new_parse_error(ParseErrorKind::Town)),
};
}
Some(result) => result,
let Ok(tokenizer) = tokenizer.read_town(city.towns.iter().map(|x| x.name.clone()).collect())
else {
return ParseResult {
address: Address::from(tokenizer),
error: Some(Error::new_parse_error(ParseErrorKind::Town)),
};
};

ParseResult {
address: Address::new(prefecture_name, &city_name, &town_name, &rest),
address: Address::from(tokenizer),
error: None,
}
}
Expand Down
2 changes: 2 additions & 0 deletions core/src/parser/adapter/orthographical_variant_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub trait OrthographicalVariants {
const: Variant;
const: Variant;
const: Variant;
const: Variant;
}

impl OrthographicalVariants for Variant {
Expand All @@ -53,6 +54,7 @@ impl OrthographicalVariants for Variant {
const: Variant = &["穂", "穗"];
const: Variant = &["梼", "檮"];
const: Variant = &["葛󠄀", "葛"];
const: Variant = &["蛍", "螢"];
}

pub struct OrthographicalVariantAdapter {
Expand Down
86 changes: 0 additions & 86 deletions core/src/parser/read_city.rs

This file was deleted.

79 changes: 0 additions & 79 deletions core/src/parser/read_prefecture.rs

This file was deleted.

Loading

0 comments on commit fb4a3ed

Please sign in to comment.