Skip to content

Commit 1384083

Browse files
committed
add serde support
wip
1 parent ec73cdf commit 1384083

File tree

4 files changed

+243
-1
lines changed

4 files changed

+243
-1
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ rust-version = "1.70.0"
1212
edition = "2021"
1313

1414
[features]
15-
default = ["span"]
15+
default = ["span", "serde"]
1616
span = []
1717
v1-fallback = ["v1"]
1818
v1 = ["kdlv1"]
1919

2020
[dependencies]
2121
miette = "7.2.0"
2222
num = "0.4.2"
23+
serde = { version = "1.0.210", optional = true }
2324
thiserror = "1.0.40"
2425
winnow = { version = "0.6.20", features = ["alloc", "unstable-recover"] }
2526
kdlv1 = { package = "kdl", version = "4.7.0", optional = true }

src/de.rs

+235
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
use serde::{de, Deserialize};
2+
use thiserror::Error;
3+
use winnow::{stream::Recoverable, Located};
4+
5+
use crate::{v2_parser::KdlParseError, KdlParseFailure};
6+
7+
/// serde deserializer for KDL documents
8+
#[derive(Debug)]
9+
pub struct Deserializer<'de> {
10+
input: Recoverable<Located<&'de str>, KdlParseError>,
11+
}
12+
13+
impl<'de> Deserializer<'de> {
14+
/// Create a new deserializer from a string
15+
pub fn from_str(input: &'de str) -> Self {
16+
Self {
17+
input: Recoverable::new(Located::new(input)),
18+
}
19+
}
20+
}
21+
22+
/// Deserialize a type from a KDL string
23+
pub fn from_str<'a, T>(input: &'a str) -> Result<T, KdlParseFailure>
24+
where
25+
T: Deserialize<'a>,
26+
{
27+
}
28+
29+
#[derive(Debug, Error)]
30+
struct DeError(String);
31+
32+
impl std::fmt::Display for DeError {
33+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34+
write!(f, "{}", self.0)
35+
}
36+
}
37+
38+
impl de::Error for DeError {
39+
fn custom<T: std::fmt::Display>(msg: T) -> Self {
40+
DeError(msg.to_string())
41+
}
42+
}
43+
44+
struct KdlVisitor;
45+
46+
impl<'de> de::Visitor<'de> for KdlVisitor {
47+
type Value = ();
48+
49+
fn expecting<'a>(&self, formatter: &mut std::fmt::Formatter<'a>) -> std::fmt::Result {
50+
write!(formatter, "a KDL value")
51+
}
52+
53+
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
54+
where
55+
A: de::MapAccess<'de>,
56+
{
57+
while let Some(key) = map.next_key()? {
58+
match key {
59+
"type" => {
60+
let value = map.next_value::<String>()?;
61+
println!("type: {}", value);
62+
}
63+
"value" => {
64+
let value = map.next_value::<String>()?;
65+
println!("value: {}", value);
66+
}
67+
_ => {
68+
map.next_value::<serde::de::IgnoredAny>()?;
69+
}
70+
}
71+
}
72+
73+
Ok(())
74+
}
75+
}
76+
77+
impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
78+
type Error = DeError;
79+
80+
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
81+
where
82+
V: de::Visitor<'de>,
83+
{
84+
self.deserialize_map(visitor)
85+
}
86+
87+
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
88+
where
89+
V: de::Visitor<'de>,
90+
{
91+
todo!()
92+
}
93+
94+
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
95+
where
96+
V: de::Visitor<'de>,
97+
{
98+
todo!()
99+
}
100+
101+
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
102+
where
103+
V: de::Visitor<'de>,
104+
{
105+
todo!()
106+
}
107+
108+
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
109+
where
110+
V: de::Visitor<'de>,
111+
{
112+
todo!()
113+
}
114+
115+
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
116+
where
117+
V: de::Visitor<'de>,
118+
{
119+
todo!()
120+
}
121+
122+
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
123+
where
124+
V: de::Visitor<'de>,
125+
{
126+
todo!()
127+
}
128+
129+
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
130+
where
131+
V: de::Visitor<'de>,
132+
{
133+
todo!()
134+
}
135+
136+
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
137+
where
138+
V: de::Visitor<'de>,
139+
{
140+
todo!()
141+
}
142+
143+
fn deserialize_unit_struct<V>(
144+
self,
145+
name: &'static str,
146+
visitor: V,
147+
) -> Result<V::Value, Self::Error>
148+
where
149+
V: de::Visitor<'de>,
150+
{
151+
todo!()
152+
}
153+
154+
fn deserialize_newtype_struct<V>(
155+
self,
156+
name: &'static str,
157+
visitor: V,
158+
) -> Result<V::Value, Self::Error>
159+
where
160+
V: de::Visitor<'de>,
161+
{
162+
todo!()
163+
}
164+
165+
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
166+
where
167+
V: de::Visitor<'de>,
168+
{
169+
todo!()
170+
}
171+
172+
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
173+
where
174+
V: de::Visitor<'de>,
175+
{
176+
todo!()
177+
}
178+
179+
fn deserialize_tuple_struct<V>(
180+
self,
181+
name: &'static str,
182+
len: usize,
183+
visitor: V,
184+
) -> Result<V::Value, Self::Error>
185+
where
186+
V: de::Visitor<'de>,
187+
{
188+
todo!()
189+
}
190+
191+
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
192+
where
193+
V: de::Visitor<'de>,
194+
{
195+
todo!()
196+
}
197+
198+
fn deserialize_struct<V>(
199+
self,
200+
name: &'static str,
201+
fields: &'static [&'static str],
202+
visitor: V,
203+
) -> Result<V::Value, Self::Error>
204+
where
205+
V: de::Visitor<'de>,
206+
{
207+
todo!()
208+
}
209+
210+
fn deserialize_enum<V>(
211+
self,
212+
name: &'static str,
213+
variants: &'static [&'static str],
214+
visitor: V,
215+
) -> Result<V::Value, Self::Error>
216+
where
217+
V: de::Visitor<'de>,
218+
{
219+
todo!()
220+
}
221+
222+
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
223+
where
224+
V: de::Visitor<'de>,
225+
{
226+
todo!()
227+
}
228+
229+
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
230+
where
231+
V: de::Visitor<'de>,
232+
{
233+
todo!()
234+
}
235+
}

src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,8 @@ mod node;
203203
mod value;
204204

205205
mod v2_parser;
206+
207+
#[cfg(feature = "serde")]
208+
pub mod de;
209+
#[cfg(feature = "serde")]
210+
pub mod se;

src/se.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)