deser_hjson/
lib.rs

1/*! A Serde deserializer for Hjson
2
3```
4use {
5    deser_hjson::*,
6    serde::Deserialize,
7    std::collections::HashMap,
8};
9// This example comes from https://hjson.github.io/
10let hjson = r#"
11// use #, // or /**/ for comments,
12// omit quotes for keys
13key: 1
14// omit quotes for strings
15contains: everything on this line
16// omit commas at the end of a line
17cool: {
18  foo: 1
19  bar: 2
20}
21// allow trailing commas
22list: [
23  1,
24  2,
25]
26// and use multiline strings
27realist:
28  '''
29  My half empty glass,
30  I will fill your empty half.
31  Now you are half full.
32  '''
33"#;
34// we'll deserialize it into this struct:
35#[derive(Deserialize, PartialEq, Debug)]
36struct Example {
37    key: i32,
38    contains: Option<String>,
39    cool: HashMap<String, u16>,
40    list: Vec<usize>,
41    realist: String,
42    missing: Option<f64>,
43}
44let mut cool = HashMap::new();
45cool.insert("foo".to_owned(), 1);
46cool.insert("bar".to_owned(), 2);
47let expected = Example {
48    key: 1,
49    contains: Some("everything on this line".to_owned()),
50    cool,
51    list: vec![1, 2],
52    realist: "My half empty glass,\nI will fill your empty half.\nNow you are half full.".to_owned(),
53    missing: None,
54};
55assert_eq!(expected, from_str(hjson).unwrap());
56
57```
58*/
59
60mod de;
61mod de_enum;
62mod de_map;
63mod de_number;
64mod de_seq;
65mod error;
66mod utf8;
67
68pub use error::*;
69
70/// Deserialize an instance of type `T` from a reader of Hjson text
71///
72/// # Example
73///
74/// ```
75/// use serde::Deserialize;
76/// use std::io::Cursor;
77///
78/// #[derive(Deserialize, Debug)]
79/// struct User {
80///     fingerprint: String,
81///     location: String,
82/// }
83///
84/// // The type of `j` is `Cursor` which implements the `Read` trait
85/// let j = Cursor::new("
86///     fingerprint: 0xF9BA143B95FF6D82
87///     location: Menlo Park, CA
88/// ");
89///
90/// let u: User = deser_hjson::from_reader(j).unwrap();
91/// println!("{:#?}", u);
92/// ```
93pub fn from_reader<R, T>(mut reader: R) -> Result<T>
94where
95    R: std::io::Read,
96    T: serde::de::DeserializeOwned,
97{
98    let mut buf = Vec::new();
99    reader.read_to_end(&mut buf)?;
100    from_slice(&buf)
101}
102
103
104/// Deserialize an instance of type `T` from bytes of Hjson text
105///
106/// # Example
107///
108/// ```
109/// use serde::Deserialize;
110///
111/// #[derive(Deserialize, Debug)]
112/// struct User {
113///     fingerprint: String,
114///     location: String,
115/// }
116///
117/// // The type of `j` is `&[u8]`
118/// let j = b"
119///     fingerprint: 0xF9BA143B95FF6D82
120///     location: Menlo Park, CA
121/// ";
122///
123/// let u: User = deser_hjson::from_slice(j).unwrap();
124/// println!("{:#?}", u);
125/// ```
126pub fn from_slice<T>(bytes: &[u8]) -> Result<T>
127where
128    T: serde::de::DeserializeOwned,
129{
130    let s = std::str::from_utf8(bytes)?;
131    from_str(s)
132}
133
134
135/// Deserialize an instance of type `T` from a string of Hjson text
136///
137/// # Example
138///
139/// ```
140/// use serde::Deserialize;
141///
142/// #[derive(Deserialize, Debug)]
143/// struct User {
144///     hands: Option<u16>,
145///     location: String,
146/// }
147///
148/// // The type of `j` is `&str`
149/// let j = "
150///     hands: 2
151///     location: Menlo Park, CA
152/// ";
153///
154/// let u: User = deser_hjson::from_str(j).unwrap();
155/// println!("{:#?}", u);
156/// ```
157pub fn from_str<T>(s: &str) -> Result<T>
158where
159    T: serde::de::DeserializeOwned,
160{
161    let mut deserializer = de::Deserializer::from_str(s);
162    let t = T::deserialize(&mut deserializer)?;
163    deserializer.check_all_consumed()?;
164    Ok(t)
165}