Holda
Holda is a Rust library that provides convenience macros for creating domain objects, especially those that wrap existing types, with built-in support for serde serialization and deserialization. It simplifies the process of creating value types with common trait implementations, reducing boilerplate and improving code readability.
Features
- Automatic Trait Implementations: The
StringHoldaandHoldaderive macros automatically implement common traits likeFrom,AsRef,Deref,Display,Debug,PartialEq,Eq,PartialOrd,Ord,Hash, andClone. serdeSupport: Easily enableserdeserialization and deserialization for your wrapper types with theserdefeature.- Customizable: Skip specific trait implementations using the
#[holda(...)]attribute. - String-Specific Macro: The
StringHoldamacro is optimized for creating wrappers aroundStringtypes, providingFromStrimplementations.
Usage
Add holda to your Cargo.toml:
[]
= "0.1.0"
StringHolda Macro
The StringHolda macro is designed for structs that hold a String. It automatically implements traits and provides FromStr implementation.
use StringHolda;
With the serde feature enabled, you can serialize and deserialize the UserName struct:
use StringHolda;
use Deserialize;
use Serialize;
Holda Macro
The Holda macro is more generic and can be used with any type. It requires specifying the field name as inner.
use Holda;
use Uuid;
You can skip trait implementations using the #[holda(...)] attribute:
use Holda;
serde Feature
To enable serde support, add the serde feature to your Cargo.toml:
[]
= { = "0.1.0", = ["serde"] }
= { = "1.0", = ["derive"] } # required
= "1.0" # required for testing
Now, the generated code will include serde::Serialize and serde::Deserialize implementations for your wrapper types.
Skipping Trait Implementations
You can use the #[holda(...)] attribute to skip specific trait implementations. The following options are available:
NoDisplay: Skips implementingDisplay.NoEq: Skips implementingPartialEqandEq.NoOrd: Skips implementingPartialOrdandOrd.NoHash: Skips implementingHash.NoClone: Skips implementingClone.NoSerde: Skips implementingSerializeandDeserialize.
License
This project is licensed under the MPL-2.0 License. See the LICENSE file for details.