omitty
A Rust procedural macro for generating type variants with omitted fields.
Features
- Simple syntax - Generate variants with just
#[omit_variant(VariantName, field1, field2)] - Automatic derive - Automatically generates Debug, Clone, Serialize, Deserialize, PartialEq, Eq
- Serde compatible - Works seamlessly with serde for JSON/serialization
- Type-safe - Compile-time guarantees for omitted fields
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["derive"] }
= "1.0"
Usage
Basic Example
use OmittableType;
This generates a new UserPublic struct with only id and username:
Multiple Omitted Fields
use OmittableType;
Multiple Variants
use OmittableType;
This generates both UserPublic and UserMinimal structs with different fields omitted.
Real-World Example: Discord-like API
use OmittableType;
Syntax
- VariantName: The name of the generated struct (identifier, not a string)
- field1, field2, ...: Names of fields to omit from the original struct
Generated Derives
All generated variants automatically derive:
DebugCloneserde::Serializeserde::DeserializePartialEqEq
Use Cases
- API responses - Return public user data without sensitive fields
- Database models - Create DTO variants without internal fields
- Multi-tier data access - Different permission levels see different fields
- Caching - Store lightweight versions of structs
- Testing - Create test fixtures with subset of data
Error Messages
The macro provides clear, actionable error messages:
// Error: Omittable can only be derived for structs with named fields (not tuple structs or unit structs)
// Error: field type must be a path type (e.g., User, MyType). Complex types like references, arrays, or tuples are not supported
// Error: missing variant name as first argument in omit_variant attribute. Usage: #[omit_variant(VariantName, field1, field2, ...)]
License
MIT OR Apache-2.0