A simple wrapper type for String
s that ensures that the string inside is not .empty()
, meaning that the length > 0.
```rust use nonemptystring::NonEmptyString;
// Constructing it from a normal (non-zero-length) String works fine. let s = "A string with a length".toowned(); assert!(NonEmptyString::new(s).isok());
// But constructing it from a non-zero-length String results in an Err
, where we get the String
back that we passed in.
let empty = "".toowned();
let result = NonEmptyString::new(empty);
assert!(result.iserr());
asserteq!(result.unwraperr(), "".to_owned())
```
NonEmptyString
implements a subset of the functions of std::string::String
, only the ones which are guaranteed to leave the NonEmptyString
in a valid state.
This means i.e. push()
is implemented, but pop()
is not.
This allows you to mostly treat it as a String without having to constantly turn it into the inner String
before you can do any sort of operation on it and then having to reconstruct a NonEmptyString
afterwards.
[serde] support is available behind the serde
feature flag:
toml
[dependencies]
serde = { version = "1", features = ["derive"] }
non-empty-string = { version = "*", features = ["serde"]}
Afterwards you can use it in a struct: ```rust use serde::{Serialize, Deserialize}; use nonemptystring::NonEmptyString;
struct MyApiObject { username: NonEmptyString, } ```
Deserialization will fail if the field is present as a String, but the length of the String
is 0.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.