Crates.io | wood |
lib.rs | wood |
version | 0.10.0 |
source | src |
created_at | 2019-05-09 05:51:01.883196 |
updated_at | 2024-04-14 00:33:32.918454 |
description | A robust material that is simple to work: Parsing, serialization and deserialization for termpose and woodslist |
homepage | |
repository | https://github.com/makoConstruct/termpose/blob/master/rust/README.md |
max_upload_size | |
id | 132964 |
size | 103,381 |
Wood is a very simple serialization datatype consisting of nested lists of strings.
Termpose, Nakedlist, and Woodslist are text formats that parse into Wood.
The rust library currently has excellent support for termpose and woodslist.
extern crate wood;
use wood::{parse_woodslist, dewoodify};
fn main(){
let r:Vec<usize> = dewoodify(&parse_woodslist("0 1 2").unwrap()).unwrap();
assert_eq!(2, r[2]); //easy as zero one two
}
Although Wood's autoderive isn't as fully featured as serde's (maybe we should make a serde crate for it), it does exist and it does work.
extern crate wood;
extern crate wood_derive;
use wood::{parse_termpose, pretty_termpose, Woodable, Dewoodable};
use wood_derive::{Woodable, Dewoodable};
#[derive(Woodable, Dewoodable, PartialEq, Debug)]
struct Dato {
a:String,
b:bool,
}
fn main(){
let od = Dato{a:"chock".into(), b:true};
let s = pretty_termpose(&od.woodify());
assert_eq!("Dato a:chock b:true", &s);
let d = parse_termpose(&s).and_then(|sw| Dato::dewoodify(&sw)).unwrap();
assert_eq!(&od, &d);
}
There are also these things called wooder combinators. I haven't found a way to make them really useful in rust for various reasons, but they'll be (usually) zero-sized values that you can assemble to specify a translation between wood and data (you usually explain both directions at once. I wanna call them "bifunctions").
extern crate wood;
extern crate wood_derive;
use wood::{wooder, Wood, Dewooder, parse_multiline_termpose};
use wood_derive::{Woodable, Dewoodable};
#[derive(Woodable, Dewoodable)]
struct Datu {
name: String,
numbers: Vec<u32>,
}
fn main(){
let data:Wood = parse_multiline_termpose("
list
entry 1
entry 2
sublist
Datu name:n numbers(0 1 2)
Datu name:nnn numbers(0 1 2)
entry 3
").unwrap();
let sublist:&Wood = data.find("list").and_then(|l| l.find("sublist")).unwrap();
let _:Vec<Datu> = wooder::TaggedSequenceBi("sublist", wooder::Iden).dewoodify(sublist).unwrap();
}