more-convert-derive-internal 0.7.0

This crate adds macros for various conversions.
Documentation

more-convert: more convert utilities

more-convert on crates.io more-convert on docs.rs

This crate provides utilities for convert

Usage

more-convert provides a derive macro

  • Convert automatically implements From or Into for named structs
    • Leave the very cumbersome From and Into implementations to us!
    • more info: doc.rs
  • EnumRepr automatically implements TryFrom and Into for enums
    • Ideal for managing Type, etc.
    • more info: doc.rs
  • EnumName provides a method to get the name of the enum variant
    • Ideal for error (kind) handling, etc.
    • more info: doc.rs

Examples

What I write below is what I picked up. I try to keep it up to date in doc.rs, so please look there!

EnumRepr

  • enum_attributes
    • serde: automatically implements serde::Serialize and serde::Deserialize

more info: doc.rs

use more_convert::EnumRepr;

#[derive(EnumRepr, Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
#[enum_repr(implicit)]
pub enum Test {
    Zero,
    Three = 3,
    Four,
}

assert_eq!(0u8, Test::Zero.into());
assert_eq!(3u8, Test::Three.into());
assert_eq!(4u8, Test::Four.into());

assert_eq!(0u8.try_into(), Ok(Test::Zero));
assert_eq!(3u8.try_into(), Ok(Test::Three));
assert_eq!(4u8.try_into(), Ok(Test::Four));

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 more_convert::Convert;

#[derive(Convert)]
#[convert(from(B))]
pub struct A {
    pub a: u8,
    pub b: u8,
}

pub struct B {
    pub a: u8,
    pub b: u8,
}

let b = B {
    a: 1u8,
    b: 2u8,
};

let a: A = b.into();

assert_eq!(a.a, 1u8);
assert_eq!(a.b, 2u8);

more Into examples are here
more From examples are here

EnumName

  • enum_attributes

    • 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
  • variant_attributes

    • rename: rename field (prefix, suffix, and rename_all are not applied)

more info: doc.rs

use more_convert::EnumName;

#[derive(EnumName)]
pub enum Error {
    InvalidCode,
    ServerError,
}

assert_eq!("InvalidCode", Error::InvalidCode.enum_name());
assert_eq!("ServerError", Error::ServerError.enum_name());

License

Licensed under