Giter Club home page Giter Club logo

struct_morph's Introduction

Struct Morph Rust Version

macro for morphing one struct into another.

Installation

cargo add struct_morph

or manually

struct_morph = "0.6"

Usage

I occasionally run into use case where i have two structs representing very similar data. They share most of the fields or can sometmes even be subset of one. Say we have a struct ProductRow coming from database and another struct ProductInfo which is what will be sent as a json from the api.

struct ProductRow {
    id: i32,
    name: String,
    description: String,
    available_count: i32,
    base_price: i32,
    discount: i32,
    created_at: DateTime,
    updated_at: DateTime,
}

struct ProductInfo {
    id: i32,
    name: String,
    description: String,
    is_available: bool,
    price: i32,
}

now for this small when we need to convert ProductRows to ProductInfos we can manually do it, but for larger structs it becomes quite mechanical.

with this library you can write the following

use struct_morph::{morph, morph_field};

#[morph(ProductRow)]
struct ProductInfo {
    id: i32,
    #[morph_field(select = name)]
    title: String,
    description: String,
    #[morph_field(transform = "is_available")]
    is_available: bool,
    #[morph_field(transform = "net_price")]
    price: i32,
}

fn is_available(value: &ProductRow) -> bool {
    value.available_count > 0
}

fn net_price(value: &ProductRow) -> i32 {
    value.base_price - value.discount
}

and then you can simply generate a product info from a product row

let product_row: ProductRow = ProductRow {
    id: 10,
    name: "The Rust Programming Language".to_string(),
    description: "The official book on the Rust programming language".to_string(),
    available_count: 10,
    base_price: 50,
    discount: 10,
    created_at: DateTime::now(),
    updated_at: DateTime::now(),
};

let product_info: ProductInfo = ProductInfo::from(product_row);

This will copy the values for fields with same name (and type) and for the rest one can provide customer transform functions. It does so by implementing a From trait for the source and target struct.

There are 2 types field modifiers

  • transform
#[morph_field(transform = "transform_func")]

this takes an transform function which takes &SourceStruct as a param and returns the correct type

  • select
#[morph_field(select = source_field)]

this takes a source field to replace the value for the field

struct_morph's People

Contributors

shrynx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

emi0x7d1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.