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.

```rust 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.

```rust use aliribraid::braid; use compactstr::CompactString as String;

[braid]

pub struct UserId; ```

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

```rust fn takestrongstring(n: DatabaseName) {} fn borrowstrongstring(n: &DatabaseNameRef) {}

let owned = DatabaseName::new(String::from("mongo")); borrowstrongstring(&owned); takestrongstring(owned); ```

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

```rust fn takerawstring(s: String) {} fn borrowrawstr(s: &str) {}

let owned = DatabaseName::new(String::from("mongo")); borrowrawstr(owned.asstr()); takeraw_string(owned.take()); ```

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