Expand description
Type-safe primitives for constrained and reliability-oriented values.
reliakit-primitives provides small owned wrapper types for values that
should satisfy common constraints before they move through an application or
library boundary.
The crate is useful when a public API should accept a validated value instead
of an unchecked String, integer, or float. Constructors validate once at the
boundary and then carry the invariant in the type.
The crate has no dependencies and forbids unsafe code.
§Examples
Basic service configuration:
use reliakit_primitives::{NonEmptyStr, Port};
fn configure(name: NonEmptyStr, port: Port) {
assert_eq!(name.as_str(), "api");
assert_eq!(port.get(), 8080);
}
let name = NonEmptyStr::new("api")?;
let port = Port::new(8080)?;
configure(name, port);Text and structured values:
use reliakit_primitives::{Email, HumanDuration, HttpUrl, SemVer};
let contact = Email::new("ops@example.com")?;
let healthcheck = HttpUrl::new("https://example.com/health")?;
let version = SemVer::parse("1.2.3-beta.1")?;
let timeout = HumanDuration::parse("30s")?;
assert_eq!(contact.domain(), "example.com");
assert!(healthcheck.is_https());
assert!(version.is_pre_release());
assert_eq!(timeout.as_secs(), 30);§Feature flags
std(default) enablesstd::error::ErrorforPrimitiveErrorand impliesalloc.allocenables the allocation-backed owned types and collection helpers listed below.
§no_std
The crate supports no_std. Building with --no-default-features (no std,
no alloc) provides the allocation-free primitives:
- numeric:
Percent,PercentageF64,Port,PositiveInt,PositiveFloat,ByteSize, Uuid,MacAddress, andHumanDuration(parsing andDisplaydo not allocate),- the error types (
PrimitiveError,PrimitiveErrorKind,PrimitiveResult).
Enabling the alloc feature additionally provides the owned, allocation-backed
types: Slug, Email, HttpUrl, HexString, Base64,
Identifier, Hostname, NonEmptyStr, BoundedStr, NonEmptyVec,
and SemVer. The default std build enables alloc for normal application
use.
Re-exports§
pub use bounded::BoundedStr;pub use collections::NonEmptyVec;pub use duration::HumanDuration;pub use error::PrimitiveError;pub use error::PrimitiveErrorKind;pub use error::PrimitiveResult;pub use mac::MacAddress;pub use non_empty::NonEmptyStr;pub use numeric::ByteSize;pub use numeric::Percent;pub use numeric::PercentageF64;pub use numeric::Port;pub use numeric::PositiveFloat;pub use numeric::PositiveInt;pub use semver::SemVer;pub use text::Base64;pub use text::Email;pub use text::HexString;pub use text::Hostname;pub use text::HttpUrl;pub use text::Identifier;pub use text::Slug;pub use uuid::Uuid;
Modules§
- bounded
- Bounded string primitive.
- collections
- Non-empty vector primitive.
- duration
- Human-readable duration primitive.
- error
- Shared primitive error type.
- mac
- MAC address primitive.
- non_
empty - Non-empty string primitive.
- numeric
- Numeric primitives.
- semver
- Semantic version primitive.
- text
- Text validation primitives.
- uuid
- UUID primitive.