Update comments with links.
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
//! Implement deserialization support of `Dynamic` for [`serde`](https://crates.io/crates/serde).
|
||||
//! Implement deserialization support of [`Dynamic`][crate::Dynamic] for [`serde`].
|
||||
|
||||
use super::str::ImmutableStringDeserializer;
|
||||
use crate::dynamic::Union;
|
||||
use crate::stdlib::{any::type_name, boxed::Box, fmt, string::ToString};
|
||||
use crate::{Dynamic, EvalAltResult, ImmutableString, LexError, ParseErrorType, NO_POS};
|
||||
use crate::{Dynamic, EvalAltResult, ImmutableString, LexError, ParseErrorType, Position::NONE};
|
||||
use serde::de::{
|
||||
DeserializeSeed, Deserializer, Error, IntoDeserializer, MapAccess, SeqAccess, Visitor,
|
||||
};
|
||||
@@ -15,19 +15,19 @@ use crate::Array;
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
use crate::Map;
|
||||
|
||||
/// Deserializer for `Dynamic` which is kept as a reference.
|
||||
/// Deserializer for [`Dynamic`][crate::Dynamic] which is kept as a reference.
|
||||
///
|
||||
/// The reference is necessary because the deserialized type may hold references
|
||||
/// (especially `&str`) to the source `Dynamic`.
|
||||
/// (especially `&str`) to the source [`Dynamic`][crate::Dynamic].
|
||||
pub struct DynamicDeserializer<'a> {
|
||||
value: &'a Dynamic,
|
||||
}
|
||||
|
||||
impl<'de> DynamicDeserializer<'de> {
|
||||
/// Create a `DynamicDeserializer` from a reference to a `Dynamic` value.
|
||||
/// Create a [`DynamicDeserializer`] from a reference to a [`Dynamic`][crate::Dynamic] value.
|
||||
///
|
||||
/// The reference is necessary because the deserialized type may hold references
|
||||
/// (especially `&str`) to the source `Dynamic`.
|
||||
/// (especially `&str`) to the source [`Dynamic`][crate::Dynamic].
|
||||
pub fn from_dynamic(value: &'de Dynamic) -> Self {
|
||||
Self { value }
|
||||
}
|
||||
@@ -37,8 +37,12 @@ impl<'de> DynamicDeserializer<'de> {
|
||||
}
|
||||
/// Shortcut for a type conversion error.
|
||||
fn type_error_str<T>(&self, error: &str) -> Result<T, Box<EvalAltResult>> {
|
||||
EvalAltResult::ErrorMismatchOutputType(error.into(), self.value.type_name().into(), NO_POS)
|
||||
.into()
|
||||
EvalAltResult::ErrorMismatchOutputType(
|
||||
error.into(),
|
||||
self.value.type_name().into(),
|
||||
Position::NONE,
|
||||
)
|
||||
.into()
|
||||
}
|
||||
fn deserialize_int<V: Visitor<'de>>(
|
||||
&mut self,
|
||||
@@ -56,7 +60,7 @@ impl<'de> DynamicDeserializer<'de> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Deserialize a `Dynamic` value into a Rust type that implements `serde::Deserialize`.
|
||||
/// Deserialize a [`Dynamic`][crate::Dynamic] value into a Rust type that implements [`serde::Deserialize`].
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
@@ -117,7 +121,7 @@ impl Error for Box<EvalAltResult> {
|
||||
fn custom<T: fmt::Display>(err: T) -> Self {
|
||||
EvalAltResult::ErrorParsing(
|
||||
ParseErrorType::BadInput(LexError::ImproperSymbol(err.to_string())),
|
||||
NO_POS,
|
||||
Position::NONE,
|
||||
)
|
||||
.into()
|
||||
}
|
||||
@@ -445,7 +449,7 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> {
|
||||
|
||||
/// `SeqAccess` implementation for arrays.
|
||||
struct IterateArray<'a, ITER: Iterator<Item = &'a Dynamic>> {
|
||||
/// Iterator for a stream of `Dynamic` values.
|
||||
/// Iterator for a stream of [`Dynamic`][crate::Dynamic] values.
|
||||
iter: ITER,
|
||||
}
|
||||
|
||||
@@ -479,9 +483,9 @@ where
|
||||
KEYS: Iterator<Item = &'a ImmutableString>,
|
||||
VALUES: Iterator<Item = &'a Dynamic>,
|
||||
{
|
||||
// Iterator for a stream of `Dynamic` keys.
|
||||
// Iterator for a stream of [`Dynamic`][crate::Dynamic] keys.
|
||||
keys: KEYS,
|
||||
// Iterator for a stream of `Dynamic` values.
|
||||
// Iterator for a stream of [`Dynamic`][crate::Dynamic] values.
|
||||
values: VALUES,
|
||||
}
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
//! Helper module defining serialization/deserialization support for [`serde`](https://crates.io/crates/serde).
|
||||
//! Helper module defining serialization/deserialization support for [`serde`].
|
||||
|
||||
pub mod de;
|
||||
pub mod ser;
|
||||
|
@@ -1,7 +1,7 @@
|
||||
//! Implement serialization support of `Dynamic` for [`serde`](https://crates.io/crates/serde).
|
||||
//! Implement serialization support of [`Dynamic`][crate::Dynamic] for [`serde`].
|
||||
|
||||
use crate::stdlib::{boxed::Box, fmt, string::ToString};
|
||||
use crate::{Dynamic, EvalAltResult, NO_POS};
|
||||
use crate::{Dynamic, EvalAltResult, Position::NONE};
|
||||
use serde::ser::{
|
||||
Error, SerializeMap, SerializeSeq, SerializeStruct, SerializeTuple, SerializeTupleStruct,
|
||||
Serializer,
|
||||
@@ -14,7 +14,7 @@ use crate::Array;
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
use crate::Map;
|
||||
|
||||
/// Serializer for `Dynamic` which is kept as a reference.
|
||||
/// Serializer for [`Dynamic`][crate::Dynamic] which is kept as a reference.
|
||||
pub struct DynamicSerializer {
|
||||
/// Buffer to hold a temporary key.
|
||||
_key: Dynamic,
|
||||
@@ -23,7 +23,7 @@ pub struct DynamicSerializer {
|
||||
}
|
||||
|
||||
impl DynamicSerializer {
|
||||
/// Create a `DynamicSerializer` from a `Dynamic` value.
|
||||
/// Create a [`DynamicSerializer`] from a [`Dynamic`][crate::Dynamic] value.
|
||||
pub fn new(_value: Dynamic) -> Self {
|
||||
Self {
|
||||
_key: Default::default(),
|
||||
@@ -32,7 +32,7 @@ impl DynamicSerializer {
|
||||
}
|
||||
}
|
||||
|
||||
/// Serialize a Rust type that implements `serde::Serialize` into a `Dynamic`.
|
||||
/// Serialize a Rust type that implements [`serde::Serialize`] into a [`Dynamic`][crate::Dynamic].
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
@@ -87,7 +87,7 @@ pub fn to_dynamic<T: Serialize>(value: T) -> Result<Dynamic, Box<EvalAltResult>>
|
||||
|
||||
impl Error for Box<EvalAltResult> {
|
||||
fn custom<T: fmt::Display>(err: T) -> Self {
|
||||
EvalAltResult::ErrorRuntime(err.to_string().into(), NO_POS).into()
|
||||
EvalAltResult::ErrorRuntime(err.to_string().into(), Position::NONE).into()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,16 +283,24 @@ impl Serializer for &mut DynamicSerializer {
|
||||
make_variant(_variant, content)
|
||||
}
|
||||
#[cfg(feature = "no_object")]
|
||||
return EvalAltResult::ErrorMismatchOutputType("Dynamic".into(), "map".into(), NO_POS)
|
||||
.into();
|
||||
return EvalAltResult::ErrorMismatchOutputType(
|
||||
"Dynamic".into(),
|
||||
"map".into(),
|
||||
Position::NONE,
|
||||
)
|
||||
.into();
|
||||
}
|
||||
|
||||
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Box<EvalAltResult>> {
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
return Ok(DynamicSerializer::new(Array::new().into()));
|
||||
#[cfg(feature = "no_index")]
|
||||
return EvalAltResult::ErrorMismatchOutputType("Dynamic".into(), "array".into(), NO_POS)
|
||||
.into();
|
||||
return EvalAltResult::ErrorMismatchOutputType(
|
||||
"Dynamic".into(),
|
||||
"array".into(),
|
||||
Position::NONE,
|
||||
)
|
||||
.into();
|
||||
}
|
||||
|
||||
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Box<EvalAltResult>> {
|
||||
@@ -325,7 +333,12 @@ impl Serializer for &mut DynamicSerializer {
|
||||
let err_type = "map";
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
let err_type = "array";
|
||||
EvalAltResult::ErrorMismatchOutputType("Dynamic".into(), err_type.into(), NO_POS).into()
|
||||
EvalAltResult::ErrorMismatchOutputType(
|
||||
"Dynamic".into(),
|
||||
err_type.into(),
|
||||
Position::NONE,
|
||||
)
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -333,8 +346,12 @@ impl Serializer for &mut DynamicSerializer {
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
return Ok(DynamicSerializer::new(Map::new().into()));
|
||||
#[cfg(feature = "no_object")]
|
||||
return EvalAltResult::ErrorMismatchOutputType("Dynamic".into(), "map".into(), NO_POS)
|
||||
.into();
|
||||
return EvalAltResult::ErrorMismatchOutputType(
|
||||
"Dynamic".into(),
|
||||
"map".into(),
|
||||
Position::NONE,
|
||||
)
|
||||
.into();
|
||||
}
|
||||
|
||||
fn serialize_struct(
|
||||
@@ -358,8 +375,12 @@ impl Serializer for &mut DynamicSerializer {
|
||||
map: Map::with_capacity(_len),
|
||||
});
|
||||
#[cfg(feature = "no_object")]
|
||||
return EvalAltResult::ErrorMismatchOutputType("Dynamic".into(), "map".into(), NO_POS)
|
||||
.into();
|
||||
return EvalAltResult::ErrorMismatchOutputType(
|
||||
"Dynamic".into(),
|
||||
"map".into(),
|
||||
Position::NONE,
|
||||
)
|
||||
.into();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -468,7 +489,11 @@ impl SerializeMap for DynamicSerializer {
|
||||
let key = crate::stdlib::mem::take(&mut self._key)
|
||||
.take_immutable_string()
|
||||
.map_err(|typ| {
|
||||
EvalAltResult::ErrorMismatchOutputType("string".into(), typ.into(), NO_POS)
|
||||
EvalAltResult::ErrorMismatchOutputType(
|
||||
"string".into(),
|
||||
typ.into(),
|
||||
Position::NONE,
|
||||
)
|
||||
})?;
|
||||
let _value = _value.serialize(&mut *self)?;
|
||||
let map = self._value.downcast_mut::<Map>().unwrap();
|
||||
@@ -488,7 +513,7 @@ impl SerializeMap for DynamicSerializer {
|
||||
{
|
||||
let _key: Dynamic = _key.serialize(&mut *self)?;
|
||||
let _key = _key.take_immutable_string().map_err(|typ| {
|
||||
EvalAltResult::ErrorMismatchOutputType("string".into(), typ.into(), NO_POS)
|
||||
EvalAltResult::ErrorMismatchOutputType("string".into(), typ.into(), Position::NONE)
|
||||
})?;
|
||||
let _value = _value.serialize(&mut *self)?;
|
||||
let map = self._value.downcast_mut::<Map>().unwrap();
|
||||
|
@@ -1,7 +1,7 @@
|
||||
//! Implement deserialization support of `ImmutableString` for [`serde`](https://crates.io/crates/serde).
|
||||
//! Implement deserialization support of [`ImmutableString`][crate::ImmutableString] for [`serde`].
|
||||
|
||||
use crate::stdlib::{any::type_name, boxed::Box};
|
||||
use crate::{EvalAltResult, ImmutableString, NO_POS};
|
||||
use crate::{EvalAltResult, ImmutableString, Position::NONE};
|
||||
use serde::de::{Deserializer, Visitor};
|
||||
|
||||
/// Deserializer for `ImmutableString`.
|
||||
@@ -16,8 +16,12 @@ impl<'a> ImmutableStringDeserializer<'a> {
|
||||
}
|
||||
/// Shortcut for a type conversion error.
|
||||
fn type_error<T>(&self) -> Result<T, Box<EvalAltResult>> {
|
||||
EvalAltResult::ErrorMismatchOutputType(type_name::<T>().into(), "string".into(), NO_POS)
|
||||
.into()
|
||||
EvalAltResult::ErrorMismatchOutputType(
|
||||
type_name::<T>().into(),
|
||||
"string".into(),
|
||||
Position::NONE,
|
||||
)
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user