| Crates.io | strong |
| lib.rs | strong |
| version | 0.3.4 |
| created_at | 2021-02-20 11:52:35.8068+00 |
| updated_at | 2021-04-10 16:31:20.844005+00 |
| description | Strongly typed String |
| homepage | |
| repository | https://github.com/octaltree/strong |
| max_upload_size | |
| id | 357979 |
| size | 30,135 |
Strongly typed String for Rust
Rust is a statically and strongly typed systems programming language. We can create a new type wrapping primitives.
struct Age(i32);
struct Email(String);
There is a problem here, there are two types of strings in Rust, and it is hard to create strong types for both String and &str.
STRONG provides two types owned StrongBuf and unsized Strong.
use strong::{validators::Email, Strong, StrongBuf, Validator};
fn login(email: &Strong<Email>, password: &Strong<Password>) { .. }
let email: StrongBuf<Email> = ..
let password: StrongBuf<Password> = ..
login(&email, &password);
Email requires some_validators feature.
use strong::{validators::Email, Strong, StrongBuf, Validator};
enum Password {}
impl Validator for Password {
type Err = std::convert::Infallible;
}
let email: StrongBuf<Email> = StrongBuf::<Email>::validate("a@example.com".into()).unwrap();
let password: &Strong<Password> = Strong::<Password>::validate("b").unwrap();
With shorthand feature, Str and S are exported and can be substituted for StrongBuf and Strong.
let email: StrongBuf<Email> = StrongBuf::validate("foo".to_string()).unwrap();
let email: Str<Email> = Str::validate("foo".to_string()).unwrap();
Licensed under MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
welcome!