Skip to main content

anycms_core/
lib.rs

1//! anycms-core
2//!
3//! A unified API response library supporting multiple Rust web frameworks.
4//!
5//! ## Features
6//! - `actix` (default): Support for actix-web framework
7//! - `axum`: Support for axum framework
8//! - `validator`: Integration with the `validator` crate for derive-based validation
9//! - `tracing`: Automatic error logging via the `tracing` crate
10//! - `full`: Enable all framework integrations
11//! - `camel-case` (default): Use camelCase for JSON field names
12//! - `snake-case`: Use snake_case for JSON field names
13//!
14//! ## Basic Usage
15//! ```rust
16//! use anycms_core::ApiResult;
17//! use serde::Serialize;
18//!
19//! #[derive(Serialize)]
20//! struct User {
21//!     id: u32,
22//!     name: String,
23//! }
24//!
25//! fn handle_request() -> ApiResult<User> {
26//!     let user = User { id: 1, name: "Alice".to_string() };
27//!     ApiResult::value(user)
28//! }
29//! ```
30//!
31//! ## Business Error Code & Timestamp
32//! ```rust
33//! use anycms_core::ApiResult;
34//! use serde::Serialize;
35//!
36//! #[derive(Serialize)]
37//! struct User {
38//!     id: u32,
39//!     name: String,
40//! }
41//!
42//! fn handle_error() -> ApiResult<User> {
43//!     ApiResult::fail("User not found")
44//!         .with_code(404)
45//!         .with_biz_code(10001)
46//!         .with_current_timestamp()
47//! }
48//!
49//! fn handle_success() -> ApiResult<()> {
50//!     ApiResult::ok()
51//!         .with_message("Done")
52//!         .with_biz_code(0)
53//!         .with_timestamp(1700000000000)
54//! }
55//! ```
56//!
57//! ## Error Handling
58//! ```rust
59//! use anycms_core::ApiResult;
60//!
61//! fn handle_error() -> ApiResult<String> {
62//!     ApiResult::fail("Resource not found").with_code(404)
63//! }
64//! ```
65//!
66//! ## Validation Errors
67//! ```rust
68//! use anycms_core::{ApiResult, FieldError};
69//!
70//! fn validate() -> ApiResult<String> {
71//!     ApiResult::validation_errors(vec![
72//!         FieldError::new("email", "invalid format"),
73//!     ])
74//! }
75//! ```
76//!
77//! ## Request Tracing
78//! ```rust
79//! use anycms_core::ApiResult;
80//!
81//! fn handler() -> ApiResult<String> {
82//!     ApiResult::ok().with_trace_id("req-abc123")
83//! }
84//! ```
85
86mod pagination;
87mod result;
88
89// Framework-specific implementations
90mod frameworks;
91
92// Optional integrations
93#[cfg(feature = "validator")]
94mod validator;
95
96#[cfg(feature = "tracing")]
97mod tracing;
98
99// Core exports (always available)
100pub use pagination::ResultPagination;
101pub use result::{ApiResult, DefaultResult, ErrorCode, FieldError, ResponseData};
102
103// Framework-specific exports (available only when corresponding feature is enabled)
104#[cfg(feature = "actix")]
105pub use frameworks::actix;
106
107#[cfg(feature = "axum")]
108pub use frameworks::axum;
109
110// Optional integration exports
111#[cfg(feature = "validator")]
112pub use validator::validation_errors_to_field_errors;
113
114#[cfg(feature = "tracing")]
115pub use tracing::ApiResultTracing;