From ad142e9e749adf64642168c0d221b0cf47f149c7 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 14 May 2023 19:51:17 +0200 Subject: refactor: use xml-stinks --- src/lib.rs | 62 ++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 20 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 4e33f8c..8a992b8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,20 +21,33 @@ #![cfg_attr(doc_cfg, feature(doc_cfg))] #![deny(clippy::all, clippy::pedantic, missing_docs)] +use std::convert::Infallible; use std::fs::File; use std::io::Read; -use quick_xml::events::BytesStart; - -use crate::api_interface_definition::APIInterfaceDefinition; +use xml_stinks::deserializer::buffered::Buffered as BufferedDeserializer; +use xml_stinks::deserializer::{Deserializer, Error as DeserializerError, IgnoreEnd}; +use xml_stinks::tagged::TagStart; +use xml_stinks::{ + impl_from_deserializer_error, + xml_stinks_if_deserializer_static_generics, + DeserializeTagged, +}; + +use crate::api_interface_definition::{ + APIInterfaceDefinition, + Error as APIInterfaceDefinitionError, +}; use crate::command::{Command, Error as CommandError}; -use crate::deserialization::buffer_deserializer::BufferDeserializer; -use crate::deserialization::{Deserialize, Deserializer, DeserializerError, IgnoreEnd}; +use crate::util::impl_from_deserializer_err_wrapped; pub mod api_interface_definition; pub mod command; -mod deserialization; +mod util; + +#[cfg(test)] +mod test_utils; /// XML. #[cfg(feature = "include-xml")] @@ -69,7 +82,17 @@ impl Registry /// Returns `Err` if parsing fails in any way. pub fn retrieve_from_bytes(xml_bytes: &[u8]) -> Result { - let mut deserializer = BufferDeserializer::new(xml_bytes); + let mut deserializer = xml_stinks_if_deserializer_static_generics!(then { + // The deserializer-static-generics feature of the dependency xml-stinks is + // enabled. + // + // The feature is enabled when building tests and is required for mocking to + // work. However, it will reject any non-owned source, so the bytes has to be + // made into a Cursor> + BufferedDeserializer::new(std::io::Cursor::new(xml_bytes.to_vec())); + } else { + BufferedDeserializer::new(xml_bytes); + }); deserializer.skip_to_tag_start(REGISTRY_TAG_NAME)?; @@ -125,12 +148,12 @@ impl Registry } } -impl Deserialize for Registry +impl DeserializeTagged for Registry { type Error = RegistryError; fn deserialize( - _start: &BytesStart, + _start: &TagStart, deserializer: &mut TDeserializer, ) -> Result { @@ -138,13 +161,13 @@ impl Deserialize for Registry let commands = deserializer.de_tag_with("commands", IgnoreEnd::No, |_, deserializer| { - deserializer.de_tag_list::("command") + deserializer.de_tag_list::(Some("command")) })?; deserializer.skip_to_tag_start("feature")?; let api_interface_definitions = - deserializer.de_tag_list::("feature")?; + deserializer.de_tag_list::(Some("feature"))?; Ok(Self { commands, @@ -165,10 +188,14 @@ pub enum RegistryError #[error("No 'commands' element was found")] MissingCommandsElement, - /// A command is invalid. + /// Invalid command. #[error("Invalid command")] InvalidCommand(#[from] CommandError), + /// Invalid API interface definition. + #[error("Invalid API interface definition")] + InvalidAPIInterfaceDefinition(#[from] APIInterfaceDefinitionError), + /// I/O failed. #[error("I/O failed")] IOFailed(#[from] std::io::Error), @@ -178,18 +205,13 @@ pub enum RegistryError DeserializationFailed(#[from] DeserializationError), } -impl From for RegistryError -{ - fn from(err: DeserializerError) -> Self - { - DeserializationError(err).into() - } -} +impl_from_deserializer_err_wrapped!(RegistryError); +impl_from_deserializer_error!(RegistryError); /// Deserialization error. #[derive(Debug, thiserror::Error)] #[error(transparent)] -pub struct DeserializationError(#[from] DeserializerError); +pub struct DeserializationError(#[from] DeserializerError); #[cfg(test)] mod tests -- cgit v1.2.3-18-g5258