Skip to content

Commit

Permalink
refactored number parsers so serde can use them
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Oct 11, 2024
1 parent 6c82211 commit 35c3f05
Show file tree
Hide file tree
Showing 4 changed files with 394 additions and 88 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ 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
183 changes: 183 additions & 0 deletions src/de.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
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 @@ -90,7 +90,12 @@ pub enum KdlErrorKind {
#[error(transparent)]
#[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 Down
Loading

0 comments on commit 35c3f05

Please sign in to comment.