add serde support

wip
This commit is contained in:
Kat Marchán 2024-10-08 22:44:00 -07:00
parent 6841734233
commit 1b39321ca6
No known key found for this signature in database
GPG Key ID: AEB529C08A3C7E9E
4 changed files with 243 additions and 1 deletions

View File

@ -12,7 +12,7 @@ rust-version = "1.82"
edition = "2021" edition = "2021"
[features] [features]
default = ["span"] default = ["span", "serde"]
span = [] span = []
v1-fallback = ["v1"] v1-fallback = ["v1"]
v1 = ["kdlv1"] v1 = ["kdlv1"]
@ -23,6 +23,7 @@ members = ["tools/*"]
[dependencies] [dependencies]
miette.workspace = true miette.workspace = true
num = "0.4.2" num = "0.4.2"
serde = { version = "1.0.210", optional = true }
winnow = { version = "0.7.13", features = ["alloc", "unstable-recover"] } winnow = { version = "0.7.13", features = ["alloc", "unstable-recover"] }
kdlv1 = { package = "kdl", version = "4.7.0", optional = true } kdlv1 = { package = "kdl", version = "4.7.0", optional = true }

235
src/de.rs Normal file
View File

@ -0,0 +1,235 @@
use serde::{de, Deserialize};
use thiserror::Error;
use winnow::{stream::Recoverable, Located};
use crate::{v2_parser::KdlParseError, KdlParseFailure};
/// serde deserializer for KDL documents
#[derive(Debug)]
pub struct Deserializer<'de> {
input: Recoverable<Located<&'de str>, KdlParseError>,
}
impl<'de> Deserializer<'de> {
/// Create a new deserializer from a string
pub fn from_str(input: &'de str) -> Self {
Self {
input: Recoverable::new(Located::new(input)),
}
}
}
/// Deserialize a type from a KDL string
pub fn from_str<'a, T>(input: &'a str) -> Result<T, KdlParseFailure>
where
T: Deserialize<'a>,
{
}
#[derive(Debug, Error)]
struct DeError(String);
impl std::fmt::Display for DeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl de::Error for DeError {
fn custom<T: std::fmt::Display>(msg: T) -> Self {
DeError(msg.to_string())
}
}
struct KdlVisitor;
impl<'de> de::Visitor<'de> for KdlVisitor {
type Value = ();
fn expecting<'a>(&self, formatter: &mut std::fmt::Formatter<'a>) -> std::fmt::Result {
write!(formatter, "a KDL value")
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: de::MapAccess<'de>,
{
while let Some(key) = map.next_key()? {
match key {
"type" => {
let value = map.next_value::<String>()?;
println!("type: {}", value);
}
"value" => {
let value = map.next_value::<String>()?;
println!("value: {}", value);
}
_ => {
map.next_value::<serde::de::IgnoredAny>()?;
}
}
}
Ok(())
}
}
impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
type Error = DeError;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.deserialize_map(visitor)
}
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
fn deserialize_unit_struct<V>(
self,
name: &'static str,
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
fn deserialize_newtype_struct<V>(
self,
name: &'static str,
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
fn deserialize_tuple_struct<V>(
self,
name: &'static str,
len: usize,
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
fn deserialize_struct<V>(
self,
name: &'static str,
fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
fn deserialize_enum<V>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
todo!()
}
}

View File

@ -209,3 +209,8 @@ mod node;
mod value; mod value;
mod v2_parser; mod v2_parser;
#[cfg(feature = "serde")]
pub mod de;
#[cfg(feature = "serde")]
pub mod se;

1
src/se.rs Normal file
View File

@ -0,0 +1 @@