Giter Site home page Giter Site logo

marshall's Introduction

Marshall

This is a 'compiler' that takes in Rust/Serde data types and produces compatible Python data classes

Example

Start with this...

#[derive(Serialize, Deserialize)]
enum Name {
    None,
    First(String),
    FirstLast(String, String),
    FirstMiddleLast {
        first: String,

        #[serde(skip_serializing_if = "is_empty")]
        middle: Vec<String>,

        last: String,
    },
}

#[derive(Serialize, Deserialize)]
struct User {
    name: Name,

    // this is a comment
    #[serde(default, skip_serializing_if = "under_18")]
    age: u32,

    birthday: (u32, u32, Option<u32>),
}

And get something like this...

# custom wrapper over dataclass
from marshall import *

@dataclass
class None_:
    ENUM_DATA = (ENUM_VARIANT_UNIT, "None")

@dataclass
class First(TupleVariant):
    ENUM_DATA = (ENUM_VARIANT_TUPLE, "First")

    _0: str

@dataclass
class FirstLast(TupleVariant):
    ENUM_DATA = (ENUM_VARIANT_TUPLE, "FirstLast")

    _0: str
    _1: str

@dataclass
class FirstMiddleLast:
    ENUM_DATA = (ENUM_VARIANT_STRUCT, "FirstMiddleLast")
    SKIP_SERIALIZING_IF = {
        "middle": is_empty,
    }

    first: str
    middle: list[str]
    last: str

# this is how we handle Rust-style enums
# when we deserialize a field of this type
# we get the correct variant
Name = None_ | First | FirstLast | FirstMiddleLast

@dataclass
class User:
    SKIP_SERIALIZING_IF = {
        "age": under_18,
    }

    name: Name
    birthday: tuple[int, int, int | None]
    age: int = 0

... and you have mutually compatible types in both languages

So you can dump data with either, and load it with either

The Python version is also careful to respect Serde's semantics with regards to serialize/deserialize skips, and field renames

There's a lot of cases that this doesn't cover

Why would I want this?

You probably don't, and I'm not going to provide any warranty for it ๐Ÿ˜‹

marshall's People

Contributors

george-lewis avatar

Watchers

 avatar

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.