Skip to main content

pactum_annotations/
lib.rs

1//! Declarative field normalization and validation for pactum contracts.
2//!
3//! This crate is IO-free. Hosts and codecs call `Normalize` and `Validate` around
4//! serialization; they do not live here.
5//!
6//! ```rust
7//! use pactum_annotations::{trim, check_email};
8//!
9//! let mut email = "  User@Example.com ".to_string();
10//! trim(&mut email);
11//! assert_eq!(email, "User@Example.com");
12//! assert!(check_email("email", &email).is_ok());
13//! ```
14
15mod checks;
16mod civil_date;
17mod context;
18mod email;
19mod error;
20mod formats;
21mod norms;
22mod phone;
23mod schema;
24mod traits;
25
26#[doc(hidden)]
27pub use async_trait as __async_trait;
28
29pub use checks::{
30    alphanumeric, ascii, bool_is_true, digits, ends_with, finite_f64, list_len, list_unique,
31    list_unique_strings, non_empty, non_negative_i64, number_f64, number_i64, one_of, positive_i64,
32    prefix_nested, starts_with, string_len, timestamp_ms, LengthUnit, NumericValue,
33};
34pub use civil_date::{check_rfc3339, check_ymd, CivilDate};
35pub use context::ValidationContext;
36pub use email::check_email;
37pub use error::{AsyncValidationError, FieldIssue, HostError};
38pub use formats::{
39    check_charset, check_hex, check_hostname, check_ip, check_ipv4, check_ipv6, check_semver,
40    check_slug, check_url, check_uuid,
41};
42pub use norms::{clamp_f64, clamp_i64, lowercase, strip_whitespace, trim, uppercase, ClampNum};
43pub use phone::{check_phone_e164, check_phone_locale, dial_plan, DialPlan, DIAL_PLANS};
44pub use schema::{CommandSpec, FieldSchema, InvariantSpec, ModelKind, ModelSchema, VariantSchema};
45pub use traits::{
46    AsyncCheckOutcome, AsyncCheckResult, AsyncValidate, CheckOutcome, Client, DescribeModel,
47    IntoAsyncError, Normalize, ScopedValidate, Server, Validate,
48};
49
50/// Derive `Normalize`, `Validate`, and `DescribeModel` from `#[norm]` / `#[check]` / `#[constraint]`.
51pub use pactum_annotations_macros::Model;
52
53/// Alias for [`Model`] when disambiguation is preferred.
54pub use pactum_annotations_macros::PactumModel;
55
56/// Mark a pure function as a reusable field rule (`#[check(rule = …)]`).
57pub use pactum_annotations_macros::pactum_rule;
58
59/// Define composable, schema-aware normalizer + check pipelines.
60pub use pactum_annotations_macros::define_rule;
61
62/// Helper used by generated constraint code.
63pub fn constraint_issue(field: &str, code: &str, reason: &str) -> FieldIssue {
64    FieldIssue::new(field, code, reason)
65}