serde_lexpr/
de.rs

1use std::io;
2
3use serde::de::DeserializeOwned;
4
5use lexpr::parse;
6
7use crate::error::Result;
8use crate::value::from_value;
9
10/// Deserialize an instance of type `T` from an S-expression string, using the
11/// default parser options.
12///
13/// ```
14/// use serde_lexpr::from_str;
15///
16/// let v: Vec<u32> = from_str("(1 2 3)").unwrap();
17/// assert_eq!(v, vec![1, 2, 3]);
18/// ```
19pub fn from_str<T>(s: &str) -> Result<T>
20where
21    T: DeserializeOwned,
22{
23    from_value(&lexpr::from_str(s)?)
24}
25
26/// Deserialize an instance of type `T` from an S-expression string.
27///
28/// ```
29/// use serde_lexpr::{from_str_custom, parse};
30///
31/// let v: Vec<u32> = from_str_custom("[1 2 3]", parse::Options::elisp()).unwrap();
32/// assert_eq!(v, vec![1, 2, 3]);
33/// ```
34pub fn from_str_custom<T>(s: &str, options: parse::Options) -> Result<T>
35where
36    T: DeserializeOwned,
37{
38    from_value(&lexpr::from_str_custom(s, options)?)
39}
40
41/// Deserialize an instance of type `T` from an S-expression byte slice, using the
42/// default parser options.
43///
44/// ```
45/// use serde_lexpr::from_slice;
46///
47/// let v: Vec<u32> = from_slice(b"(1 2 3)").unwrap();
48/// assert_eq!(v, vec![1, 2, 3]);
49/// ```
50pub fn from_slice<T>(s: &[u8]) -> Result<T>
51where
52    T: DeserializeOwned,
53{
54    from_value(&lexpr::from_slice(s)?)
55}
56
57/// Deserialize an instance of type `T` from an S-expression byte slice, using the
58/// default parser options.
59///
60/// ```
61/// use serde_lexpr::{from_slice_custom, parse};
62///
63/// let v: Vec<u32> = from_slice_custom(b"[1 2 3]", parse::Options::elisp()).unwrap();
64/// assert_eq!(v, vec![1, 2, 3]);
65/// ```
66pub fn from_slice_custom<T>(s: &[u8], options: parse::Options) -> Result<T>
67where
68    T: DeserializeOwned,
69{
70    from_value(&lexpr::from_slice_custom(s, options)?)
71}
72
73/// Parse a value from an input stream of S-expressions, using the
74/// default parser options.
75///
76/// ```
77/// use serde_lexpr::from_reader;
78///
79/// let cursor = std::io::Cursor::new(b"(1 2 3)");
80/// let v: Vec<u32> = from_reader(cursor).unwrap();
81/// assert_eq!(v, vec![1, 2, 3]);
82/// ```
83pub fn from_reader<T>(rdr: impl io::Read) -> Result<T>
84where
85    T: DeserializeOwned,
86{
87    from_value(&lexpr::from_reader(rdr)?)
88}
89
90/// Parse a value from an input stream of S-expressions, using the
91/// default parser options.
92///
93/// ```
94/// use serde_lexpr::{from_reader_custom, parse};
95///
96/// let cursor = std::io::Cursor::new(b"(1 2 3)");
97/// let v: Vec<u32> = from_reader_custom(cursor, parse::Options::elisp()).unwrap();
98/// assert_eq!(v, vec![1, 2, 3]);
99/// ```
100pub fn from_reader_custom<T>(rdr: impl io::Read, options: parse::Options) -> Result<T>
101where
102    T: DeserializeOwned,
103{
104    from_value(&lexpr::from_reader_custom(rdr, options)?)
105}