specta-serde
A comprehensive serde attribute handling system for Specta. This crate provides functionality to parse and apply serde attributes like #[serde(rename = "...")], #[serde(rename_all = "...")], and enum representation attributes to DataType instances, with separate handling for serialization and deserialization phases.
Features
- Comprehensive Attribute Support: Handles
rename,rename_all,skip,flatten,default,transparent, and enum representation attributes - Separate Processing Modes: Distinct handling for serialization and deserialization transformations
- Enum Representations: Full support for external, internal, adjacent, untagged, and string enum representations
- Type-Safe Transformations: Apply serde semantics while maintaining type safety
- Integration Ready: Works seamlessly with existing Specta workflows and TypeScript exports
Quick Start
Add specta-serde to your Cargo.toml:
[]
= { = "2.0.0-rc.23", = ["derive"] }
= "0.0.10"
= { = "1.0", = ["derive"] }
Basic Usage
Processing Type Collections
use ;
use ;
use ;
Individual Type Transformations
use ;
// Transform a specific DataType
let transformed = apply_serde_transformations?;
Supported Serde Attributes
Container Attributes
#[serde(rename_all = "...")]
Transforms field/variant names according to the specified case convention:
Supported cases:
lowercaseUPPERCASEPascalCasecamelCasesnake_caseSCREAMING_SNAKE_CASEkebab-caseSCREAMING-KEBAB-CASE
#[serde(transparent)]
For wrapper types that should serialize as their inner type:
;
Field Attributes
#[serde(rename = "...")]
Rename specific fields:
Skip Attributes
Control serialization/deserialization:
#[serde(flatten)]
Flatten nested structures:
#[serde(default)]
Use default values during deserialization:
Enum Representations
External (Default)
// Serializes as: "Active" or { "Inactive": { "reason": "..." } }
Internal
// Serializes as: { "type": "Text", "content": "..." }
Adjacent
// Serializes as: { "type": "Success", "data": "..." }
Untagged
// Serializes as the inner value directly
String Enums
Unit-only enums with rename_all become string enums:
Advanced Usage
Processing Modes
The SerdeMode enum controls which transformations are applied:
use SerdeMode;
// For serialization (Rust -> JSON)
let ser_transformed = apply_serde_transformations?;
// For deserialization (JSON -> Rust)
let de_transformed = apply_serde_transformations?;
This allows different behavior based on direction:
skip_serializingonly affectsSerdeMode::Serializeskip_deserializingonly affectsSerdeMode::Deserializeskipaffects both modes
Integration with TypeScript
use Typescript;
use process_for_serialization;
let types = default
.;
// Transform for serialization before export
let transformed_types = process_for_serialization?;
default
.with_serde
.export_to?;
Validation
The crate includes validation to ensure serde attributes are used correctly:
use validate;
let types = default
.;
// Validate serde usage
validate?;
Error Handling
The crate defines specific error types for various serde-related issues:
use Error;
match apply_serde_transformations
Examples
See the examples directory for comprehensive usage examples, including:
- Basic serde transformations
- Complex enum representations
- Integration with TypeScript exports
- Error handling patterns
Contributing
Contributions are welcome! Please see the main Specta repository for contribution guidelines.
License
This project is licensed under the MIT License - see the LICENSE file for details.