Derive conversion traits when items are structurally similar.

Overview

structural-convert

Derive conversion traits when items are structurally similar.

Inspired by serde and struct-convert crates.

Features

  • One to one fields mapping derive for
    • From
    • Into
    • TryFrom
    • TryInto
  • Inner fields type conversion using .into()/.try_into()
  • Rename enum variants and named fields
  • Skip enum variants and named fields
  • Fallback to default enum variant
  • Named fields conversion fallback to default
  • Enum not matched variants fallback to enum default
  • Struct named fields only - Middle man type conversion using as attribute

Features Wishlist

  • Implement attributes for unnamed fields (default, skip, as)

Examples

Check the tests folder for more examples, but here is some samples:

Struct

#[derive(Debug, PartialEq)]
struct Rhs {
    z: i8,
    x: u32,
}

#[derive(Debug, PartialEq, StructuralConvert)]
#[convert(from(path = "Rhs"))]
struct Lhs {
    z: i32,
    x: u32,
}

assert_eq!(Lhs { z: 1, x: 2 }, Rhs { z: 1, x: 2 }.into());
assert_eq!(Lhs { z: 1, x: 2 }, Rhs { z: 1, x: 2 }.into());

Generated code:

impl From<Rhs> for Lhs {
    fn from(value: Rhs) -> Self {
        match value {
            Rhs { z, x } => Lhs {
                z: z.into(),
                x: x.into(),
            },
        }
    }
}

Enum

    #[derive(Debug, PartialEq)]
    enum Rhs {
        A { z: i8, x: u32 },
    }

    #[derive(Debug, PartialEq, StructuralConvert)]
    #[convert(from(path = "Rhs"))]
    enum Lhs {
        A { z: i32, x: u32 },
    }

    assert_eq!(Lhs::A { z: 1, x: 2 }, Rhs::A { z: 1, x: 2 }.into());

Generated code:

impl From<Rhs> for Lhs {
    fn from(value: Rhs) -> Self {
        match value {
            Rhs::A { z, x } => Lhs::A {
                z: z.into(),
                x: x.into(),
            },
        }
    }
}
You might also like...
A syntax exploration of eventually stable Rust Iterator items

Rust Iterator Items: a syntax exploration This crate is a thin wrapper around the unstable generator feature, allowing users to create new items that

List public items (public API) of library crates. Enables diffing public API between releases.

cargo-public-items List public items (the public API) of a Rust library crate by analyzing the rustdoc JSON of the crate. Automatically builds the rus

List public items (public API) of Rust library crates. Enables diffing public API between releases.

cargo wrapper for this library You probably want the cargo wrapper to this library. See https://github.com/Enselic/cargo-public-items. public_items Li

An interface for managing collections of labeled items and generating random subsets with specified restrictions

An interface for managing collections of labeled items and generating random subsets with specified restrictions

rpsc is a *nix command line tool to quickly search for file systems items matching varied criterions like permissions, extended attributes and much more.

rpsc rpsc is a *nix command line tool to quickly search for file systems items matching varied criterions like permissions, extended attributes and mu

Fills an `impl` with the associated items required by the trait.

portrait Fill impl-trait blocks with default, delegation and more. Motivation Rust traits support provided methods, which are great for backwards comp

miette is a diagnostic library for Rust. It includes a series of traits/protocols that allow you to hook into its error reporting facilities, and even write your own error reports!
miette is a diagnostic library for Rust. It includes a series of traits/protocols that allow you to hook into its error reporting facilities, and even write your own error reports!

miette is a diagnostic library for Rust. It includes a series of traits/protocols that allow you to hook into its error reporting facilities, and even write your own error reports!

Traits for inspecting memory usage of Rust types

memuse This crate contains traits for measuring the dynamic memory usage of Rust types. About Memory-tracking is a common activity in large applicatio

Annotation to easily define ad-hoc / one-shot extension traits

Annotation to easily define ad-hoc / one-shot extension traits

Pure-Rust traits and utilities for constant-time cryptographic implementations.

subtle Pure-Rust traits and utilities for constant-time cryptographic implementations. It consists of a Choice type, and a collection of traits using

A collection of numeric types and traits for Rust.

