1
#![no_std]
2
#![doc = include_str!("../README.md")]
3
#![warn(clippy::pedantic)]
4
#![warn(missing_docs)]
5
#![allow(clippy::module_name_repetitions)]
6

            
7
extern crate alloc;
8

            
9
#[cfg(feature = "std")]
10
extern crate std;
11

            
12
/// Serde deserialization support.
13
#[cfg(feature = "serde")]
14
pub mod de;
15
/// Parse data or a reader into a sequence of Rsn events.
16
pub mod parser;
17
/// Serde serialization support.
18
#[cfg(feature = "serde")]
19
pub mod ser;
20
/// Parse data or a reader into a sequence of tokens.
21
pub mod tokenizer;
22
/// Types for generically representing the parsed value from an Rsn document.
23
pub mod value;
24
/// Types for writing Rsn.
25
pub mod writer;
26

            
27
/// Deserializes `D` from `source` using the default Rsn
28
/// [`Config`](parser::Config).
29
///
30
/// ```rust
31
/// let deserialized: Vec<usize> = rsn::from_str("[1, 2, 3]").unwrap();
32
/// assert_eq!(deserialized, vec![1, 2, 3]);
33
/// ```
34
///
35
/// # Errors
36
///
37
/// Returns an error if `source` isn't valid Rsn or cannot be deserialized as
38
/// `D`.
39
#[cfg(feature = "serde")]
40
32
pub fn from_str<'de, D: serde::Deserialize<'de>>(source: &'de str) -> Result<D, de::Error> {
41
32
    parser::Config::default().deserialize(source)
42
32
}
43

            
44
/// Deserializes `D` from `slice` using the default Rsn
45
/// [`Config`](parser::Config).
46
///
47
/// ```rust
48
/// let deserialized: Vec<usize> = rsn::from_slice(b"[1, 2, 3]").unwrap();
49
/// assert_eq!(deserialized, vec![1, 2, 3]);
50
/// ```
51
///
52
/// # Errors
53
///
54
/// Returns an error if `slice` isn't valid Rsn or cannot be deserialized as
55
/// `D`.
56
#[cfg(feature = "serde")]
57
pub fn from_slice<'de, D: serde::Deserialize<'de>>(source: &'de [u8]) -> Result<D, de::Error> {
58
    parser::Config::default().deserialize_from_slice(source)
59
}
60

            
61
/// Deserializes `D` from `reader` using the default Rsn
62
/// [`Config`](parser::Config).
63
///
64
/// ```rust
65
/// let deserialized: Vec<usize> = rsn::from_reader(&b"[1, 2, 3]"[..]).unwrap();
66
/// assert_eq!(deserialized, vec![1, 2, 3]);
67
/// ```
68
///
69
/// # Errors
70
///
71
/// Returns an error if `reader` returns an error while reading, doesn't contain
72
/// valid Rsn, or cannot be deserialized as `D`.
73
#[cfg(all(feature = "serde", feature = "std"))]
74
pub fn from_reader<D: serde::de::DeserializeOwned, R: std::io::Read>(
75
    reader: R,
76
) -> Result<D, de::Error> {
77
    parser::Config::default().deserialize_from_reader(reader)
78
}
79

            
80
/// Serializes `value` into a `String` using the default Rsn
81
/// [`Config`](ser::Config).
82
///
83
/// ```rust
84
/// let serialized = rsn::to_string(&vec![1, 2, 3]).unwrap();
85
/// assert_eq!(serialized, "[1,2,3]");
86
/// ```
87
///
88
/// # Errors
89
///
90
/// Rsn itself does not produce any errors while serializing values. This
91
/// function will return errors that arise within `Serialize` implementations
92
/// encountered while serializing `value`.
93
#[cfg(feature = "serde")]
94
5
pub fn to_string<S: serde::Serialize>(
95
5
    value: &S,
96
5
) -> Result<alloc::string::String, core::fmt::Error> {
97
5
    ser::Config::default().serialize(value)
98
5
}
99

            
100
/// Serializes `value` into a `Vec<u8>` using the default Rsn
101
/// [`Config`](ser::Config).
102
///
103
/// ```rust
104
/// let serialized = rsn::to_vec(&vec![1, 2, 3]).unwrap();
105
/// assert_eq!(serialized, b"[1,2,3]");
106
/// ```
107
///
108
/// # Errors
109
///
110
/// Rsn itself does not produce any errors while serializing values. This
111
/// function will return errors that arise within `Serialize` implementations
112
/// encountered while serializing `value`.
113
#[cfg(feature = "serde")]
114
pub fn to_vec<S: serde::Serialize>(value: &S) -> Result<alloc::vec::Vec<u8>, core::fmt::Error> {
115
    ser::Config::default().serialize_to_vec(value)
116
}
117

            
118
/// Serializes `value` into a writer using the default Rsn
119
/// [`Config`](ser::Config).
120
///
121
/// ```rust
122
/// let mut serialized = Vec::new();
123
/// rsn::to_writer(&vec![1, 2, 3], &mut serialized).unwrap();
124
/// assert_eq!(serialized, b"[1,2,3]");
125
/// ```
126
///
127
/// # Errors
128
///
129
/// Returns any errors occurring while serializing `value` or while writing to
130
/// `writer`.
131
#[cfg(all(feature = "serde", feature = "std"))]
132
pub fn to_writer<S: serde::Serialize, W: std::io::Write>(
133
    value: &S,
134
    writer: W,
135
) -> std::io::Result<usize> {
136
    ser::Config::default().serialize_to_writer(value, writer)
137
}
138

            
139
/// Serializes `value` into a `String` using
140
/// [`Config::pretty()`](ser::Config::pretty()).
141
///
142
/// ```rust
143
/// let input = vec![1, 2, 3];
144
/// let serialized = rsn::to_string_pretty(&input).unwrap();
145
/// assert_eq!(serialized, "[\n  1,\n  2,\n  3\n]");
146
/// ```
147
///
148
/// # Errors
149
///
150
/// Rsn itself does not produce any errors while serializing values. This
151
/// function will return errors that arise within `Serialize` implementations
152
/// encountered while serializing `value`.
153
#[cfg(feature = "serde")]
154
4
pub fn to_string_pretty<S: serde::Serialize>(
155
4
    value: &S,
156
4
) -> Result<alloc::string::String, core::fmt::Error> {
157
4
    ser::Config::pretty().serialize(value)
158
4
}
159

            
160
#[cfg(test)]
161
mod tests;