- Schema-based serialization: Define data structures using OpenAPI 3.x compatible schemas
- Compact binary format: 3-5x size reduction compared to JSON
- Type-safe: Full Rust type safety with optional derive macros
- Cross-language: Binary output compatible with all other Compactr clients (.js, .cs)
- Thread-safe: Schema registry with concurrent access support
Add this to your Cargo.toml:
[dependencies]
compactr = "0.1"
# For derive macro support
compactr = { version = "0.1", features = ["derive"] }
# For serde integration
compactr = { version = "0.1", features = ["serde"] }
# For all features
compactr = { version = "0.1", features = ["full"] }use compactr::{SchemaType, Value, Property};
use indexmap::IndexMap;
// Define a schema
let mut properties = IndexMap::new();
properties.insert("id".to_string(), Property::required(SchemaType::string_uuid()));
properties.insert("name".to_string(), Property::required(SchemaType::string()));
properties.insert("age".to_string(), Property::required(SchemaType::int32()));
let schema = SchemaType::object(properties);
// Create a value
let mut obj = IndexMap::new();
obj.insert("id".to_string(), Value::from("550e8400-e29b-41d4-a716-446655440000"));
obj.insert("name".to_string(), Value::from("Alice"));
obj.insert("age".to_string(), Value::from(30_i32));
let value = Value::Object(obj);
// Encode and decode (implementation in progress)
// let encoded = encode(&value, &schema)?;
// let decoded = decode(&encoded, &schema)?;use compactr_derive::Compactr;
use uuid::Uuid;
#[derive(Compactr)]
struct User {
#[compactr(format = "uuid")]
id: Uuid,
name: String,
age: i32,
}use openapiv3::OpenAPI;
use compactr::{SchemaType, Encoder, Decoder};
// Load from JSON/YAML
let spec: OpenAPI = serde_json::from_str(&spec_json)?;
let user_schema = spec.components.schemas.get("User")?;
// Convert to Compactr schema
let compactr_schema = convert_schema(user_schema)?;
// Use for encoding/decoding
let mut encoder = Encoder::new();
encoder.encode(&user_data, &compactr_schema)?;// Matches this OpenAPI schema:
// components:
// schemas:
// User:
// type: object
// required: [id, name]
// properties:
// id: { type: string, format: uuid }
// name: { type: string }
// email: { type: string }
let mut props = IndexMap::new();
props.insert("id", Property::required(SchemaType::string_uuid()));
props.insert("name", Property::required(SchemaType::string()));
props.insert("email", Property::optional(SchemaType::string()));
let schema = SchemaType::object(props);Compactr works with popular Rust OpenAPI libraries:
- openapiv3 - Parse OpenAPI 3.0.x specs
- oas3 - Parse OpenAPI 3.1.x specs
- utoipa - Code-first OpenAPI generation
- progenitor - Client generation
See examples/openapi_*.rs for complete integration examples.
| Schema Type | Rust Type | Binary Size |
|---|---|---|
boolean |
bool |
1 byte |
integer(int32) |
i32 |
4 bytes |
integer(int64) |
i64 |
8 bytes |
number(float) |
f32 |
4 bytes |
number(double) |
f64 |
8 bytes |
string |
String |
2 + N bytes |
string(uuid) |
Uuid |
16 bytes |
string(datetime) |
DateTime<Utc> |
8 bytes |
string(date) |
NaiveDate |
4 bytes |
string(ipv4) |
Ipv4Addr |
4 bytes |
string(ipv6) |
Ipv6Addr |
16 bytes |
binary |
Vec<u8> |
4 + N bytes |
array |
Vec<T> |
4 + items |
object |
IndexMap<String, T> |
sum of fields |
- Project structure and dependencies
- Error types
- Value types
- Schema definitions
- Schema registry
- Encoder/Decoder implementation
- Format implementations (UUID, DateTime, Date, IPv4, IPv6, Binary)
- OpenAPI integration examples
- Derive macros
- Cross-language compatibility tests
git clone https://github.com/yourusername/compactr.rs
cd compactr.rs
cargo build
cargo testcargo test --all-featurescargo benchContributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Apache 2.0 (c) 2025 Frederic Charette