# anycms-core
A unified API response library supporting multiple Rust web frameworks.
## Features
- **Framework-agnostic core**: `ApiResult<T>` structure works independently of any web framework
- **Multiple framework support**: Built-in integrations for actix-web and axum
- **Feature flags**: Use only what you need - zero unused dependencies
- **Flexible response format**: Support for single values, lists, pagination, error codes, and extra metadata
## Installation
Add this to your `Cargo.toml`:
```toml
[dependencies]
anycms-core = "0.2"
```
### Feature Flags
- `actix` (default): Enable actix-web integration
- `axum`: Enable axum integration
- `full`: Enable all framework integrations
#### Examples
```toml
# Default (actix-web only)
anycms-core = "0.2"
# Axum only
anycms-core = { version = "0.2", features = ["axum"] }
# Both frameworks
anycms-core = { version = "0.2", features = ["full"] }
```
## Project Structure
```
src/
├── lib.rs # Library entry point with feature-gated exports
├── result.rs # Core ApiResult<T> definition (framework-agnostic)
├── pagination.rs # Pagination metadata structure
└── frameworks/
├── mod.rs # Framework module declarations
├── actix.rs # actix-web integration (Responder trait)
└── axum.rs # axum integration (IntoResponse trait)
```
## Architecture
The library is designed with a clear separation of concerns:
1. **Core Layer** ([result.rs](src/result.rs), [pagination.rs](src/pagination.rs))
- Contains data structures and builder methods
- Zero framework dependencies
- Always compiled regardless of features
2. **Framework Layer** ([frameworks/](src/frameworks/))
- Implements framework-specific traits
- Conditionally compiled based on feature flags
- Isolated to prevent conflicts
## Usage
### Actix-web
```rust
use actix_web::{get, web};
use anycms_core::{ApiResult, ResultPagination};
#[get("/users")]
async fn list_users() -> ApiResult<User> {
let users = fetch_users().await;
let pagination = ResultPagination::new(100, 1, 10);
ApiResult::list(users)
.with_pagination(pagination)
.with_extra("has_more", serde_json::json!(true))
}
```
### Axum
```rust
use axum::{extract::Json, routing::get};
use anycms_core::{ApiResult, ResultPagination};
async fn list_users() -> ApiResult<User> {
let users = fetch_users().await;
let pagination = ResultPagination::new(100, 1, 10);
ApiResult::list(users)
.with_pagination(pagination)
.with_extra("has_more", serde_json::json!(true))
}
```
## Examples
Run the demo examples:
```bash
# Actix-web demo
cargo run --example actix_demo --features actix
# Axum demo
cargo run --example axum_demo --features axum
# Check examples
cargo check --examples --all-features
```
## API Response Format
### Success Response (Single Value)
```json
{
"success": true,
"data": { "id": 1, "name": "John" }
}
```
### Success Response (List)
```json
{
"success": true,
"list": [...],
"pagination": {
"total": 100,
"page": 1,
"page_size": 10,
"current_page": 1
}
}
```
### Error Response
```json
{
"success": false,
"message": "User not found",
"code": 404
}
```
## License
MIT