14 releases (8 breaking)
0.10.0 | Apr 14, 2024 |
---|---|
0.9.0 | Aug 11, 2023 |
0.8.0 | Jul 12, 2022 |
0.5.0 | May 2, 2021 |
0.4.2 | May 30, 2019 |
#1110 in Parser implementations
700 downloads per month
Used in wood_derive
80KB
2K
SLoC
Rust Wood
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.
Examples
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();
}