-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip
- Loading branch information
Showing
4 changed files
with
243 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|