#string #validation #newtype #normalization #wrapper #reduce-boilerplate

no-std aliri_braid

Improve and strengthen your strings by making them strongly-typed with less boilerplate

18 releases

0.4.0 May 26, 2023
0.3.1 Nov 2, 2022
0.2.4 Jul 21, 2022
0.2.3 Jun 15, 2022
0.1.5 May 20, 2021

#349 in Rust patterns

Download history 26492/week @ 2024-07-26 27483/week @ 2024-08-02 25338/week @ 2024-08-09 22927/week @ 2024-08-16 25497/week @ 2024-08-23 20676/week @ 2024-08-30 26660/week @ 2024-09-06 24822/week @ 2024-09-13 28791/week @ 2024-09-20 30620/week @ 2024-09-27 28020/week @ 2024-10-04 30389/week @ 2024-10-11 31863/week @ 2024-10-18 31182/week @ 2024-10-25 29968/week @ 2024-11-01 30143/week @ 2024-11-08

128,963 downloads per month
Used in 20 crates (16 directly)

MIT/Apache

47KB
245 lines

aliri_braid

Build Status

Improve and strengthen your strings

Strongly-typed APIs reduce errors and confusion over passing around un-typed strings. Braid helps in that endeavor by making it painless to create wrappers around your string values, ensuring that you use them in the right way every time.

Examples of the documentation and implementations provided for braids are available below and in the aliri_braid_examples crate documentation.

Usage

A braid is created by attaching #[braid] to a struct definition. The macro will take care of automatically updating the representation of the struct to wrap a string and generate the borrowed form of the strong type.

use aliri_braid::braid;

#[braid]
pub struct DatabaseName;

Braids of custom string types are also supported, so long as they implement a set of expected traits. If not specified, the type named String in the current namespace will be used.

use aliri_braid::braid;
use compact_str::CompactString as String;

#[braid]
pub struct UserId;

Once created, braids can be passed around as strongly-typed, immutable strings.

fn take_strong_string(n: DatabaseName) {}
fn borrow_strong_string(n: &DatabaseNameRef) {}

let owned = DatabaseName::new(String::from("mongo"));
borrow_strong_string(&owned);
take_strong_string(owned);

A braid can also be untyped for use in stringly-typed interfaces.

fn take_raw_string(s: String) {}
fn borrow_raw_str(s: &str) {}

let owned = DatabaseName::new(String::from("mongo"));
borrow_raw_str(owned.as_str());
take_raw_string(owned.take());

For more information, see the documentation on docs.rs.

Dependencies

~255–710KB
~17K SLoC