tamer: Introduce tpwrap module to contain quick_xml::Error adapter

This adapter exists to implement PartialEq so that it can be derived on
Error objects.  This is used primarily (well, exclusively atm) for tests.
main
Mike Gerwitz 2021-07-23 23:21:19 -04:00
parent fb8422d670
commit d9dcfe8777
5 changed files with 110 additions and 50 deletions

View File

@ -37,6 +37,7 @@ pub mod fs;
pub mod ir;
pub mod ld;
pub mod obj;
pub mod tpwrap;
#[cfg(test)]
pub mod test;

View File

@ -139,11 +139,11 @@ use crate::test::quick_xml::MockBytesStart as BytesStart;
use crate::test::quick_xml::MockXmlEvent as XmlEvent;
#[cfg(test)]
use crate::test::quick_xml::MockXmlReader as XmlReader;
use crate::tpwrap::quick_xml::{Error as XmlError, InnerXmlError};
#[cfg(not(test))]
use quick_xml::events::BytesStart;
#[cfg(not(test))]
use quick_xml::events::Event as XmlEvent;
use quick_xml::Error as XmlError;
#[cfg(not(test))]
use quick_xml::Reader as XmlReader;
use std::convert::TryInto;
@ -656,7 +656,7 @@ impl<'i, B: BufRead, I: Interner<'i>> XmloReader<'i, B, I> {
reader
.read_text(ele.name(), buffer)
.map_err(|err| match err {
XmlError::TextNotFound => {
InnerXmlError::TextNotFound => {
XmloError::MissingFragmentText(id.to_string())
}
_ => err.into(),
@ -774,7 +774,7 @@ pub enum XmloEvent<'i> {
#[derive(Debug, PartialEq)]
pub enum XmloError {
/// XML parsing error.
XmlError(XmlParseError),
XmlError(XmlError),
/// The root node was not an `lv:package`.
UnexpectedRoot,
/// A `preproc:sym` node was found, but is missing `@name`.
@ -798,8 +798,8 @@ pub enum XmloError {
MissingFragmentText(String),
}
impl From<XmlError> for XmloError {
fn from(e: XmlError) -> Self {
impl From<InnerXmlError> for XmloError {
fn from(e: InnerXmlError) -> Self {
XmloError::XmlError(e.into())
}
}
@ -856,39 +856,5 @@ impl std::error::Error for XmloError {
}
}
/// Thin wrapper around [`XmlError`] to implement [`PartialEq`].
///
/// This will always yield `false`,
/// but allows us to derive the trait on types using [`XmloError`];
/// otherwise, this madness propagates indefinitely.
#[derive(Debug)]
pub struct XmlParseError(XmlError);
impl PartialEq for XmlParseError {
/// [`XmlError`] does not implement [`PartialEq`] and so this will
/// always yield `false`.
fn eq(&self, _other: &Self) -> bool {
false
}
}
impl From<XmlError> for XmlParseError {
fn from(e: XmlError) -> Self {
Self(e)
}
}
impl Display for XmlParseError {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
self.0.fmt(fmt)
}
}
impl std::error::Error for XmlParseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.0)
}
}
#[cfg(test)]
mod test;

View File

@ -55,10 +55,10 @@ xmlo_tests! {
fn proxies_xml_failures(sut, interner) {
sut.reader.next_event =
Some(Box::new(|_, _| Err(XmlError::UnexpectedEof("test".into()))));
Some(Box::new(|_, _| Err(InnerXmlError::UnexpectedEof("test".into()))));
match sut.read_event() {
Err(XmloError::XmlError(XmlParseError(XmlError::UnexpectedEof(_)))) => (),
Err(XmloError::XmlError(XmlError(InnerXmlError::UnexpectedEof(_)))) => (),
bad => panic!("expected XmlError: {:?}", bad),
}
}
@ -214,7 +214,7 @@ xmlo_tests! {
)])),
))),
3 => Ok(XmlEvent::End(MockBytesEnd::new(b"preproc:sym-dep"))),
_ => Err(XmlError::UnexpectedEof(
_ => Err(InnerXmlError::UnexpectedEof(
format!("MockXmlReader out of events: {}", event_i).into(),
)),
}));
@ -257,7 +257,7 @@ xmlo_tests! {
b"preproc:sym-ref",
Some(MockAttributes::new(vec![])),
))),
_ => Err(XmlError::UnexpectedEof(
_ => Err(InnerXmlError::UnexpectedEof(
format!("MockXmlReader out of events: {}", event_i).into(),
)),
}));
@ -287,7 +287,7 @@ xmlo_tests! {
b"preproc:unexpected",
Some(MockAttributes::new(vec![])),
))),
_ => Err(XmlError::UnexpectedEof(
_ => Err(InnerXmlError::UnexpectedEof(
format!("MockXmlReader out of events: {}", event_i).into(),
)),
}));
@ -372,7 +372,7 @@ xmlo_tests! {
}
fn fragment_fails_with_missing_text(sut, interner) {
sut.reader.next_text = Some(Err(XmlError::TextNotFound));
sut.reader.next_text = Some(Err(InnerXmlError::TextNotFound));
sut.reader.next_event = Some(Box::new(|_, _| {
Ok(XmlEvent::Start(MockBytesStart::new(
@ -412,7 +412,7 @@ xmlo_tests! {
)])),
))),
_ => Err(XmlError::UnexpectedEof(
_ => Err(InnerXmlError::UnexpectedEof(
format!("MockXmlReader out of events: {}", event_i).into(),
)),
}));
@ -515,7 +515,7 @@ xmlo_tests! {
b"preproc:sym",
))),
_ => Err(XmlError::UnexpectedEof(
_ => Err(InnerXmlError::UnexpectedEof(
format!("MockXmlReader out of events: {}", event_i).into(),
)),
}));
@ -567,7 +567,7 @@ xmlo_tests! {
b"preproc:sym",
))),
_ => Err(XmlError::UnexpectedEof(
_ => Err(InnerXmlError::UnexpectedEof(
format!("MockXmlReader out of events: {}", event_i).into(),
)),
}));
@ -601,7 +601,7 @@ xmlo_tests! {
Some(MockAttributes::new(vec![])),
))),
_ => Err(XmlError::UnexpectedEof(
_ => Err(InnerXmlError::UnexpectedEof(
format!("MockXmlReader out of events: {}", event_i).into(),
)),
}));
@ -664,7 +664,7 @@ macro_rules! sym_test_reader_event {
)),
))),
_ => Err(XmlError::UnexpectedEof(
_ => Err(InnerXmlError::UnexpectedEof(
format!("MockXmlReader out of events: {}", event_i).into(),
)),
}

View File

@ -0,0 +1,28 @@
// Wrappers around third-party modules
//
// Copyright (C) 2014-2021 Ryan Specialty Group, LLC.
//
// This file is part of TAME.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//! Wrappers around third-party modules to make them play nicely with TAMER.
//!
//! Some third-party libraries provide interfaces that present integration issues.
//! Those are often addressed in the context that they are used,
//! but sometimes those adapters need to be shared across different parts
//! of the system.
//! They live here.
pub mod quick_xml;

View File

@ -0,0 +1,65 @@
// `quick-xml` wrappers for TAME.
//
// Copyright (C) 2014-2021 Ryan Specialty Group, LLC.
//
// This file is part of TAME.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use std::fmt::Display;
/// Wrapped error type.
pub type InnerXmlError = quick_xml::Error;
/// Thin wrapper around [`quick_xml::Error`] to implement [`PartialEq`].
///
/// This will always yield `false`,
/// but allows us to derive the trait on types using [`Error`];
/// otherwise, this madness propagates indefinitely.
#[derive(Debug)]
pub struct Error(pub InnerXmlError);
impl PartialEq for Error {
/// [`quick_xml::Error`] does not implement [`PartialEq`] and so this
/// will always yield `false`.
fn eq(&self, _other: &Self) -> bool {
false
}
}
impl From<InnerXmlError> for Error {
fn from(e: InnerXmlError) -> Self {
Self(e)
}
}
impl Into<InnerXmlError> for Error {
fn into(self) -> InnerXmlError {
self.0
}
}
impl Display for Error {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
// NB: If we eventually use `source` to display a hierarchy of
// errors, then we likely do not want the duplication here.
self.0.fmt(fmt)
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.0)
}
}