phone_type

Crates.iophone_type
lib.rsphone_type
version0.3.0
sourcesrc
created_at2022-12-06 23:07:22.678821
updated_at2023-04-13 19:41:56.10474
descriptionPhone type for Rust
homepagehttps://github.com/jonhteper/phone-type
repositoryhttps://github.com/jonhteper/phone-type
max_upload_size
id731464
size9,714
JP (jonhteper)

documentation

README

Phone-type

Crates.io

This crate contains the Phone type, who is only a String wrapper and uses phone-number-verifier to valid the phone's format.

Install

Add in Cargo.toml:

phone_type = "0.3.0"

Or run in your project directory:

cargo add phone_type

Examples

Use in structure:

use phone_type::*;

struct ContactInformation {
    pub name: String,
    pub age: i8,
    pub phone: Phone,
}

fn main() {
    let info = ContactInformation {
        name: "John Doe".to_string(),
        age: 33,
        phone: Phone::new("111 111 1111").unwrap(),
    };
    /*...*/
}

Force type in constructor:

use phone_type::*;

struct ContactInformation {
    name: String,
    age: i8,
    phone: String,
}

impl ContactInformation {
    pub fn new(name: String, age: i8, phone: Phone) -> Self {
        Self {
            name,
            age,
            phone: phone.to_string(),
        }
    }
}


fn main() {
    let info = ContactInformation::new(
        "John Doe".to_string(),
        33,
        Phone::new("111 111 1111").unwrap(),
    );
    /*...*/
}

Serde support

The serde support is available behind serde feature, and is actived by default. If you dont want this feature, use:

phone_type = {version = "0.3.0", default-features = false}

Example

use serde::{Serialize, Deserialize};
use serde_json::json;

use crate::*;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Contact {
    pub name: String,
    pub phone: Phone,
}

fn main() {
    let contact_json = json!({
        "name": "John Doe",
        "phone": "111 111 1111"
    });

    let contact = Contact {
        name: "John Doe".to_string(),
        phone: Phone::new("111 111 1111").unwrap(),
    };

    let deserialize_result = serde_json::from_value::<Contact>(contact_json).unwrap();

    assert_eq!(&deserialize_result, &contact);

}
Commit count: 12

cargo fmt