Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jonhteper/phone-type
Phone type for Rust
https://github.com/jonhteper/phone-type
Last synced: about 1 month ago
JSON representation
Phone type for Rust
- Host: GitHub
- URL: https://github.com/jonhteper/phone-type
- Owner: jonhteper
- License: mit
- Created: 2022-12-06T20:21:18.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-26T19:36:48.000Z (5 months ago)
- Last Synced: 2024-12-31T03:32:50.679Z (about 1 month ago)
- Language: Rust
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Phone-type
[![Crates.io](https://shields.io/crates/v/phone_type.svg)](https://crates.io/crates/phone_type)
This crate contains the `Phone` type, who is only a String wrapper and uses
[phone-number-verifier](https://crates.io/crates/phone-number-verifier) to valid the phone's format.## Install
Add in `Cargo.toml`:
```toml
phone_type = "0.3.0"
```Or run in your project directory:
```bash
cargo add phone_type
```## Examples
Use in structure:
```rust
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:
```rust
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:
```toml
phone_type = {version = "0.3.0", default-features = false}
```### Example
```rust
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_json).unwrap();
assert_eq!(&deserialize_result, &contact);
}
```