rust_phone_number_geo/
lib.rs

1use crate::algorithm::binary_search_algorithm::BinarySearchAlgorithm;
2use crate::algorithm::lookup_algorithm::LookupAlgorithm;
3use crate::algorithm::PhoneNoInfo;
4use crate::enums::error_response::ErrorResponse;
5
6mod algorithm;
7mod enums;
8
9pub struct PhoneNoInfoUtils {}
10
11impl PhoneNoInfoUtils {
12    /// # 描述:获取手机号码运营商及归属地信息
13    /// 说明:该方法内部采用的是懒加载的方式加载手机号码数据源到内存中,懒加载内部机制保证了仅会加载一次,所以无需担心高并发请求会出现多次加载的情况。
14    /// 同理,除了首次调用会读取数据源文件,再次调用将直接读取内存数据,所以无需担心性能问题。
15    /// 返回的结果正常示例:PhoneNoInfo { province: "上海", city: "上海", zip_code: "200000", area_code: "021", card_type: "中国联通" }
16    /// 返回结果的异常示例:Response { desc: "无法匹配手机号" },当desc的值是“内部错误”时,证明程序存在不可恢复的错误,请联系作者。
17    /// 等等
18    /// # 使用实例:
19    /// ```
20    /// use rust_phone_number_geo::PhoneNoInfoUtils;
21    /// let result = PhoneNoInfoUtils::get_phone_info(None,"18501763350");
22    /// if result.is_ok() {
23    ///     // 正常处理业务逻辑
24    ///     println!("{:?}",result.unwrap());
25    /// }else {
26    ///     println!("{:?}",result.err().unwrap());
27    /// }
28    /// ```
29    pub fn get_phone_info(_algorithm_name: Option<String>,phone_number: &str) -> Result<PhoneNoInfo,ErrorResponse> {
30        return BinarySearchAlgorithm::lookup(phone_number);
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use crate::PhoneNoInfoUtils;
37
38    #[test]
39    fn test_find_phone() {
40        let result = PhoneNoInfoUtils::get_phone_info(None,"1111111");
41        if result.is_ok() {
42            println!("{:?}",result.unwrap());
43        }else {
44            println!("{:?}",result.err().unwrap());
45        }
46    }
47}
48
49