From bcfded3bf7c7daab8d7e8617261756cc15ca6227 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 14 May 2023 17:53:46 +0200 Subject: docs: add example to crate root documentation --- src/lib.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 5235e54..fb42cdf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,65 @@ //! XML is awful. +//! +//! # Examples +//! ``` +//! use std::convert::Infallible; +//! +//! use xml_stinks::deserializer::buffered::Buffered as BufferedDeserializer; +//! use xml_stinks::deserializer::{Deserializer, IgnoreEnd}; +//! use xml_stinks::tagged::TagStart; +//! use xml_stinks::DeserializeTagged; +//! +//! let mut deserializer = +//! BufferedDeserializer::new("BazDF".as_bytes()); +//! +//! let foo = deserializer.de_tag::("foo", IgnoreEnd::No).unwrap(); +//! +//! assert_eq!(&foo.name, "Baz"); +//! assert_eq!(foo.bar.num, 223); +//! +//! struct Foo +//! { +//! name: String, +//! bar: Bar, +//! } +//! +//! impl DeserializeTagged for Foo +//! { +//! type Error = Infallible; +//! +//! fn deserialize( +//! start: &TagStart, +//! deserializer: &mut TDeserializer, +//! ) -> Result +//! { +//! let name = deserializer.de_text().unwrap(); +//! +//! let bar = deserializer.de_tag::("bar", IgnoreEnd::No).unwrap(); +//! +//! Ok(Self { name, bar }) +//! } +//! } +//! +//! struct Bar +//! { +//! num: u32, +//! } +//! +//! impl DeserializeTagged for Bar +//! { +//! type Error = Infallible; +//! +//! fn deserialize( +//! start: &TagStart, +//! deserializer: &mut TDeserializer, +//! ) -> Result +//! { +//! let num = u32::from_str_radix(&deserializer.de_text().unwrap(), 16).unwrap(); +//! +//! Ok(Self { num }) +//! } +//! } +//! ``` #![deny(clippy::all, clippy::pedantic, unsafe_code, missing_docs)] use crate::deserializer::{Deserializer, Error as DeserializerError, MaybeStatic}; use crate::tagged::TagStart; -- cgit v1.2.3-18-g5258