11 releases

0.3.4 Apr 10, 2021
0.3.3 Feb 28, 2021
0.2.2 Feb 21, 2021
0.1.2 Feb 20, 2021

#832 in Rust patterns

Download history 80/week @ 2022-11-27 78/week @ 2022-12-04 74/week @ 2022-12-11 113/week @ 2022-12-18 200/week @ 2022-12-25 534/week @ 2023-01-01 537/week @ 2023-01-08 779/week @ 2023-01-15 496/week @ 2023-01-22 485/week @ 2023-01-29 361/week @ 2023-02-05 619/week @ 2023-02-12 648/week @ 2023-02-19 1074/week @ 2023-02-26 619/week @ 2023-03-05 609/week @ 2023-03-12

3,066 downloads per month
Used in playwright

MIT license

27KB
636 lines

STRONG   crates.io MIT

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.

Getting Started

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();

Shorthand

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();

License

Licensed under MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)

Contributing

welcome!

Dependencies

~0–1.2MB
~30K SLoC