diff options
author | HampusM <hampus@hampusmat.com> | 2023-05-14 19:51:17 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-05-14 19:51:17 +0200 |
commit | ad142e9e749adf64642168c0d221b0cf47f149c7 (patch) | |
tree | 642308f9aec6032115c62bb6ae269fa37c3fc453 /src/lib.rs | |
parent | 4d46e44801d8ef40c8a2f4dea1d2c9088f2557a1 (diff) |
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 62 |
1 files changed, 42 insertions, 20 deletions
@@ -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<Registry, RegistryError> { - 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<Vec<u8>> + 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<TDeserializer: Deserializer>( - _start: &BytesStart, + _start: &TagStart, deserializer: &mut TDeserializer, ) -> Result<Self, Self::Error> { @@ -138,13 +161,13 @@ impl Deserialize for Registry let commands = deserializer.de_tag_with("commands", IgnoreEnd::No, |_, deserializer| { - deserializer.de_tag_list::<Command>("command") + deserializer.de_tag_list::<Command, _>(Some("command")) })?; deserializer.skip_to_tag_start("feature")?; let api_interface_definitions = - deserializer.de_tag_list::<APIInterfaceDefinition>("feature")?; + deserializer.de_tag_list::<APIInterfaceDefinition, _>(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<DeserializerError> 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<Infallible>); #[cfg(test)] mod tests |