#[cfg(feature = "parse")]
pub fn from_str<T>(s: &'_ str) -> Result<T, Error>
where
T: serde::de::DeserializeOwned,
{
T::deserialize(Deserializer::new(s))
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Error {
inner: crate::edit::de::Error,
}
impl Error {
fn new(inner: crate::edit::de::Error) -> Self {
Self { inner }
}
pub(crate) fn add_key(&mut self, key: String) {
self.inner.add_key(key)
}
pub fn message(&self) -> &str {
self.inner.message()
}
#[cfg(feature = "parse")]
pub fn span(&self) -> Option<std::ops::Range<usize>> {
self.inner.span()
}
}
impl serde::de::Error for Error {
fn custom<T>(msg: T) -> Self
where
T: std::fmt::Display,
{
Error::new(crate::edit::de::Error::custom(msg))
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}
impl std::error::Error for Error {}
#[cfg(feature = "parse")]
pub struct Deserializer<'a> {
input: &'a str,
}
#[cfg(feature = "parse")]
impl<'a> Deserializer<'a> {
pub fn new(input: &'a str) -> Self {
Self { input }
}
}
#[cfg(feature = "parse")]
impl<'de, 'a> serde::Deserializer<'de> for Deserializer<'a> {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde::de::Visitor<'de>,
{
let inner = toml_edit::de::Deserializer::parse(self.input).map_err(Error::new)?;
inner.deserialize_any(visitor).map_err(Error::new)
}
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: serde::de::Visitor<'de>,
{
let inner = toml_edit::de::Deserializer::parse(self.input).map_err(Error::new)?;
inner.deserialize_option(visitor).map_err(Error::new)
}
fn deserialize_newtype_struct<V>(
self,
name: &'static str,
visitor: V,
) -> Result<V::Value, Error>
where
V: serde::de::Visitor<'de>,
{
let inner = toml_edit::de::Deserializer::parse(self.input).map_err(Error::new)?;
inner
.deserialize_newtype_struct(name, visitor)
.map_err(Error::new)
}
fn deserialize_struct<V>(
self,
name: &'static str,
fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Error>
where
V: serde::de::Visitor<'de>,
{
let inner = toml_edit::de::Deserializer::parse(self.input).map_err(Error::new)?;
inner
.deserialize_struct(name, fields, visitor)
.map_err(Error::new)
}
fn deserialize_enum<V>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Error>
where
V: serde::de::Visitor<'de>,
{
let inner = toml_edit::de::Deserializer::parse(self.input).map_err(Error::new)?;
inner
.deserialize_enum(name, variants, visitor)
.map_err(Error::new)
}
serde::forward_to_deserialize_any! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
bytes byte_buf map unit
ignored_any unit_struct tuple_struct tuple identifier
}
}
#[cfg(feature = "parse")]
pub struct ValueDeserializer<'a> {
input: &'a str,
}
#[cfg(feature = "parse")]
impl<'a> ValueDeserializer<'a> {
pub fn new(input: &'a str) -> Self {
Self { input }
}
}
#[cfg(feature = "parse")]
impl<'de, 'a> serde::Deserializer<'de> for ValueDeserializer<'a> {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde::de::Visitor<'de>,
{
let inner = self
.input
.parse::<toml_edit::de::ValueDeserializer>()
.map_err(Error::new)?;
inner.deserialize_any(visitor).map_err(Error::new)
}
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: serde::de::Visitor<'de>,
{
let inner = self
.input
.parse::<toml_edit::de::ValueDeserializer>()
.map_err(Error::new)?;
inner.deserialize_option(visitor).map_err(Error::new)
}
fn deserialize_newtype_struct<V>(
self,
name: &'static str,
visitor: V,
) -> Result<V::Value, Error>
where
V: serde::de::Visitor<'de>,
{
let inner = self
.input
.parse::<toml_edit::de::ValueDeserializer>()
.map_err(Error::new)?;
inner
.deserialize_newtype_struct(name, visitor)
.map_err(Error::new)
}
fn deserialize_struct<V>(
self,
name: &'static str,
fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Error>
where
V: serde::de::Visitor<'de>,
{
let inner = self
.input
.parse::<toml_edit::de::ValueDeserializer>()
.map_err(Error::new)?;
inner
.deserialize_struct(name, fields, visitor)
.map_err(Error::new)
}
fn deserialize_enum<V>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Error>
where
V: serde::de::Visitor<'de>,
{
let inner = self
.input
.parse::<toml_edit::de::ValueDeserializer>()
.map_err(Error::new)?;
inner
.deserialize_enum(name, variants, visitor)
.map_err(Error::new)
}
serde::forward_to_deserialize_any! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
bytes byte_buf map unit
ignored_any unit_struct tuple_struct tuple identifier
}
}