#non-empty #string #str #non-zero

non-empty-string

A simple type for non empty Strings, similar to NonZeroUsize and friends

7 releases

0.2.5 Oct 15, 2024
0.2.4 Oct 3, 2023
0.2.3 Nov 27, 2022
0.2.2 Sep 18, 2022
0.1.0 Sep 25, 2021

#858 in Rust patterns

Download history 1826/week @ 2024-08-22 1567/week @ 2024-08-29 1981/week @ 2024-09-05 1759/week @ 2024-09-12 1557/week @ 2024-09-19 1954/week @ 2024-09-26 2256/week @ 2024-10-03 2643/week @ 2024-10-10 3363/week @ 2024-10-17 2056/week @ 2024-10-24 2432/week @ 2024-10-31 2379/week @ 2024-11-07 2204/week @ 2024-11-14 1864/week @ 2024-11-21 2046/week @ 2024-11-28 1953/week @ 2024-12-05

8,658 downloads per month

MIT/Apache

22KB
383 lines

Non Empty String

Crates.io Documentation

A simple wrapper type for Strings that ensures that the string inside is not .empty(), meaning that the length > 0.

Example

use non_empty_string::NonEmptyString;

// Constructing it from a normal (non-zero-length) String works fine.
let s = "A string with a length".to_owned();
assert!(NonEmptyString::new(s).is_ok());

// But constructing it from a zero-length String results in an `Err`, where we get the `String` back that we passed in.
let empty = "".to_owned();
let result = NonEmptyString::new(empty);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), "".to_owned())

Methods of std::string::String

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.

If a method is missing that you think upholds the invariant of NonEmptyString, don't hesitate to open an issue.

Traits

NonEmptyString implements quite a few of the traits that String implements, where it simply forwards it to the underlying string, which allows e.g. indexing with ranges:

use non_empty_string::NonEmptyString;

let non_empty = NonEmptyString::new("ABC".to_owned()).unwrap();
assert_eq!(&non_empty[1..], "BC");

If a trait is missing that you think upholds the invariant of NonEmptyString, don't hesitate to open an issue.

Serde Support

serde support is available behind the serde feature flag:

[dependencies]
serde = { version = "1", features = ["derive"] }
non-empty-string = { version = "*", features = ["serde"]}

Afterwards you can use it in a struct:

use serde::{Serialize, Deserialize};
use non_empty_string::NonEmptyString;

#[derive(Serialize, Deserialize)]
struct MyApiObject {
  username: NonEmptyString,
}

Deserialization will fail if the field is present as a String, but the length of the String is 0.

License

Licensed under either of

at your option.

Contribution

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.

Dependencies

~1.5MB
~39K SLoC