more-convert: more convert utilities
This crate provides utilities for convert
Note
The Convert attribute guarantees that the into method automatically implements the From trait internally, ensuring seamless conversions!
The information provided below is a summary of key points. For the most current and detailed documentation, please refer to doc.rs.
Usage
more-convert provides a derive macro
-
Convert:
- This macro is designed to handle simple conversions by automatically implementing
the
Fromtrait for structs. - It aims to eliminate boilerplate code within your architecture, focusing on straightforward use cases.
- For more detailed information, please visit: doc.rs
- This macro is designed to handle simple conversions by automatically implementing
the
-
EnumRepr:
- This macro primarily implements the
TryFromtrait for safe conversions from the representation type back to the enum. This ensures that conversions are explicitly handled and potential errors are managed. - Optionally, the
Fromtrait can be implemented for converting an enum to its representation type when a default value is specified using the#[enum_repr(default)]attribute. This provides a fallback mechanism for conversions. - By using enums instead of primitive types like
u8, it enhances code readability and maintainability, making it easier to manage types and ensure type safety in conversions. - For more detailed information, please visit: doc.rs
- This macro primarily implements the
-
VariantName provides a method to retrieve the name of an enum variant as a string.
- This is particularly useful for error handling and logging, where understanding the specific variant can aid in debugging and reporting.
- For more detailed information, please visit: doc.rs
-
EnumArray
- It automatically implements the
COUNTandVARIANTSconst, which return the number of variants and an array of all variants, respectively. - This is useful when you need to iterate over every possible value of an enum or perform operations on each variant.
- It automatically implements the
Examples
Convert
- field_attributes
- ignore: skip the field
- rename: rename the field
- map
- map: map of expr
- map_field: map of field
- map_struct: map of struct
more info: doc.rs
use Convert;
let b = B ;
let a: A = b.into;
assert_eq!;
assert_eq!;
assert_eq!;
EnumRepr
-
enum_attributes
- serde: automatically implements
serde::Serializeandserde::Deserialize - implicit: not required to specify the discriminant (not recommended)
- serde: automatically implements
-
variant_attributres
- default: set the fallback value for the
Fromtrait
- default: set the fallback value for the
more info: doc.rs
impled TryFrom (not use default attribute)
use EnumRepr;
assert_eq!;
assert_eq!;
assert_eq!;
// return error with unknown value
assert_eq!;
assert_eq!;
impled From (use default attribute)
use EnumRepr;
// return fallback with unknown value
// impled From
assert_eq!;
assert_eq!;
VariantName
-
enum_attributes
- without_tarit: without
more_convert::VariantNametrait and definition fn asconst(and the caller does not need to depend on this crate) - rename_all: apply rule to field name
- Possible values: "lowercase", "UPPERCASE", "PascalCase", "camelCase", "snake_case", "SCREAMING_SNAKE_CASE", "kebab-case", "SCREAMING-KEBAB-CASE"
- prefix: add prefix to field name
- suffix: add suffix to field name
- without_tarit: without
-
variant_attributes
- rename: rename field (prefix, suffix, and rename_all are not applied)
more info: doc.rs
use VariantName;
// not apply rename_all to prefix, Don't forget the underscore.
assert_eq!;
assert_eq!;
EnumArray
- enum_attributes: none
- variant_attributes: none
use EnumArray;
assert_eq!;
assert_eq!;
License
Licensed under