https://github.com/octaltree/strong
Strongly typed String for Rust
https://github.com/octaltree/strong
Last synced: 6 months ago
JSON representation
Strongly typed String for Rust
- Host: GitHub
- URL: https://github.com/octaltree/strong
- Owner: octaltree
- License: mit
- Created: 2021-02-20T10:22:03.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-04-10T16:28:09.000Z (over 4 years ago)
- Last Synced: 2024-10-07T14:35:01.613Z (about 1 year ago)
- Language: Rust
- Homepage:
- Size: 66.4 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# STRONG [](https://crates.io/crates/strong) 
Strongly typed String for Rust
Rust is a statically and strongly typed systems programming language. We can create a new type wrapping primitives.
```rust
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`.
```rust
use strong::{validators::Email, Strong, StrongBuf, Validator};
fn login(email: &Strong, password: &Strong) { .. }
let email: StrongBuf = ..
let password: StrongBuf = ..
login(&email, &password);
```
`Email` requires `some_validators` feature.
## Getting Started
```rust
use strong::{validators::Email, Strong, StrongBuf, Validator};
enum Password {}
impl Validator for Password {
type Err = std::convert::Infallible;
}
let email: StrongBuf = StrongBuf::::validate("a@example.com".into()).unwrap();
let password: &Strong = Strong::::validate("b").unwrap();
```
### Shorthand
With `shorthand` feature, `Str` and `S` are exported and can be substituted for `StrongBuf` and `Strong`.
```rust
let email: StrongBuf = StrongBuf::validate("foo".to_string()).unwrap();
let email: Str = Str::validate("foo".to_string()).unwrap();
```
## License
Licensed under MIT license ([LICENSE-MIT](LICENSE) or https://opensource.org/licenses/MIT)
### Contributing
welcome!