hipack/
lib.rs

1// Copyright 2015-2016 hipack-rs developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9pub use self::value::Value;
10pub mod value;
11
12pub use self::parser::Parser;
13pub mod parser;
14
15pub use self::writer::Writer;
16pub mod writer;
17
18use std::collections::BTreeMap;
19use std::io::{Cursor, Write, Read};
20use std::io;
21
22
23pub fn parse_string(input: &str) -> parser::Result<Value> {
24    Parser::new(Cursor::new(input.as_bytes()).bytes()).parse_message()
25}
26
27pub fn parse<Iter>(input: Iter) -> parser::Result<Value>
28    where Iter: Iterator<Item=io::Result<u8>>
29{
30    Parser::new(input).parse_message()
31}
32
33pub fn write_string(value: &BTreeMap<String, Value>) -> String {
34    let mut output = Cursor::new(Vec::new());
35    Writer::pretty(&mut output).write_message(value).ok();
36    String::from_utf8(output.into_inner()).unwrap()
37}
38
39pub fn write<W>(writer: &mut  W, value: &BTreeMap<String, Value>) -> io::Result<()>
40    where W: Write
41{
42    Writer::pretty(writer).write_message(value)
43}