Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Gerwitz <mike.gerwitz@ryansg.com>2020-04-20 10:52:55 -0400
committerMike Gerwitz <mike.gerwitz@ryansg.com>2020-04-28 09:06:25 -0400
commitcfc13f901668b25d6e471f8b842c18942dd93ad1 (patch)
tree676804dc40b674e7dabcf2e333f587aad0cf5f8c
parent0a9a3214b797880c0e33d1e13fd5f3c77b8bbf9c (diff)
downloadtame-cfc13f901668b25d6e471f8b842c18942dd93ad1.tar.gz
tame-cfc13f901668b25d6e471f8b842c18942dd93ad1.tar.bz2
tame-cfc13f901668b25d6e471f8b842c18942dd93ad1.zip
[DEV-7084] TAMER: ir::asg::IdentKindError: Replace string with enum
-rw-r--r--tamer/src/ir/asg/ident.rs59
-rw-r--r--tamer/src/ir/asg/mod.rs2
2 files changed, 49 insertions, 12 deletions
diff --git a/tamer/src/ir/asg/ident.rs b/tamer/src/ir/asg/ident.rs
index b5ef44b..d0841d1 100644
--- a/tamer/src/ir/asg/ident.rs
+++ b/tamer/src/ir/asg/ident.rs
@@ -21,6 +21,7 @@
use crate::ir::legacyir::{SymAttrs, SymDtype, SymType};
use std::convert::TryFrom;
+use std::error::Error;
/// Types of identifiers.
///
@@ -181,7 +182,7 @@ impl std::fmt::Display for IdentKind {
}
impl<'i> TryFrom<SymAttrs<'i>> for IdentKind {
- type Error = &'static str;
+ type Error = IdentKindError;
/// Attempt to raise [`SymAttrs`] into an [`IdentKind`].
///
@@ -193,29 +194,29 @@ impl<'i> TryFrom<SymAttrs<'i>> for IdentKind {
}
impl<'i> TryFrom<&SymAttrs<'i>> for IdentKind {
- type Error = &'static str;
+ type Error = IdentKindError;
/// Attempt to raise [`SymAttrs`] into an [`IdentKind`].
///
/// Certain [`IdentKind`] require that certain attributes be present,
/// otherwise the conversion will fail.
fn try_from(attrs: &SymAttrs<'i>) -> Result<Self, Self::Error> {
- let ty = attrs.ty.as_ref().ok_or("missing symbol type")?;
+ let ty = attrs.ty.as_ref().ok_or(Self::Error::MissingType)?;
macro_rules! ident {
($to:expr) => {
Ok($to)
};
($to:expr, dim) => {
- Ok($to(Dim(attrs.dim.ok_or("missing dim")?)))
+ Ok($to(Dim(attrs.dim.ok_or(Self::Error::MissingDim)?)))
};
($to:expr, dtype) => {
- Ok($to(attrs.dtype.ok_or("missing dtype")?))
+ Ok($to(attrs.dtype.ok_or(Self::Error::MissingDtype)?))
};
($to:expr, dim, dtype) => {
Ok($to(
- Dim(attrs.dim.ok_or("missing dim")?),
- attrs.dtype.ok_or("missing dtype")?,
+ Dim(attrs.dim.ok_or(Self::Error::MissingDim)?),
+ attrs.dtype.ok_or(Self::Error::MissingDtype)?,
))
};
}
@@ -243,6 +244,34 @@ impl<'i> TryFrom<&SymAttrs<'i>> for IdentKind {
}
}
+#[derive(Debug, PartialEq, Eq)]
+pub enum IdentKindError {
+ /// Symbol type was not provided.
+ MissingType,
+
+ /// Number of symbol dimensions were not provided.
+ MissingDim,
+
+ /// Symbol dtype was not provided.
+ MissingDtype,
+}
+
+impl std::fmt::Display for IdentKindError {
+ fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
+ match self {
+ Self::MissingType => write!(fmt, "missing symbol type"),
+ Self::MissingDim => write!(fmt, "missing dim"),
+ Self::MissingDtype => write!(fmt, "missing dtype"),
+ }
+ }
+}
+
+impl Error for IdentKindError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ None
+ }
+}
+
/// Identifier dimensions.
///
/// This determines the number of subscripts needed to access a scalar
@@ -345,11 +374,13 @@ mod test {
);
// no dim
- IdentKind::try_from(SymAttrs {
+ let result = IdentKind::try_from(SymAttrs {
ty: Some($src),
..Default::default()
})
.expect_err("must fail when missing dim");
+
+ assert_eq!(IdentKindError::MissingDim, result);
}
};
@@ -369,11 +400,13 @@ mod test {
);
// no dtype
- IdentKind::try_from(SymAttrs {
+ let result = IdentKind::try_from(SymAttrs {
ty: Some($src),
..Default::default()
})
.expect_err("must fail when missing dtype");
+
+ assert_eq!(IdentKindError::MissingDtype, result);
}
};
@@ -395,20 +428,24 @@ mod test {
);
// no dim
- IdentKind::try_from(SymAttrs {
+ let dim_result = IdentKind::try_from(SymAttrs {
ty: Some($src),
dtype: Some(dtype),
..Default::default()
})
.expect_err("must fail when missing dim");
+ assert_eq!(IdentKindError::MissingDim, dim_result);
+
// no dtype
- IdentKind::try_from(SymAttrs {
+ let dtype_result = IdentKind::try_from(SymAttrs {
ty: Some($src),
dim: Some(dim),
..Default::default()
})
.expect_err("must fail when missing dtype");
+
+ assert_eq!(IdentKindError::MissingDtype, dtype_result);
}
};
}
diff --git a/tamer/src/ir/asg/mod.rs b/tamer/src/ir/asg/mod.rs
index d64024d..761716c 100644
--- a/tamer/src/ir/asg/mod.rs
+++ b/tamer/src/ir/asg/mod.rs
@@ -197,7 +197,7 @@ mod object;
mod section;
pub use graph::{Asg, AsgError, AsgResult, IndexType, ObjectRef, SortableAsg};
-pub use ident::{DataType, Dim, IdentKind};
+pub use ident::{DataType, Dim, IdentKind, IdentKindError};
pub use object::{
FragmentText, IdentObject, IdentObjectData, IdentObjectState, Source,
TransitionError, TransitionResult,