Skip to main content

reliakit_primitives/
lib.rs

1//! Type-safe primitives for constrained and reliability-oriented values.
2//!
3//! `reliakit-primitives` provides small owned wrapper types for values that
4//! should satisfy common constraints before they move through an application or
5//! library boundary.
6//!
7//! The crate is useful when a public API should accept a validated value instead
8//! of an unchecked `String`, integer, or float. Constructors validate once at the
9//! boundary and then carry the invariant in the type.
10//!
11//! The crate has no dependencies and forbids unsafe code.
12//!
13//! # Examples
14//!
15//! Basic service configuration:
16//!
17//! ```
18//! use reliakit_primitives::{NonEmptyStr, Port};
19//!
20//! fn configure(name: NonEmptyStr, port: Port) {
21//!     assert_eq!(name.as_str(), "api");
22//!     assert_eq!(port.get(), 8080);
23//! }
24//!
25//! let name = NonEmptyStr::new("api")?;
26//! let port = Port::new(8080)?;
27//!
28//! configure(name, port);
29//! # Ok::<(), reliakit_primitives::PrimitiveError>(())
30//! ```
31//!
32//! Text and structured values:
33//!
34//! ```
35//! use reliakit_primitives::{Email, HumanDuration, HttpUrl, SemVer};
36//!
37//! let contact = Email::new("ops@example.com")?;
38//! let healthcheck = HttpUrl::new("https://example.com/health")?;
39//! let version = SemVer::parse("1.2.3-beta.1")?;
40//! let timeout = HumanDuration::parse("30s")?;
41//!
42//! assert_eq!(contact.domain(), "example.com");
43//! assert!(healthcheck.is_https());
44//! assert!(version.is_pre_release());
45//! assert_eq!(timeout.as_secs(), 30);
46//! # Ok::<(), reliakit_primitives::PrimitiveError>(())
47//! ```
48//!
49//! # Feature flags
50//!
51//! - `std` enables `std::error::Error` for [`PrimitiveError`] and is enabled by
52//!   default.
53//! - `alloc` enables allocation-backed types without `std`.
54//!
55//! # `no_std`
56//!
57//! The crate supports `no_std` when default features are disabled and `alloc` is
58//! enabled for string-backed and vector-backed primitives.
59
60#![cfg_attr(not(feature = "std"), no_std)]
61#![forbid(unsafe_code)]
62#![warn(missing_docs)]
63
64extern crate alloc;
65
66/// Bounded string primitive.
67pub mod bounded;
68/// Non-empty vector primitive.
69pub mod collections;
70/// Human-readable duration primitive.
71pub mod duration;
72/// Shared primitive error type.
73pub mod error;
74/// Non-empty string primitive.
75pub mod non_empty;
76/// Numeric primitives.
77pub mod numeric;
78/// Semantic version primitive.
79pub mod semver;
80/// Text validation primitives.
81pub mod text;
82/// UUID primitive.
83pub mod uuid;
84
85pub use bounded::BoundedStr;
86pub use collections::NonEmptyVec;
87pub use duration::HumanDuration;
88pub use error::{PrimitiveError, PrimitiveErrorKind, PrimitiveResult};
89pub use non_empty::NonEmptyStr;
90pub use numeric::{ByteSize, Percent, PercentageF64, Port, PositiveFloat, PositiveInt};
91pub use semver::SemVer;
92pub use text::{Email, HexString, HttpUrl, Slug};
93pub use uuid::Uuid;