Crates.io | ph-mobile-network |
lib.rs | ph-mobile-network |
version | 0.1.4 |
source | src |
created_at | 2024-04-27 15:25:04.804001 |
updated_at | 2024-04-28 12:05:32.469257 |
description | A library for identifying mobile network operators in the Philippines based on phone number prefixes. |
homepage | https://github.com/codeitlikemiley/ph-mobile-network |
repository | https://github.com/codeitlikemiley/ph-mobile-network |
max_upload_size | |
id | 1222696 |
size | 52,001 |
The PH Mobile Network Library provides a robust solution for identifying and validating mobile network operators based on phone number prefixes in the Philippines. It supports networks such as Globe, Smart, Sun, TNT, and Dito, and ensures comprehensive validation against common input errors.
Features
Network Identification: Identify the network operator for a given phone number based on its prefix.
Ability to Reset Specific Mobile Network to Empty Sets
Appending Mobile Network Prefixes on Compile Time
Input Validation: Validate phone numbers for correct length and numeric consistency.
Error Handling: Detailed error responses for various types of input and internal processing issues.
Add this to your Cargo.toml
:
[dependencies]
ph-mobile-network = "0.1.4"
Usage
To use the library, include it in your Rust file and utilize the MobileNetwork::get function to determine the network of a phone number and validate it:
examples/main.rs
use ph_mobile_network::mobile_network::MobileNetwork;
use ph_mobile_network::mutate::{append_dito_prefixes, append_globe_prefixes, append_smart_prefixes, append_sun_prefixes, append_tnt_prefixes, reset_dito_prefixes, reset_globe_prefixes, reset_smart_prefixes, reset_sun_prefixes, reset_tnt_prefixes};
use ph_mobile_network::validate::Validate;
fn main() {
// if for some reason the default library prefixes has invalid values you can always reset it
reset_dito_prefixes();
reset_globe_prefixes();
reset_smart_prefixes();
reset_sun_prefixes();
reset_tnt_prefixes();
// if the current prefix isnt supported yet by the library you can always append it on compile time
// append new prefixes on different networks
append_dito_prefixes(&["0911", "0912"]); // Adding new TNT prefixes
append_globe_prefixes(&["0917", "0996"]); // Adding new Globe prefixes
append_smart_prefixes(&["0918", "0919"]); // Adding new Smart prefixes
append_sun_prefixes(&["0933", "0934"]); // Adding new Sun prefixes
append_tnt_prefixes(&["0899", "0900"]); // Adding new Dito prefixes
// Example phone number to validate
let number = "09171234567";
// Get the network based on the phone number and validate the number
match MobileNetwork::get(number) {
Ok(network) => {
println!("Network identified: {}", network.to_string());
match network.validate(number) {
Ok(valid) => println!("Validation result: {}", valid),
Err(e) => println!("Validation error: {:?}", e),
}
},
Err(e) => println!("Error retrieving network: {:?}", e),
}
}
run: cargo run --example main
output:
cargo run --example main
Compiling ph-mobile-network v0.1.1 (/Users/uriah/Code/ph-mobile-network)
Finished dev [unoptimized + debuginfo] target(s) in 0.25s
Running `target/debug/examples/main`
Network identified: globe
Validation result: true
Error Handling
The library defines several errors to manage possible failure scenarios:
These errors are encapsulated in the MobileNetworkError
enum.
Errors are handled using Rust's robust error handling features, allowing for detailed debugging and recovery options.
Note: there might be missing , prefix or invalid prefix on the library ,if there is please submit a PR , all the prefixes are define on ./src/globals
Contributions are welcome! If you would like to contribute, please fork the repository and submit a pull request.
git checkout -b missing-prefix/xxxx
)git commit -am 'Add xxxx prefix on MobileNetwork::{Variants}'
)git push origin missing-prefix/xxxx
)This project is licensed under the MIT License - see the LICENSE file for details.