Skip to content

Commit

Permalink
json5 error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasoa committed Apr 6, 2024
1 parent 9653520 commit bec2101
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 87 deletions.
119 changes: 35 additions & 84 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@
// except according to those terms.

use crate::error::{Error, Result};
use serde::de::{
self, Deserialize, DeserializeSeed, EnumAccess, IntoDeserializer,
MapAccess, SeqAccess, VariantAccess, Visitor,
};
use std::{
io::Read,
ops::{AddAssign, MulAssign, Neg},
};
use serde::de::{self, Deserialize, Visitor};
use std::io::Read;

pub struct Deserializer<R: Read> {
// This string starts with the input data and characters are truncated off
Expand Down Expand Up @@ -204,7 +198,7 @@ impl<R: Read> Deserializer<R> {
match header.element_type {
ElementType::Int => self.read_json_compatible(header),
ElementType::Int5 => self.read_json5_compatible(header),
t => return Err(Error::UnexpectedType(t)),
t => Err(Error::UnexpectedType(t)),
}
}
}
Expand Down Expand Up @@ -306,20 +300,14 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {
visitor.visit_u64(self.read_integer(header)?)
}

fn deserialize_option<V>(
self,
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
fn deserialize_option<V>(self, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
todo!()
}

fn deserialize_unit<V>(
self,
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
fn deserialize_unit<V>(self, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
Expand All @@ -328,9 +316,9 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {

fn deserialize_unit_struct<V>(
self,
name: &'static str,
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
_name: &'static str,
_visitor: V,
) -> Result<V::Value>
where
V: Visitor<'de>,
{
Expand All @@ -339,30 +327,23 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {

fn deserialize_newtype_struct<V>(
self,
name: &'static str,
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
_name: &'static str,
_visitor: V,
) -> Result<V::Value>
where
V: Visitor<'de>,
{
todo!()
}

fn deserialize_seq<V>(
self,
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
fn deserialize_seq<V>(self, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
todo!()
}

fn deserialize_tuple<V>(
self,
len: usize,
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
fn deserialize_tuple<V>(self, _len: usize, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
Expand All @@ -371,20 +352,17 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {

fn deserialize_tuple_struct<V>(
self,
name: &'static str,
len: usize,
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
_name: &'static str,
_len: usize,
_visitor: V,
) -> Result<V::Value>
where
V: Visitor<'de>,
{
todo!()
}

fn deserialize_map<V>(
self,
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
fn deserialize_map<V>(self, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
Expand All @@ -393,10 +371,10 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {

fn deserialize_struct<V>(
self,
name: &'static str,
fields: &'static [&'static str],
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
_name: &'static str,
_fields: &'static [&'static str],
_visitor: V,
) -> Result<V::Value>
where
V: Visitor<'de>,
{
Expand All @@ -405,100 +383,73 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {

fn deserialize_enum<V>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
_name: &'static str,
_variants: &'static [&'static str],
_visitor: V,
) -> Result<V::Value>
where
V: Visitor<'de>,
{
todo!()
}

fn deserialize_identifier<V>(
self,
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
fn deserialize_identifier<V>(self, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
todo!()
}

fn deserialize_ignored_any<V>(
self,
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
fn deserialize_ignored_any<V>(self, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
todo!()
}

fn deserialize_f32<V>(
self,
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
fn deserialize_f32<V>(self, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
todo!()
}

fn deserialize_f64<V>(
self,
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
fn deserialize_f64<V>(self, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
todo!()
}

fn deserialize_char<V>(
self,
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
fn deserialize_char<V>(self, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
todo!()
}

fn deserialize_str<V>(
self,
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
fn deserialize_str<V>(self, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
todo!()
}

fn deserialize_string<V>(
self,
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
fn deserialize_string<V>(self, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
todo!()
}

fn deserialize_bytes<V>(
self,
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
fn deserialize_bytes<V>(self, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
todo!()
}

fn deserialize_byte_buf<V>(
self,
visitor: V,
) -> std::prelude::v1::Result<V::Value, Self::Error>
fn deserialize_byte_buf<V>(self, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::JsonError(e) => Some(e),
Error::JsonError(e) => Some(e),
Error::Json5Error(e) => Some(e),
Error::Io(e) => Some(e),
_ => None,
}
Expand Down
15 changes: 13 additions & 2 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@ pub(crate) use serde_json5::from_reader as parse_json5;

#[cfg(not(feature = "serde_json5"))]
pub(crate) fn parse_json5<I, T>(_input: I) -> crate::Result<T> {
Err(crate::Error::Json5Error("Json5 data was encountered, but json5 support is not enabled. Enable the `serde_json5` feature of the serde-sqlite-jsonb crate to enable support for json5 data."))
Err(crate::Error::Json5Error(Json5Error))
}

#[cfg(feature = "serde_json5")]
pub(crate) type Json5Error = serde_json5::Error;
#[cfg(not(feature = "serde_json5"))]
pub(crate) type Json5Error = &'static str;
#[derive(Debug)]
pub(crate) struct Json5Error;

#[cfg(not(feature = "serde_json5"))]
impl std::fmt::Display for Json5Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Json5 data was encountered, but json5 support is not enabled. Enable the `serde_json5` feature of the serde-sqlite-jsonb crate to enable support for json5 data.")
}
}

#[cfg(not(feature = "serde_json5"))]
impl std::error::Error for Json5Error {}

0 comments on commit bec2101

Please sign in to comment.