usecrate::error::{self, Error, ErrorImpl, Result}; usecrate::path::Path; use serde::de::{ self, Deserialize, DeserializeOwned, DeserializeSeed, Expected, IgnoredAny as Ignore,
IntoDeserializer, Unexpected, Visitor,
}; use std::collections::BTreeMap; use std::f64; use std::fmt; use std::io; use std::marker::PhantomData; use std::mem; use std::str; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; use yaml_rust::parser::{Event as YamlEvent, MarkedEventReceiver, Parser}; use yaml_rust::scanner::{Marker, TScalarStyle, TokenType};
/// A structure that deserializes YAML into Rust values. /// /// # Examples /// /// Deserializing a single document: /// /// ``` /// use anyhow::Result; /// use serde::Deserialize; /// use serde_yaml::Value; /// /// fn main() -> Result<()> { /// let input = "---\nk: 107\n"; /// let de = serde_yaml::Deserializer::from_str(input); /// let value = Value::deserialize(de)?; /// println!("{:?}", value); /// Ok(()) /// } /// ``` /// /// Deserializing multi-doc YAML: /// /// ``` /// use anyhow::Result; /// use serde::Deserialize; /// use serde_yaml::Value; /// /// fn main() -> Result<()> { /// let input = "---\nk: 107\n...\n---\nj: 106\n"; /// /// for document in serde_yaml::Deserializer::from_str(input) { /// let value = Value::deserialize(document)?; /// println!("{:?}", value); /// } /// /// Ok(()) /// } /// ``` pubstruct Deserializer<'a> {
input: Input<'a>,
}
impl<'a> Deserializer<'a> { /// Creates a YAML deserializer from a `&str`. pubfn from_str(s: &'a str) -> Self { let input = Input::Str(s);
Deserializer { input }
}
/// Creates a YAML deserializer from a `&[u8]`. pubfn from_slice(v: &'a [u8]) -> Self { let input = Input::Slice(v);
Deserializer { input }
}
/// Creates a YAML deserializer from an `io::Read`. /// /// Reader-based deserializers do not support deserializing borrowed types /// like `&str`, since the `std::io::Read` trait has no non-copying methods /// -- everything it does involves copying bytes out of the data source. pubfn from_reader<R>(rdr: R) -> Self where
R: io::Read + 'a,
{ let input = Input::Read(Box::new(rdr));
Deserializer { input }
}
struct DeserializerFromEvents<'a> {
events: &'a [(Event, Marker)], /// Map from alias id to index in events.
aliases: &'a BTreeMap<usize, usize>,
pos: &'a mut usize,
path: Path<'a>,
remaining_depth: u8,
}
match event {
Event::Alias(_) => unreachable!(),
Event::Scalar(v, style, tag) => { let get_type = InvalidType { exp }; match visit_scalar(v, *style, tag, get_type) {
Ok(void) => match void {},
Err(invalid_type) => invalid_type,
}
}
Event::SequenceStart => de::Error::invalid_type(Unexpected::Seq, exp),
Event::MappingStart => de::Error::invalid_type(Unexpected::Map, exp),
Event::SequenceEnd => panic!("unexpected end of sequence"),
Event::MappingEnd => panic!("unexpected end of mapping"),
}
}
impl<'a> DeserializerFromEvents<'a> { fn deserialize_scalar<'de, V>(&mut self, visitor: V) -> Result<V::Value> where
V: Visitor<'de>,
{ let (next, marker) = self.next()?; match next {
Event::Alias(mut pos) => self.jump(&mut pos)?.deserialize_scalar(visitor),
Event::Scalar(v, style, tag) => visit_scalar(v, *style, tag, visitor),
other => Err(invalid_type(other, &visitor)),
}
.map_err(|err| error::fix_marker(err, marker, self.path))
}
}
impl<'de, 'a, 'r> de::Deserializer<'de> for &'r mut DeserializerFromEvents<'a> { type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> where
V: Visitor<'de>,
{ let (next, marker) = self.next()?; match next {
Event::Alias(mut pos) => self.jump(&mut pos)?.deserialize_any(visitor),
Event::Scalar(v, style, tag) => visit_scalar(v, *style, tag, visitor),
Event::SequenceStart => self.visit_sequence(visitor),
Event::MappingStart => self.visit_mapping(visitor),
Event::SequenceEnd => panic!("unexpected end of sequence"),
Event::MappingEnd => panic!("unexpected end of mapping"),
} // The de::Error impl creates errors with unknown line and column. Fill // in the position here by looking at the current index in the input.
.map_err(|err| error::fix_marker(err, marker, self.path))
}
/// Parses an enum as a single key:value pair where the key identifies the /// variant and the value gives the content. A String will also parse correctly /// to a unit enum value. fn deserialize_enum<V>( self,
name: &'static str,
variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value> where
V: Visitor<'de>,
{ let (next, marker) = self.peek()?; match next {
Event::Alias(mut pos) => {
*self.pos += 1; self.jump(&mut pos)?
.deserialize_enum(name, variants, visitor)
}
Event::Scalar(_, _, t) => { iflet Some(TokenType::Tag(handle, suffix)) = t { if handle == "!" { iflet Some(tag) = variants.iter().find(|v| *v == suffix) { return visitor.visit_enum(EnumAccess {
de: self,
name,
tag: Some(tag),
});
}
}
}
visitor.visit_enum(UnitVariantAccess { de: self })
}
Event::MappingStart => {
*self.pos += 1; let value = visitor.visit_enum(EnumAccess {
de: self,
name,
tag: None,
})?; self.end_mapping(1)?;
Ok(value)
}
Event::SequenceStart => { let err = de::Error::invalid_type(Unexpected::Seq, &"string or singleton map");
Err(error::fix_marker(err, marker, self.path))
}
Event::SequenceEnd => panic!("unexpected end of sequence"),
Event::MappingEnd => panic!("unexpected end of mapping"),
}
}
/// Deserialize an instance of type `T` from a string of YAML text. /// /// This conversion can fail if the structure of the Value does not match the /// structure expected by `T`, for example if `T` is a struct type but the Value /// contains something other than a YAML map. It can also fail if the structure /// is correct but `T`'s implementation of `Deserialize` decides that something /// is wrong with the data, for example required struct fields are missing from /// the YAML map or some number is too big to fit in the expected primitive /// type. /// /// YAML currently does not support zero-copy deserialization. pubfn from_str<T>(s: &str) -> Result<T> where
T: DeserializeOwned,
{
from_str_seed(s, PhantomData)
}
/// Deserialize an instance of type `T` from a string of YAML text with a seed. /// /// This conversion can fail if the structure of the Value does not match the /// structure expected by `T`, for example if `T` is a struct type but the Value /// contains something other than a YAML map. It can also fail if the structure /// is correct but `T`'s implementation of `Deserialize` decides that something /// is wrong with the data, for example required struct fields are missing from /// the YAML map or some number is too big to fit in the expected primitive /// type. /// /// YAML currently does not support zero-copy deserialization. pubfn from_str_seed<T, S>(s: &str, seed: S) -> Result<T> where
S: for<'de> DeserializeSeed<'de, Value = T>,
{
seed.deserialize(Deserializer::from_str(s))
}
/// Deserialize an instance of type `T` from an IO stream of YAML. /// /// This conversion can fail if the structure of the Value does not match the /// structure expected by `T`, for example if `T` is a struct type but the Value /// contains something other than a YAML map. It can also fail if the structure /// is correct but `T`'s implementation of `Deserialize` decides that something /// is wrong with the data, for example required struct fields are missing from /// the YAML map or some number is too big to fit in the expected primitive /// type. pubfn from_reader<R, T>(rdr: R) -> Result<T> where
R: io::Read,
T: DeserializeOwned,
{
from_reader_seed(rdr, PhantomData)
}
/// Deserialize an instance of type `T` from an IO stream of YAML with a seed. /// /// This conversion can fail if the structure of the Value does not match the /// structure expected by `T`, for example if `T` is a struct type but the Value /// contains something other than a YAML map. It can also fail if the structure /// is correct but `T`'s implementation of `Deserialize` decides that something /// is wrong with the data, for example required struct fields are missing from /// the YAML map or some number is too big to fit in the expected primitive /// type. pubfn from_reader_seed<R, T, S>(rdr: R, seed: S) -> Result<T> where
R: io::Read,
S: for<'de> DeserializeSeed<'de, Value = T>,
{
seed.deserialize(Deserializer::from_reader(rdr))
}
/// Deserialize an instance of type `T` from bytes of YAML text. /// /// This conversion can fail if the structure of the Value does not match the /// structure expected by `T`, for example if `T` is a struct type but the Value /// contains something other than a YAML map. It can also fail if the structure /// is correct but `T`'s implementation of `Deserialize` decides that something /// is wrong with the data, for example required struct fields are missing from /// the YAML map or some number is too big to fit in the expected primitive /// type. /// /// YAML currently does not support zero-copy deserialization. pubfn from_slice<T>(v: &[u8]) -> Result<T> where
T: DeserializeOwned,
{
from_slice_seed(v, PhantomData)
}
/// Deserialize an instance of type `T` from bytes of YAML text with a seed. /// /// This conversion can fail if the structure of the Value does not match the /// structure expected by `T`, for example if `T` is a struct type but the Value /// contains something other than a YAML map. It can also fail if the structure /// is correct but `T`'s implementation of `Deserialize` decides that something /// is wrong with the data, for example required struct fields are missing from /// the YAML map or some number is too big to fit in the expected primitive /// type. /// /// YAML currently does not support zero-copy deserialization. pubfn from_slice_seed<T, S>(v: &[u8], seed: S) -> Result<T> where
S: for<'de> DeserializeSeed<'de, Value = T>,
{
seed.deserialize(Deserializer::from_slice(v))
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.20 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.