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

#2428 in Rust patterns

Download history 308/week @ 2024-12-10 165/week @ 2024-12-17 119/week @ 2024-12-24 169/week @ 2024-12-31 133/week @ 2025-01-07 168/week @ 2025-01-14 140/week @ 2025-01-21 158/week @ 2025-01-28 206/week @ 2025-02-04 165/week @ 2025-02-11 121/week @ 2025-02-18 208/week @ 2025-02-25 161/week @ 2025-03-04 252/week @ 2025-03-11 363/week @ 2025-03-18 605/week @ 2025-03-25

1,423 downloads per month
Used in 2 crates (via playwright)

MIT license

29KB
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.8MB
~34K SLoC