Crate iodeser

Crate iodeser 

Source
Expand description

§IoDeSer

IoDeSer is a open-source project that allows to deserialize and serialize objects into .io formatted String.

IoDeSer defines traits, methods, macros and implementations for basic Rust types. See this crate’s source code for additional information.

§Status

This crate is in alpha status and should not be used in production environment.

§Design

The main foundation of this project is cross-language compatibility. See the project account for more information about other language libraries status, goals and status.

§Features

IoDeSer supports few features that will add additional de/serialization support for external crates.

At this moment in time the only externally supported crate is chrono. See cargo documentation for examples of specifying optional features.

§Examples

use iodeser::*; // required import

#[derive(IoDeSer, Debug, PartialEq)] // required macro derive IoDeSer, Debug and PartialEq is not required
struct Person<'a> {
    #[io_name("Name")]      // optional renaming
    pub name: &'a str,
    #[io_name("SecondName")]  // optional renaming
    pub second_name: Option<&'a str>,
    #[io_name("LastName")]  // optional renaming
    pub last_name: &'a str,
    #[io_name("Age")]       // optional renaming
    #[io_order(LAST)]       // optional ordering using FIRST or LAST keyword
    pub age: u8,
    #[io_name("Address")]   // optional renaming
    #[io_order(FIRST)]      // optional ordering using FIRST or LAST keyword
    pub address: Vec<Address<'a>>,
}

#[derive(IoDeSer, Debug, PartialEq)] // required macro derive, Debug and PartialEq is not required
struct Address<'a> {
    #[io_order(3)]          // optional ordering using integer
    pub city: &'a str,
    #[io_order(1)]          // optional ordering using integer
    pub number: AddressNumberType<'a>,
    #[io_order(2)]          // optional ordering using integer
    pub street: &'a str,
}

#[derive(IoDeSer, Debug, PartialEq)] // required macro derive, Debug and PartialEq is not required
enum AddressNumberType<'a>{
    Numeric(u16),
    String(&'a str)
}

fn main() {
    let person = Person {
        name: "John",
        second_name: None,
        last_name: "Kowalski",
        age: 21,
        address: vec![
            Address {
                city: "Warsaw",
                number: AddressNumberType::Numeric(65),
                street: "Tęczowa",
            },
            Address {
                city: "Hamburg",
                number: AddressNumberType::String("220a"),
                street: "Strasse",
            },
        ],
    };

    let io_serialization: String = to_io!(&person); // serialization
    println!("{}", &io_serialization);

    let person_deserialization: Person = from_io!(io_serialization, Person).unwrap(); // deserialization
    println!("{:?}", &person_deserialization);

    assert_eq!(person, person_deserialization);
}

Macros§

from_io
Deserializes .io formatted String into Self.
to_io
Serialize this value via reference into .io file format.

Enums§

Error
Represents all errors that might occur during deserialization.

Traits§

IoDeSer
Trait for serializing and deserializing objects into .io formatted String.

Type Aliases§

Result
Alias for a Result with the error type errors::Error

Derive Macros§

IoDeSer
Procedural macro which implements IoDeSer trait for Rust structs using derive attribute.