| Crates.io | more-convert |
| lib.rs | more-convert |
| version | 0.14.0 |
| created_at | 2025-01-25 23:33:12.077698+00 |
| updated_at | 2025-08-17 10:45:35.456613+00 |
| description | This crate adds macros for various conversions. |
| homepage | |
| repository | https://github.com/moriyoshi-kasuga/more-convert |
| max_upload_size | |
| id | 1530935 |
| size | 28,075 |
This crate provides utilities for convert
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.
more-convert provides a derive macro
Convert:
From trait for structs.EnumRepr:
TryFrom trait for safe conversions from the
representation type back to the enum. This ensures that conversions are explicitly
handled and potential errors are managed.From trait 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.u8, it enhances code readability and
maintainability, making it easier to manage types and ensure type safety in conversions.VariantName provides a method to retrieve the name of an enum variant as a string.
EnumArray
COUNT and VARIANTS const, which return the number of
variants and an array of all variants, respectively.more info: doc.rs
use more_convert::Convert;
#[derive(Convert)]
#[convert(from(B))]
pub struct A {
#[convert(map = value.sample.to_string())]
pub sample: String,
// auto into of inner
pub opt: Option<u16>,
// auto into of inner
pub vec: Vec<u16>,
}
pub struct B {
pub sample: u8,
pub opt: Option<u8>,
pub vec: Vec<u8>,
}
let b = B {
sample: 1u8,
opt: Some(0u8),
vec: vec![1u8, 2u8],
};
let a: A = b.into();
assert_eq!(a.sample, String::from("1"));
assert_eq!(a.opt, Some(0u16));
assert_eq!(a.vec, vec![1u16, 2u16]);
enum_attributes
serde::Serialize and serde::Deserializevariant_attributres
From traitmore info: doc.rs
use more_convert::EnumRepr;
#[derive(EnumRepr, Clone, Copy, Debug, PartialEq)]
#[enum_repr(implicit, serde)]
#[repr(u8)]
pub enum Test {
Zero,
Three = 3,
Four,
}
assert_eq!(u8::from(Test::Zero), 0u8);
assert_eq!(serde_json::to_string(&Test::Zero).unwrap(), "0");
assert_eq!(serde_json::from_str::<Test>("0").unwrap(), Test::Zero);
// return error with unknown value
assert_eq!(Test::try_from(1).unwrap_err().to_string(), String::from("Failed to convert value 1 to enum Test"));
assert_eq!(serde_json::from_str::<Test>("1").unwrap_err().to_string(), String::from("Failed to convert value 1 to enum Test"));
use more_convert::EnumRepr;
#[derive(EnumRepr, Clone, Copy, Debug, PartialEq)]
#[enum_repr(implicit, serde)]
#[repr(u8)]
pub enum Test {
#[enum_repr(default)]
Zero,
Three = 3,
Four,
}
// return fallback with unknown value
// impled From
assert_eq!(Test::Zero, 1u8.into());
assert_eq!(serde_json::from_str::<Test>("1").unwrap(), Test::Zero);
enum_attributes
more_convert::VariantName trait and definition fn as const (and the caller does not need to depend on this crate)variant_attributes
more info: doc.rs
use more_convert::VariantName;
// not apply rename_all to prefix, Don't forget the underscore.
#[derive(VariantName)]
#[variant_name(rename_all = "snake_case", prefix = "error_")]
pub enum Error {
InvalidCode,
ServerError,
}
assert_eq!("error_invalid_code", Error::InvalidCode.variant_name());
assert_eq!("error_server_error", Error::ServerError.variant_name());
use more_convert::EnumArray;
#[derive(Debug, PartialEq, EnumArray)]
pub enum Test {
Zero,
Two,
Three,
}
assert_eq!(Test::COUNT, 3usize);
assert_eq!(Test::VARIANTS, &[Test::Zero, Test::Two, Test::Three]);
Licensed under