derive-into
A Rust derive macro for easily creating conversions between structs and enums.
Features
- Automate conversions between similar data structures
- Support for struct-to-struct, tuple struct, and enum conversions
- Field renaming capabilities
- Automatic handling of wrapped types with
From/Intoimplementations - Special handling for
Option,Vec, andHashMaptypes, including recursive nested containers - Support for both infallible (
From/Into) and fallible (TryFrom) conversions - Fine-grained control with field-level attributes
- Support for nested type conversions
- HashMap conversion with key and value type conversions
- Custom conversion functions with the
with_funcattribute
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
Quick Start
use Convert;
// Source struct with conversion attributes
// Generate Into<Destination> implementation
// Destination struct
// Usage
let source = Source ;
let destination: Destination = source.into;
Struct-Level Attributes
Struct-level attributes can be applied at the struct or enum level to control conversion behavior:
| Attribute | Description |
|---|---|
#[convert(into(path = "Type"))] |
Generate an From<Self> for Type implementation |
#[convert(try_into(path = "Type"))] |
Generate an TryFrom<Self> for Type implementation |
#[convert(try_from(path = "Type"))] |
Generate a TryFrom<Type> for Self implementation |
#[convert(from(path = "Type"))] |
Generate a From<Type> for Self implementation |
#[convert(into(path = "Type", default))] |
Enable default values for fields not explicitly mapped in the target type |
Multiple conversion types can be specified for a single struct:
| #[convert(try_from(path = "Type"))] | Specify a path for try_from conversion |
Field-Level Attributes
Field-level attributes can be applied at three different scopes:
-
Global scope - applies to all conversion types:
-
Conversion type scope - applies only to a specific conversion type (into, from, try_from):
-
Specific conversion scope - applies only to a singular conversion target:
Common field-level attributes:
| Attribute | Description |
|---|---|
#[convert(rename = "new_name")] |
Map this field to a differently named field in the target type |
#[convert(unwrap_or_default)] |
Automatically calls unwrap_or_default on Option value before converting it |
#[convert(unwrap)] |
Automatically unwrap an Option value (fails in try_from if None) |
#[convert(skip)] |
Skip this field during conversion (target must provide a default) |
#[convert(default)] |
Use default value for this field during conversion |
#[convert(with_func = func_name)] |
Use custom function for conversion. The function needs to take a reference to the parent struct |
Enum Conversion
The macro supports enum-to-enum conversion with similar attribute control:
Type Conversions
The macro intelligently handles various type scenarios:
- Direct Mapping: Fields with identical types are directly copied
- Automatic Conversion: Fields with types that implement
From/Intoare automatically converted - Container Types: Special handling for
Option<T>,Vec<T>, andHashMap<K, V>with inner type conversion - Recursive Container Conversion: Nested containers like
Option<Vec<T>>,Vec<Option<T>>,HashMap<K, Vec<V>>,Option<HashMap<K, V>>, etc. are converted recursively — inner types are converted at every nesting level - Tuple Structs: Support for conversions between tuple structs
- Nested Type Conversions: Automatically handles nested struct and enum conversions
Examples
Basic Struct Conversion
use Convert;
// Usage
let source = Source ;
let target: Target = source.into;
Handling Option and Vec Types
The macro automatically handles conversion of inner types for Option and Vec:
use Convert;
;
Recursive Nested Container Conversion
Container types are converted recursively at every nesting level. This means types like Option<Vec<T>>, Vec<Option<T>>, Vec<Vec<T>>, HashMap<K, Vec<V>>, and any arbitrary nesting depth just work — inner types are automatically converted using their From/Into/TryFrom/TryInto implementations.
use Convert;
use HashMap;
;
;
Using UnwrapOrDefault for Options
use Convert;
// This will succeed
let source = Source ;
let target: = try_from;
assert!;
// This will fail because value is None
let source = Source ;
let target: = try_from;
assert!;
Using Unwrap for Options
use Convert;
// This will succeed
let source = Source ;
let target: = try_from;
assert!;
// This will fail because value is None
let source = Source ;
let target: = try_from;
assert!;
Using Default Values
use Convert;
Tuple Struct Conversion
use Convert;
;
;
Complex Nested Conversions with Scoped Attributes
use Convert;
use HashMap;
// Custom conversion function
Enum Conversion with Nested Types
use Convert;
Custom Conversion Functions
use Convert;
// Custom conversion function
License
This project is licensed under the MIT License - see the LICENSE file for details.