Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Serde support #83

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ rust-version = "1.56.0"
edition = "2021"

[features]
default = ["span"]
default = ["span", "serde"]
span = []

[dependencies]
miette = "7.2.0"
num = "0.4.2"
serde = { version = "1.0.210", optional = true }
thiserror = "1.0.40"
winnow = { version = "0.6.20", features = ["alloc", "unstable-recover"] }

Expand Down
202 changes: 202 additions & 0 deletions src/de.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
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())
}
}

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!()
}
}
9 changes: 7 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub struct KdlDiagnostic {
pub kind: KdlErrorKind,
}

/// A type reprenting additional information specific to the type of error being returned.
/// A type representing additional information specific to the type of error being returned.
#[derive(Debug, Diagnostic, Clone, Eq, PartialEq, Error)]
pub enum KdlErrorKind {
/// An error occurred while parsing an integer.
Expand All @@ -91,6 +91,11 @@ pub enum KdlErrorKind {
#[diagnostic(code(kdl::parse_float))]
ParseFloatError(ParseFloatError),

/// Tried to parse a negative number as an unsigned integer.
#[error("Tried to parse a negative number as an unsigned integer.")]
#[diagnostic(code(kdl::negative_unsigned))]
NegativeUnsignedError,

/// Generic parsing error. The given context string denotes the component
/// that failed to parse.
#[error("Expected {0}.")]
Expand All @@ -102,4 +107,4 @@ pub enum KdlErrorKind {
#[error("An unspecified parse error occurred.")]
#[diagnostic(code(kdl::other))]
Other,
}
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,8 @@ mod node;
mod value;

mod v2_parser;

#[cfg(feature = "serde")]
pub mod de;
#[cfg(feature = "serde")]
pub mod se;
1 change: 1 addition & 0 deletions src/se.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Loading
Loading