Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add words-to-number module #40

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ chrono = { version = "0.4.31", optional = true }

# Edit `Makefile` and `src/lib.src` after making changes in this section:
[features]
# For now, by default we enable all features:
default = ["full"]
full = [
"add-ordinal-suffix",
"commas",
Expand All @@ -47,8 +45,12 @@ full = [
"time-ago",
"get-place-by-iran-national-id",
"half-space",
"legal-id"
]
"legal-id",
"words-to-number",
] # For now, by default we enable all features:


default = ["full"]
add-ordinal-suffix = []
commas = []
digits = []
Expand All @@ -67,8 +69,8 @@ extract-card-number = []
time-ago = ["dep:thiserror", "dep:chrono"]
get-place-by-iran-national-id = ["dep:thiserror"]
half-space = []
legal-id= ["dep:thiserror"]

legal-id = ["dep:thiserror"]
words-to-number = ["dep:thiserror", "commas", "digits", "remove-ordinal-suffix"]
[package.metadata.docs.rs]
all-features = true

Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fmt:
cargo fmt


build: full default add-ordinal-suffix commas digits find-capital-by-province persian-chars national-id remove-ordinal-suffix url-fix verity-card-number time-ago phone-number bill number-to-words get-bank-name-by-card-number extract-card-number get-place-by-iran-national-id half-space legal-id
build: full default add-ordinal-suffix commas digits find-capital-by-province persian-chars national-id remove-ordinal-suffix url-fix verity-card-number time-ago phone-number bill number-to-words get-bank-name-by-card-number extract-card-number get-place-by-iran-national-id half-space legal-id words-to-number

check: clippy lint

Expand Down Expand Up @@ -126,4 +126,7 @@ half-space:
legal-id:
@ echo ""
cargo build --no-default-features --features=legal-id
words-to-number:
@ echo ""
cargo build --no-default-features --features=words-to-number
@ ls -sh target/debug/*.rlib
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
feature = "get-place-by-iran-national-id",
feature = "half-space",
feature = "legal-id",
feature = "words-to-number",
)))]
compile_error!("No available Cargo feature is included");

Expand Down Expand Up @@ -73,3 +74,6 @@ pub mod half_space;

#[cfg(feature = "legal-id")]
pub mod legal_id;

#[cfg(feature = "words-to-number")]
pub mod words_to_number;
71 changes: 71 additions & 0 deletions src/words_to_number/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
pub(super) const NEGATIVE_PREFIX: &str = "منفی";

pub(super) static UNITS: &[(&str, i64)] = &[
("صفر", 0),
("یک", 1),
("دو", 2),
("سه", 3),
("چهار", 4),
("پنج", 5),
("شش", 6),
("شیش", 6),
("هفت", 7),
("هشت", 8),
("نه", 9),
("ده", 10),
("یازده", 11),
("دوازده", 12),
("سیزده", 13),
("چهارده", 14),
("پانزده", 15),
("شانزده", 16),
("هفده", 17),
("هجده", 18),
("نوزده", 19),
("بیست", 20),
("سی", 30),
("چهل", 40),
("پنجاه", 50),
("شصت", 60),
("هفتاد", 70),
("هشتاد", 80),
("نود", 90),
("صد", 100),
("یکصد", 100),
("دویست", 200),
("سیصد", 300),
("چهارصد", 400),
("پانصد", 500),
("ششصد", 600),
("هفتصد", 700),
("هشتصد", 800),
("نهصد", 900),
];

pub(super) static MAGNITUDE: &[(&str, i64)] = &[
("هزار", 1000),
("میلیون", 1000000),
("بیلیون", 1000000000),
("میلیارد", 1000000000),
("تریلیون", 1000000000000),
];

// pub(super) static ALL_WORDS: &[(&str, i64)] = UNITS.iter().chain(MAGNITUDE.iter());

pub(super) fn get_unit_number(unit: &str) -> Option<&i64> {
let result = UNITS
.iter()
.find(|(key, _)| key == &unit)
.map(|(_, details)| details);

result
}

pub(super) fn get_magnitute_number(unit: &str) -> Option<&i64> {
let result = MAGNITUDE
.iter()
.find(|(key, _)| key == &unit)
.map(|(_, details)| details);

result
}
9 changes: 9 additions & 0 deletions src/words_to_number/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use thiserror::Error;

#[derive(Error, Debug, PartialEq, Eq)]
pub enum WordsToNumberError {
#[error("There is a invalid unit in the input")]
InvalidUnit,
#[error("The input cannot be a empty string")]
EmptyInput,
}
Loading