Giter Club home page Giter Club logo

serde_typename's Introduction

crates.io docs

serde_typename

Conveniently serialize and deserialize rust types into / from their serde name.

Usage

use serde::{Serialize, Deserialize};
use serde_typename::{to_str, from_str};

/// Enums

#[derive(Debug, PartialEq, Serialize, Deserialize)]
enum Enum {
    UnitVariant,
    #[serde(rename = "RENAMED")]
    Renamed,
    HoldsData(u8),
    HoldsDataAsTuple(u8, u8),
    HoldsDataAsStruct { field: u8 }
}

// Safe to serialize, held data gets discarded
assert_eq!(to_str(&Enum::UnitVariant).unwrap(), "UnitVariant");
assert_eq!(to_str(&Enum::Renamed).unwrap(), "RENAMED");
assert_eq!(to_str(&Enum::HoldsData(0)).unwrap(), "HoldsData");
assert_eq!(to_str(&Enum::HoldsDataAsTuple(0, 0)).unwrap(), "HoldsDataAsTuple");
assert_eq!(to_str(&Enum::HoldsDataAsStruct { field: 0 }).unwrap(), "HoldsDataAsStruct");

// Safe to deserialize since no data is held
assert_eq!(from_str::<Enum>("UnitVariant").unwrap(), Enum::UnitVariant);
assert_eq!(from_str::<Enum>("RENAMED").unwrap(), Enum::Renamed);
// Cant be deserialized since their data was lost during serialization
assert!(from_str::<Enum>("HoldsData").is_err());
assert!(from_str::<Enum>("HoldsDataAsTuple").is_err());
assert!(from_str::<Enum>("HoldsDataAsStruct").is_err());

/// Structs

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct UnitStruct;
assert_eq!(to_str(&UnitStruct).unwrap(), "UnitStruct");
assert_eq!(from_str::<UnitStruct>("UnitStruct").unwrap(), UnitStruct);

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct TupleStruct(u64, u64);
assert_eq!(to_str(&TupleStruct(0, 0)).unwrap(), "TupleStruct");
assert!(from_str::<TupleStruct>("TupleStruct").is_err());

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Struct {
    field: u8
}
assert_eq!(to_str(&Struct { field: 123 }).unwrap(), "Struct");
assert!(from_str::<Struct>("Struct").is_err());

Acknowledgement

This crate originated as a fork of serde_variant which is maintained by Daniel Mueller [email protected]

serde_typename's People

Contributors

mara-schulke 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.