13 unstable releases (3 breaking)
0.4.1 | Feb 23, 2024 |
---|---|
0.4.0 | Feb 22, 2024 |
0.3.8 | Feb 22, 2024 |
0.2.2 | Feb 4, 2024 |
0.1.3 | Feb 3, 2024 |
#557 in Parser implementations
90 downloads per month
42KB
740 lines
Contains (Cab file, 14KB) .vs/rust-library/v17/.wsuo
About
Repository stores code for Rust library that allows to read from and write to .io file format.
Functions and plans
The current status of both serialization and deserialization:
- Primitive types
- Strings
- Arrays
- Vectors
- Hashmaps
- Structs (Named{} and tuple())
- Generics
- Tuples
- &str type
- Slices
- Option
- Result
- Combinations of all above
- Enums
Full list of supported types can be found in this crate's documentation.
Capabilities
- Serialization of supported types using macro to_io!() using objects reference,
- Deserialization of supported types using macro from_io!() using .io formatted String and wanted objects type,
- Renaming structs fields in and from .io formatted String using #[io_name()] helper macro using String literal as argument.
- Ordering structs fields in and from .io formatted String using #[io_order()] helper macro using either FIRST and LAST keywords or an i16 Integer.
See example below for usage of those capabilities.
Example usage
use iodeser::*; // required import
#[derive(IoDeSer, Debug, PartialEq)] // required macro derive IoDeSer, Debug and PartialEq is not required
struct Person<'a, T: IoDeSer> {
#[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, T>>,
}
#[derive(IoDeSer, Debug, PartialEq)] // required macro derive, Debug and PartialEq is not required
struct Address<'a, T: IoDeSer> {
#[io_order(3)] // optional ordering using integer
pub city: &'a str,
#[io_order(1)] // optional ordering using integer
pub number: T,
#[io_order(2)] // optional ordering using integer
pub street: &'a str,
}
fn main() {
let person = Person::<u8> {
name: "John",
second_name: None,
last_name: "Kowalski",
age: 21,
address: vec![
Address::<u8> {
city: "Warsaw",
number: 65,
street: "Tęczowa",
},
Address::<u8> {
city: "Hamburg",
number: 220,
street: "Strasse",
},
],
};
let io_serialization: String = to_io!(&person); // serialization
println!("{}", &io_serialization);
let person_deserialization: Person<u8> = from_io!(io_serialization, Person<u8>).unwrap(); // deserialization
println!("{:?}", &person_deserialization);
assert_eq!(person, person_deserialization);
}
/*
Output:
|
Address->|
|
number->|65|
street->|Tęczowa|
city->|Warsaw|
|
+
|
number->|220|
street->|Strasse|
city->|Hamburg|
|
|
Name->|John|
SecondName->|||
LastName->|Kowalski|
Age->|21|
|
Person { name: "John", second_name: None, last_name: "Kowalski", age: 21, address: [Address { city: "Warsaw", number: 65, street: "Tęczowa" }, Address { city: "Hamburg", number: 220, street: "Strasse" }] }
*/
Dependencies
~1–1.7MB
~38K SLoC