[DEV-7087] TAMER: AsgError: Wrap TransitionError

See next commit.
master
Mike Gerwitz 2020-03-24 15:54:26 -04:00
parent 03fa2ffc0b
commit b35dd4f4dd
3 changed files with 22 additions and 28 deletions

View File

@ -648,9 +648,8 @@ mod test {
// Set up an object to fail redeclaration.
let node = sut.declare(&sym, IdentKind::Meta, src.clone())?;
let obj = sut.get(node).unwrap();
let msg = String::from("test fail");
obj.fail_redeclare
.replace(Some(TransitionError::Incompatible(msg.clone())));
let terr = TransitionError::Incompatible(String::from("test fail"));
obj.fail_redeclare.replace(Some(terr.clone()));
// Should invoke StubIdentObject::redeclare on the above `obj`.
let result = sut.declare(&sym, IdentKind::Meta, Source::default());
@ -660,7 +659,7 @@ mod test {
let obj = sut.get(node).unwrap();
assert_eq!(src, obj.given_ident.as_ref().unwrap().2);
assert_eq!(AsgError::IncompatibleIdent(msg), err);
assert_eq!(AsgError::ObjectTransition(terr), err);
Ok(())
} else {

View File

@ -227,18 +227,12 @@ pub type Node<O> = Option<O>;
/// The caller will know the problem values.
#[derive(Debug, PartialEq)]
pub enum AsgError<Ix: Debug> {
/// The provided identifier is not in a state that is permitted to
/// receive a fragment.
/// An object could not change state in the manner requested.
///
/// See [`Asg::set_fragment`] for more information.
BadFragmentDest(String),
/// An attempt to redeclare an identifier with additional information
/// has failed because the provided information was not compatible
/// with the original declaration.
///
/// See [`Asg::declare`] for more information.
IncompatibleIdent(String),
/// See [`Asg::declare`] and [`Asg::set_fragment`] for more
/// information.
/// See also [`TransitionError`].
ObjectTransition(TransitionError),
/// The node was not expected in the current context
UnexpectedNode(String),
@ -250,12 +244,7 @@ pub enum AsgError<Ix: Debug> {
impl<Ix: Debug> std::fmt::Display for AsgError<Ix> {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::BadFragmentDest(msg) => {
write!(fmt, "bad fragment destination: {}", msg)
}
Self::IncompatibleIdent(msg) => {
write!(fmt, "identifier redeclaration failed: {}", msg)
}
Self::ObjectTransition(err) => std::fmt::Display::fmt(&err, fmt),
Self::UnexpectedNode(msg) => {
write!(fmt, "unexpected node: {}", msg)
}
@ -268,16 +257,16 @@ impl<Ix: Debug> std::fmt::Display for AsgError<Ix> {
impl<Ix: Debug> std::error::Error for AsgError<Ix> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
match self {
Self::ObjectTransition(err) => err.source(),
_ => None,
}
}
}
impl<Ix: Debug> From<TransitionError> for AsgError<Ix> {
fn from(e: TransitionError) -> Self {
match e {
TransitionError::Incompatible(msg) => Self::IncompatibleIdent(msg),
TransitionError::BadFragmentDest(msg) => Self::BadFragmentDest(msg),
}
fn from(err: TransitionError) -> Self {
Self::ObjectTransition(err)
}
}

View File

@ -340,7 +340,7 @@ impl<'i> IdentObjectState<'i, IdentObject<'i>> for IdentObject<'i> {
/// another.
///
/// TODO: Provide enough information to construct a useful message.
#[derive(Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
pub enum TransitionError {
/// An attempt to redeclare an identifier with additional information
/// has failed because the provided information was not compatible
@ -370,6 +370,12 @@ impl std::fmt::Display for TransitionError {
}
}
impl std::error::Error for TransitionError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
/// Compiled fragment for identifier.
///
/// This represents the text associated with an identifier.