1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use std::result;
use std::error;
use std::fmt;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ErrorKind {
	InvalidBinaryStr(String),
	InvalidDecimalStr(String),
	InvalidHexStr(String),

	BitAccessOutOfBounds{bit_idx: usize, upper_bound: usize},

	UnmatchingBitwidth(usize, usize),
	InvalidZeroBitWidth,
	BitWidthOutOfBounds{bitwidth: usize, lo: usize, hi: usize},
	BitWidthTooLarge{bitwidth: usize, upper_bound: usize},
	BitWidthTooSmall{bitwidth: usize, lower_bound: usize}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Error{
	kind      : ErrorKind,
	message   : String,
	annotation: Option<String>
}

impl Error {
	/// Returns a reference to the kind of this `Error`.
	#[inline]
	pub fn kind(&self) -> &ErrorKind {
		&self.kind
	}

	/// Returns the error message description of this `Error`.
	#[inline]
	pub fn message(&self) -> &str {
		self.message.as_str()
	}

	/// Returns the optional annotation of this `Error`.
	#[inline]
	pub fn annotation(&self) -> Option<&str> {
		match self.annotation {
			Some(ref ann) => Some(ann.as_str()),
			None          => None
		}
	}

	#[inline]
	pub(crate) fn new(kind: ErrorKind, message: String) -> Error {
		Error{kind, message, annotation: None}
	}

	#[inline]
	pub(crate) fn invalid_zero_bitwidth() -> Error {
		Error{
			kind: ErrorKind::InvalidZeroBitWidth,
			message: "Encountered invalid bitwidth of zero (0).".to_owned(),
			annotation: None
		}
	}

	#[inline]
	pub(crate) fn bitwidth_too_small(bitwidth: usize, lower_bound: usize) -> Error {
		Error{
			kind: ErrorKind::BitWidthTooSmall{bitwidth, lower_bound},
			message: format!("Encountered bitwidth of {:?} smaller than the lower bound of {:?}.", bitwidth, lower_bound),
			annotation: None
		}
	}

	#[inline]
	pub(crate) fn bitwidth_too_large(bitwidth: usize, upper_bound: usize) -> Error {
		Error{
			kind: ErrorKind::BitWidthTooLarge{bitwidth, upper_bound},
			message: format!("Encountered bitwidth of {:?} larger than the upper bound of {:?}.", bitwidth, upper_bound),
			annotation: None
		}
	}

	#[inline]
	pub(crate) fn bitwidth_out_of_bounds(bitwidth: usize, lo: usize, hi: usize) -> Error {
		Error{
			kind: ErrorKind::BitWidthOutOfBounds{bitwidth, lo, hi},
			message: format!("Encountered bitwidth of {:?} out of valid bounds of {:?} to {:?}.", bitwidth, lo, hi),
			annotation: None
		}
	}

	#[inline]
	pub(crate) fn unmatching_bitwidths(left: usize, right: usize) -> Error {
		Error{
			kind: ErrorKind::UnmatchingBitwidth(left, right),
			message: format!("Encountered invalid operation on entities with non-matching bit-widths of {:?} and {:?}.", left, right),
			annotation: None
		}
	}

	#[inline]
	pub(crate) fn with_annotation<A>(mut self, annotation: A) -> Error
		where A: Into<String>
	{
		self.annotation = Some(annotation.into());
		self
	}

	pub(crate) fn bit_access_out_of_bounds(bit_idx: usize, upper_bound: usize) -> Error {
		Error{
			kind: ErrorKind::BitAccessOutOfBounds{bit_idx, upper_bound},
			message: format!("Encountered invalid bit access at index {:?} with an upper bound of {:?}.", bit_idx, upper_bound),
			annotation: None
		}
	}
}

impl<T> Into<Result<T>> for Error {
	fn into(self) -> Result<T> {
		Err(self)
	}
}

impl fmt::Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		<Self as fmt::Debug>::fmt(self, f)
	}
}

impl error::Error for Error {
	fn description(&self) -> &str {
		self.message.as_str()
	}
}

pub type Result<T> = result::Result<T, Error>;