Skip to main content

api_bones/
lib.rs

1//! # api-bones
2//!
3//! Opinionated REST API types: errors (RFC 9457), pagination, health checks, and more.
4//!
5//! ## `no_std` support
6//!
7//! This crate is `#![no_std]` when the default `std` feature is disabled.
8//!
9//! | Features enabled         | Available types                                    |
10//! |--------------------------|-----------------------------------------------------|
11//! | *(none)*                 | Pure-`core` types: `ErrorCode`, `HealthStatus`, `PaginationParams`, `SortDirection` |
12//! | `alloc`                  | All types that use `String`/`Vec`/`Arc`             |
13//! | `std` *(default)*        | Full feature set including `HashMap`-backed types   |
14//!
15//! ```toml
16//! # no_std + alloc (WASM, embedded with allocator)
17//! api-bones = { version = "...", default-features = false, features = ["alloc"] }
18//!
19//! # pure no_std (core types only)
20//! api-bones = { version = "...", default-features = false }
21//! ```
22//!
23//! ## Core type: [`ApiError`]
24//!
25//! Every service serializes errors into [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457)
26//! Problem Details format:
27//!
28//! ```json
29//! {
30//!   "type": "urn:api-bones:error:resource-not-found",
31//!   "title": "Resource Not Found",
32//!   "status": 404,
33//!   "detail": "Booking 123 not found"
34//! }
35//! ```
36//!
37//! ```rust
38//! use api_bones::{ApiError, ErrorCode};
39//!
40//! fn find_booking(id: u64) -> Result<(), ApiError> {
41//!     Err(ApiError::not_found(format!("booking {id} not found")))
42//! }
43//! ```
44//!
45//! ## Feature flags (selection)
46//!
47//! | Feature    | What it enables                                      |
48//! |------------|------------------------------------------------------|
49//! | `schemars` | [`schemars::JsonSchema`] derive on all public types  |
50//! | `utoipa`   | [`utoipa::ToSchema`] derive on all public types      |
51//!
52//! Enable `schemars` in your `Cargo.toml`:
53//!
54//! ```toml
55//! api-bones = { version = "3", features = ["schemars"] }
56//! ```
57//!
58//! ## Add as dependency
59//!
60//! ```toml
61//! [dependencies]
62//! api-bones = "3"
63//! ```
64
65#![cfg_attr(not(feature = "std"), no_std)]
66
67// When `std` is not available but `alloc` is, bring the alloc crate into scope.
68// Under `std`, the `alloc` crate is re-exported by `std` so no explicit import
69// is needed.
70#[cfg(all(not(feature = "std"), feature = "alloc"))]
71extern crate alloc;
72
73// Modules that require heap allocation (String / Vec / Arc).
74#[cfg(any(feature = "std", feature = "alloc"))]
75pub mod audit;
76#[cfg(any(feature = "std", feature = "alloc"))]
77pub mod bulk;
78#[cfg(any(feature = "std", feature = "alloc"))]
79pub mod cache;
80#[cfg(all(any(feature = "std", feature = "alloc"), feature = "uuid"))]
81pub mod correlation_id;
82#[cfg(any(feature = "std", feature = "alloc"))]
83pub mod cors;
84#[cfg(feature = "base64")]
85pub mod cursor;
86#[cfg(any(feature = "std", feature = "alloc"))]
87pub mod deprecated;
88#[cfg(any(feature = "std", feature = "alloc"))]
89pub mod etag;
90#[cfg(any(feature = "std", feature = "alloc"))]
91pub mod header_id;
92#[cfg(all(any(feature = "std", feature = "alloc"), feature = "uuid"))]
93pub mod idempotency;
94#[cfg(any(feature = "std", feature = "alloc"))]
95pub mod links;
96#[cfg(all(any(feature = "std", feature = "alloc"), feature = "uuid"))]
97pub mod org_id;
98#[cfg(any(feature = "std", feature = "alloc"))]
99pub mod range;
100#[cfg(all(any(feature = "std", feature = "alloc"), feature = "uuid"))]
101pub mod request_id;
102#[cfg(any(feature = "std", feature = "alloc"))]
103pub mod response;
104#[cfg(any(feature = "std", feature = "alloc"))]
105pub mod slug;
106#[cfg(all(any(feature = "std", feature = "alloc"), feature = "uuid"))]
107pub mod traceparent;
108#[cfg(any(feature = "std", feature = "alloc"))]
109pub mod url;
110#[cfg(any(feature = "std", feature = "alloc"))]
111pub mod vary;
112pub mod version;
113
114// Modules available in all configurations; individual types inside are gated
115// where they require `alloc` or `std`.
116pub mod common;
117pub mod error;
118pub mod health;
119pub mod method;
120pub mod pagination;
121pub mod query;
122pub mod ratelimit;
123pub mod retry;
124pub mod status;
125
126#[cfg(any(feature = "std", feature = "alloc"))]
127pub mod content_type;
128
129#[cfg(feature = "http")]
130pub mod header;
131
132#[cfg(all(feature = "serde", any(feature = "std", feature = "alloc")))]
133pub mod serde;
134
135#[cfg(feature = "fake")]
136mod fake_impls;
137
138#[cfg(feature = "icalendar")]
139pub mod calendar;
140
141// OpenAPI helpers: Example<T> and DeprecatedField (issues #119, #120).
142pub mod openapi;
143
144pub mod has_id;
145pub use has_id::HasId;
146
147#[cfg(any(feature = "std", feature = "alloc"))]
148pub use audit::{
149    AuditInfo, DeviceLease, DeviceLeaseKind, Principal, PrincipalId, PrincipalKind,
150    PrincipalParseError, ResolvedPrincipal,
151};
152#[cfg(any(feature = "std", feature = "alloc"))]
153pub use bulk::{BulkItemResult, BulkRequest, BulkResponse};
154#[cfg(feature = "uuid")]
155pub use common::ResourceId;
156#[cfg(feature = "uuid")]
157pub use common::new_resource_id;
158#[cfg(feature = "chrono")]
159pub use common::parse_timestamp;
160// Timestamp is chrono::DateTime when chrono is on (no alloc needed),
161// or String when chrono is off (needs alloc or std).
162#[cfg(any(feature = "std", feature = "alloc"))]
163pub use cache::CacheControl;
164#[cfg(any(feature = "chrono", feature = "std", feature = "alloc"))]
165pub use common::Timestamp;
166#[cfg(any(feature = "std", feature = "alloc"))]
167pub use content_type::ContentType;
168#[cfg(all(any(feature = "std", feature = "alloc"), feature = "uuid"))]
169pub use correlation_id::{CorrelationId, CorrelationIdError};
170#[cfg(any(feature = "std", feature = "alloc"))]
171pub use cors::{CorsHeaders, CorsOrigin};
172#[cfg(feature = "base64")]
173pub use cursor::{Cursor, CursorError};
174#[cfg(any(feature = "std", feature = "alloc"))]
175pub use deprecated::Deprecated;
176pub use error::ErrorCode;
177#[cfg(any(feature = "std", feature = "alloc"))]
178pub use error::ErrorTypeMode;
179#[cfg(any(feature = "std", feature = "alloc"))]
180pub use error::HttpError;
181#[cfg(all(any(feature = "std", feature = "alloc"), feature = "serde"))]
182pub use error::ProblemJson;
183#[cfg(any(feature = "std", feature = "alloc"))]
184pub use error::{ApiError, ValidationError};
185#[cfg(feature = "std")]
186pub use error::{error_type_mode, set_error_type_mode, urn_namespace};
187#[cfg(any(feature = "std", feature = "alloc"))]
188pub use etag::{ETag, IfMatch, IfNoneMatch};
189#[cfg(feature = "http")]
190pub use header::{HeaderName, HeaderValue};
191#[cfg(any(feature = "std", feature = "alloc"))]
192pub use header_id::HeaderId;
193pub use health::HealthStatus;
194#[cfg(feature = "std")]
195pub use health::ReadinessResponse;
196#[cfg(any(feature = "std", feature = "alloc"))]
197pub use health::{HealthCheck, LivenessResponse};
198#[cfg(all(any(feature = "std", feature = "alloc"), feature = "uuid"))]
199pub use idempotency::{IdempotencyKey, IdempotencyKeyError};
200#[cfg(any(feature = "std", feature = "alloc"))]
201pub use links::{Link, Links};
202pub use method::HttpMethod;
203#[cfg(all(
204    any(feature = "std", feature = "alloc"),
205    feature = "uuid",
206    feature = "http"
207))]
208pub use org_id::OrgIdHeaderError;
209#[cfg(all(any(feature = "std", feature = "alloc"), feature = "uuid"))]
210pub use org_id::{OrgId, OrgIdError, OrgPath};
211pub use pagination::PaginationParams;
212#[cfg(any(feature = "std", feature = "alloc"))]
213pub use pagination::{
214    CursorPaginatedResponse, CursorPagination, CursorPaginationParams, PaginatedResponse,
215};
216#[cfg(any(feature = "std", feature = "alloc"))]
217pub use pagination::{KeysetPaginatedResponse, KeysetPaginationParams};
218pub use query::SortDirection;
219#[cfg(any(feature = "std", feature = "alloc"))]
220pub use query::{FilterEntry, FilterParams, SearchParams, SortParams};
221#[cfg(any(feature = "std", feature = "alloc"))]
222pub use range::{ByteRange, ContentRange, ParseRangeError, RangeHeader};
223#[cfg(any(feature = "std", feature = "alloc"))]
224pub use ratelimit::RateLimitInfo;
225#[cfg(all(any(feature = "std", feature = "alloc"), feature = "uuid"))]
226pub use request_id::{RequestId, RequestIdError, RequestIdParseError};
227#[cfg(any(feature = "std", feature = "alloc"))]
228pub use response::{ApiResponse, ApiResponseBuilder, ResponseMeta};
229pub use retry::{BackoffStrategy, Idempotent, RetryPolicy};
230#[cfg(any(feature = "std", feature = "alloc"))]
231pub use retry::{RetryAfter, RetryAfterParseError};
232#[cfg(any(feature = "std", feature = "alloc"))]
233pub use slug::{Slug, SlugError};
234pub use status::StatusCode;
235#[cfg(all(any(feature = "std", feature = "alloc"), feature = "uuid"))]
236pub use traceparent::{SamplingFlags, SpanId, TraceContext, TraceContextError, TraceId};
237#[cfg(any(feature = "std", feature = "alloc"))]
238pub use url::{QueryBuilder, UrlBuilder};
239#[cfg(any(feature = "std", feature = "alloc"))]
240pub use vary::Vary;
241pub use version::{ApiVersion, ApiVersionParseError, SemverTriple};