Expand description
§muon-rs
A Rust library for the MuON data format, using serde.
§Deserializing
The easiest way to deserialize data is to derive serde::Deserialize
on
a struct. Then use one of the from_
functions.
§Example
MuON file:
book: Pale Fire
author: Vladimir Nabokov
year: 1962
character: John Shade
location: New Wye
character: Charles Kinbote
location: Zembla
book: The Curious Incident of the Dog in the Night-Time
author: Mark Haddon
year: 2003
character: Christopher Boone
location: Swindon
character: Siobhan
Rust code:
#[derive(Debug, Deserialize, Serialize)]
struct BookList {
book: Vec<Book>,
}
#[derive(Debug, Deserialize, Serialize)]
struct Book {
title: String,
author: String,
year: Option<i16>,
character: Vec<Character>,
}
#[derive(Debug, Deserialize, Serialize)]
struct Character {
name: String,
location: Option<String>,
}
let muon = File::open("tests/books.muon")?;
let books: BookList = muon_rs::from_reader(muon)?;
println!("{:?}", books);
§Serializing
Deriving serde::Serialize
on a struct is just as easy. The
to_
functions are used to serialize MuON data.
§Example
let books = BookList {
book: vec![
Book {
title: "Flight".to_string(),
author: "Sherman Alexie".to_string(),
year: Some(2007),
character: vec![
Character {
name: "Zits".to_string(),
location: Some("Seattle".to_string()),
},
Character {
name: "Justice".to_string(),
location: None,
},
],
},
],
};
let muon = muon_rs::to_string(&books)?;
println!("{:?}", muon);
§Types
MuON types can be mapped to different Rust types.
MuON Type | Rust Types |
---|---|
text | String |
bool | bool |
int | i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize |
number | f32 f64 |
datetime | DateTime |
date | Date |
time | Time |
record | struct implementing Deserialize |
dictionary | HashMap |
any | Value |
Structs§
- Date
- Date with no time or offset
- Date
Time - Date and time with offset
- Deserializer
- Structure that can deserialize MuON into values.
- Serializer
- Structure that can serialize values into MuON.
- Time
- Time with no date or offset
- Time
Offset - Fixed time offset
Enums§
Functions§
- from_
reader - Deserialize
T
from a reader IO stream containing MuON - from_
slice - Deserialize
T
from a byte slice containing MuON - from_
str - Deserialize
T
from a string slice containing MuON - to_
string - Serialize
T
to a String in MuON format - to_vec
- Serialize
T
to a Vec of bytes in MuON format - to_
writer - Serialize
T
to an IO writer in MuON format
Type Aliases§
- Result
- MuON result type