nepali-phone is a Rust crate for parsing and validating Nepali phone numbers.
It detects whether a number is mobile or landline, identifies the mobile operator, and resolves the geographic district / area from the STD area code for all 77 districts acrbss Nepal's 7 provinces.
use nepal_phone::parse;
let p = parse("9841234567").unwrap();
println!("{}", p);
// Mobile(9841234567, Nepal Telecom)
let map = p.to_map();
// {"type": "mobile", "number": "9841234567", "operator": "Nepal Telecom"}
let p = parse("+977-142314819").unwrap();
println!("{}", p);
// Landline(0142314819, code=01, areas=[Kathmandu, Lalitpur, Bhaktapur])Rust >= 1.70
Add to your Cargo.toml:
[dependencies]
nepali-phone = "0.1"use nepal_phone::{parse, is_valid, is_mobile_number, is_landline_number};Checks whether the given string is a valid Nepali phone number of any kind.
nepal_phone::is_valid("9851377890"); // true
nepal_phone::is_valid("+977-142314819"); // true
nepal_phone::is_valid("8251377890"); // false
nepal_phone::is_valid("abc"); // falsenepal_phone::is_mobile_number("9841234567"); // true
nepal_phone::is_mobile_number("+977-9842536789"); // true
nepal_phone::is_mobile_number("9779841234567"); // true
nepal_phone::is_mobile_number("0142314819"); // falsenepal_phone::is_landline_number("0142314819"); // true
nepal_phone::is_landline_number("+977-142314819"); // true
nepal_phone::is_landline_number("9841234567"); // falseParses a phone number and returns a [PhoneNumber] on success, or None if the number is invalid or unrecognised.
Numbers with a +977 / 977 country-code prefix and hyphens are handled automatically.
use nepal_phone::parse;
parse("9851377890");
// Some(Mobile { number: "9851377890", operator: NepalTelecom })
parse("+977-142314819");
// Some(Landline { number: "0142314819", area_code: "01", areas: [Kathmandu, Lalitpur, Bhaktapur] })
parse("abc"); // None
parse("12345"); // None
parse(""); // NoneReturns the parsed number as a HashMap<&str, String> — useful for serialisation or passing data across API boundaries.
Mobile keys: type, number, operator
let map = nepal_phone::parse("9851377890").unwrap().to_map();
// {
// "type": "mobile",
// "number": "9851377890",
// "operator": "Nepal Telecom"
// }Landline keys: type, number, area_code, areas (comma-separated)
let map = nepal_phone::parse("+977-142314819").unwrap().to_map();
// {
// "type": "landline",
// "number": "0142314819",
// "area_code": "01",
// "areas": "Kathmandu, Lalitpur, Bhaktapur"
// }Mobile numbers are matched to one of five operators based on their prefix:
| Operator | Operator variant |
Prefixes |
|---|---|---|
| Nepal Telecom | Operator::NepalTelecom |
984, 985, 986, 974, 975 |
| Ncell | Operator::Ncell |
980, 981, 982 |
| Smart Cell | Operator::SmartCell |
961, 962, 988 |
| UTL | Operator::Utl |
972 |
| Hello Mobile | Operator::HelloMobile |
963 |
Operator implements Display:
println!("{}", nepal_phone::Operator::NepalTelecom); // "Nepal Telecom"
println!("{}", nepal_phone::Operator::Ncell); // "Ncell"Provides STD area-code-to-district mapping for all 77 districts across Nepal's 7 provinces.
use nepal_phone::{AREAS, areas_by_code, Area};Look up districts by area code
nepal_phone::areas_by_code("01");
// [Bhaktapur (01), Kathmandu (01), Lalitpur (01)]
nepal_phone::areas_by_code("061");
// [Kaski (061)]
nepal_phone::areas_by_code("000"); // [] — unknown codeIterate all districts
for area in nepal_phone::AREAS {
println!("{area}"); // e.g. "Kathmandu (01)", "Kaski (061)", ...
}Area implements Display as "<name> (<area_code>)":
let a = nepal_phone::Area { name: "Pokhara Metro", area_code: "061" };
println!("{a}"); // "Pokhara Metro (061)"Feedback and contributions are welcome. Please open an issue or pull request on GitHub.