num A collection of numeric types and traits for Rust. This includes new types for big integers, rationals (aka fractions), and complex numbers, new t

A typemap for a set of known types optionally without heap allocation, and supporting iterating by traits

fixed_typemap docs.rs GitHub Sponsors Implements typemaps that support a lot of extra funcctionality using procedural macros. docs.rs has a lot more t

Visitor traits for horned-owl with overloadable implementations

horned-visit Visitor traits for horned-owl with overloadable implementations. 🗺️ Overview This library provides visitor traits for the horned-owl obj

Async `TryFrom/TryInto` traits

async-convert Async TryFrom/TryInto traits API Docs | Releases | Contributing Installation $ cargo add async-convert Safety This crate uses #![deny(un

Various extention traits for providing asynchronous higher-order functions

async-hofs Various extention traits for providing asynchronous higher-order functions. // This won't make any name conflicts since all imports inside

A general solution for commonly used crypt in rust, collection of cryptography-related traits and algorithms.

Crypto-rs A general solution for commonly used crypt in rust, collection of cryptography-related traits and algorithms. This is a Rust implementation

Expose various non-cryptographic hashing functions with Digest traits

noncrypto-digests Expose various non-cryptographic hashing functions with Digest traits. This allows users to use any hashing function with the same t

A copypastable guide to implementing simple derive macros in Rust.

A copypastable guide to implementing simple derive macros in Rust. The goal Let's say we have a trait with a getter trait MyTrait {

This crate allows writing a struct in Rust and have it derive a struct of arrays layed out in memory according to the arrow format.

Arrow2-derive - derive for Arrow2 This crate allows writing a struct in Rust and have it derive a struct of arrays layed out in memory according to th

Owner
Julius Lungys
Julius Lungys
A typemap for a set of known types optionally without heap allocation, and supporting iterating by traits

fixed_typemap docs.rs GitHub Sponsors Implements typemaps that support a lot of extra funcctionality using procedural macros. docs.rs has a lot more t

Austin Hicks 2 Dec 27, 2021
Derive AsyncRead and AsyncWrite.

derive-tokio-io Derive AsyncRead and AsyncWrite. Usage If the struct has only one field, AsyncRead and AsyncWrite are derived for that field. use deri

Eray Karatay 3 Nov 17, 2022
Procedural macro to derive Serde serializer-deserializer for Prost

prost-serde-derive prost-serde-derive is a procedural macro to generate Serde serializers and deserializers for Prost-generated structs. Rationale Cur

Park Joon-Kyu 4 Dec 15, 2022
Simple Rust derive-macro that simplifies integral enum creation

Integral enum A simple way to define integer-like enums. This macro implements bunch of traits that are usually implemented via looooong derive(..) at

null 3 Jan 6, 2023
Derive `clone_from` for `Clone` in Rust

Derive Clone including clone_from This crate offers a derive macro called CloneFrom to derive Clone with a specialized clone_from implementation. When

Moritz Hoffmann 7 Mar 13, 2023
derive(Code) simplifies error handling by providing an easy-to-use enumeration of error codes

enum-code Introduction enum-code is a derive macro for enum types. This library generates code that associates error codes with error types. It can be

Bay 5 Jun 14, 2023
Conversion Tools API Rust client

ConversionTools Rust This Conversion Tools API Rust client allows you to use the site API and convert files faster and more conveniently. Site Convers

WinsomeQuill 2 Jan 23, 2022
Conversion of .eopkg into .stone

a-piece-of-pisi Crazy time: We're building a tool to convert a selection of .eopkg packages into .stone packages to be consumed by moss-rs to vastly a

Serpent OS 6 Oct 17, 2023
Traits - Collection of cryptography-related traits

RustCrypto: Traits Collection of traits which describe functionality of cryptographic primitives. Crates Name Algorithm Crates.io Docs MSRV aead Authe

Rust Crypto 401 Dec 27, 2022
Stdto provides a set of functional traits for conversion between various data representations.

Stdto stdto provides a set of functional traits for conversion between various data representations. | Examples | Docs | Latest Note | stdto = "0.13.0

Doha Lee 5 Dec 21, 2022