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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use bitwidth::BitWidth;
use bitpos::BitPos;
use radix::Radix;
use apint::{ApInt, ShiftAmount};
use apint::{PrimitiveTy};

use std::result;
use std::error;
use std::fmt;

/// Represents the kind of an `Error`.
/// 
/// This also stores the unique information tied to the error report.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ErrorKind {
	/// Returned on trying to create a `Radix` from an invalid `u8` representation.
	InvalidRadix(u8),

	/// Returned whenever trying to parse an invalid string representation for an `ApInt`.
	InvalidStringRepr{
		/// The string storing the invalid representation of the int for the given radix.
		input: String,
		/// The radix that was used.
		radix: Radix,
		/// An optional index and character when encountering an invalid character.
		pos_char: Option<(usize, char)>
	},

	/// Returned on trying to access an invalid bit position.
	InvalidBitAccess{
		/// The invalid bit position that was tried to access.
		pos: BitPos,
		/// The upper bound for valid bit positions in this context.
		width: BitWidth
	},

	/// Returned on trying to shift with an invalid shift amount.
	InvalidShiftAmount{
		/// The invalid shift amount.
		shift_amount: ShiftAmount,
		/// The bit width for which this shift amount of invalid.
		width: BitWidth
	},

	/// Returns on trying to cast an `ApInt` to a primitive type
	/// that can not represent the value represented by the `ApInt`.
	ValueUnrepresentable{
		/// The `ApInt` that the user wanted to represent as the given `PrimitiveTy`.
		value: ApInt,
		/// The `PrimitiveTy` that the user wanted for representing the given `ApInt`.
		destination_ty: PrimitiveTy
	},

	/// Returned on violation of matching bitwidth constraints of operations.
	UnmatchingBitwidth(BitWidth, BitWidth),

	/// Returned on trying to create a `BitWidth` from an invalid `usize` representation.
	InvalidBitWidth(usize),

	/// Returned on truncating an `ApInt` with a bitwidth greater than the current one.
	TruncationBitWidthTooLarge{
		/// The target bit width.
		target: BitWidth,
		/// The current actual bit width.
		current: BitWidth
	},

	/// Returned on extending an `ApInt` with a bitwidth less than the current one.
	ExtensionBitWidthTooSmall{
		/// The target bit width.
		target: BitWidth,
		/// The current actual bit width.
		current: BitWidth
	},

	/// Returned on constructing an `ApInt` from an empty iterator of `Digit`s.
	ExpectedNonEmptyDigits,
}

/// Represents an error that may occure upon using the `ApInt` library.
/// 
/// All errors have a unique kind which also stores extra information for error reporting.
/// Besides that an `Error` also stores a message and an optional additional annotation.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Error{
	kind      : ErrorKind,
	message   : String,
	annotation: Option<String>
}

//  ===========================================================================
///  Public getters for `Error`.
/// ===========================================================================
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
		}
	}
}

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

//  ===========================================================================
///  Default constructors for `Error`.
/// ===========================================================================
impl Error {
	pub(crate) fn invalid_radix(val: u8) -> Error {
		Error{
			kind: ErrorKind::InvalidRadix(val),
			message: format!("Encountered an invalid parsing radix of {:?}.", val),
			annotation: None
		}
	}

	pub(crate) fn invalid_string_repr<S>(input: S, radix: Radix) -> Error
		where S: Into<String>
	{
		let input = input.into();
		Error{
			kind: ErrorKind::InvalidStringRepr{input, radix, pos_char: None},
			message: format!("Encountered an invalid string representation for the given radix (= {:?}).", radix),
			annotation: None
		}
	}

	pub(crate) fn invalid_char_in_string_repr<S>(input: S, radix: Radix, pos: usize, ch: char) -> Error
		where S: Into<String>
	{
		let input = input.into();
		Error{
			kind: ErrorKind::InvalidStringRepr{input, radix, pos_char: None},
			message: format!("Encountered an invalid character (= '{:?}') at position {:?} within the given string \
				              representation for the given radix (= {:?}).", ch, pos, radix),
			annotation: None
		}
	}

	pub(crate) fn invalid_bitwidth(val: usize) -> Error {
		Error{
			kind: ErrorKind::InvalidBitWidth(val),
			message: format!("Encountered invalid bitwidth of {:?}.", val),
			annotation: None
		}
	}

	pub(crate) fn invalid_zero_bitwidth() -> Error {
		Error::invalid_bitwidth(0)
	}

	pub(crate) fn extension_bitwidth_too_small<W1, W2>(target: W1, current: W2) -> Error
		where W1: Into<BitWidth>,
		      W2: Into<BitWidth>
	{
		let target = target.into();
		let current = current.into();
		Error{
			kind: ErrorKind::ExtensionBitWidthTooSmall{target, current},
			message: format!("Tried to extend an `ApInt` with a width of {:?} to a smaller target width of {:?}", current, target),
			annotation: None
		}
	}

	pub(crate) fn truncation_bitwidth_too_large<W1, W2>(target: W1, current: W2) -> Error
		where W1: Into<BitWidth>,
		      W2: Into<BitWidth>
	{
		let target = target.into();
		let current = current.into();
		Error{
			kind: ErrorKind::TruncationBitWidthTooLarge{target, current},
			message: format!("Tried to truncate an `ApInt` with a width of {:?} to a larger target width of {:?}", current, target),
			annotation: None
		}
	}

	pub(crate) fn unmatching_bitwidths<W1, W2>(lhs: W1, rhs: W2) -> Error
		where W1: Into<BitWidth>,
		      W2: Into<BitWidth>
	{
		let lhs = lhs.into();
		let rhs = rhs.into();
		Error{
			kind: ErrorKind::UnmatchingBitwidth(lhs, rhs),
			message: format!("Encountered invalid operation on entities with non-matching bit-widths of {:?} and {:?}.", lhs, rhs),
			annotation: None
		}
	}

	pub(crate) fn invalid_shift_amount<S, W>(shift_amount: S, width: W) -> Error
		where S: Into<ShiftAmount>,
		      W: Into<BitWidth>
	{
		let shift_amount = shift_amount.into();
		let width = width.into();
		Error{
			kind: ErrorKind::InvalidShiftAmount{shift_amount, width},
			message: format!("Encountered invalid shift amount of {:?} on bit-width with {:?} bits.", shift_amount, width),
			annotation: None
		}
	}

	pub(crate) fn invalid_bit_access<P, W>(pos: P, width: W) -> Error
		where P: Into<BitPos>,
		      W: Into<BitWidth>
	{
		let pos = pos.into();
		let width = width.into();
		Error{
			kind: ErrorKind::InvalidBitAccess{pos, width},
			message: format!("Encountered invalid bit access at position {:?} with a total bit-width of {:?}.", pos, width),
			annotation: None
		}
	}

	pub(crate) fn expected_non_empty_digits() -> Error {
		Error{
			kind: ErrorKind::ExpectedNonEmptyDigits,
			message: "Encountered an empty iterator upon construction of an `ApInt` from a digit iterator.".to_owned(),
			annotation: None
		}
	}

	pub(crate) fn encountered_unrepresentable_value(
		value: ApInt,
		destination_ty: PrimitiveTy
	)
		-> Error
	{
		let message = format!(
			"Encountered a value ({:?}) that is unrepresentable \
			 by the destination type {:?}.", value, destination_ty);
		Error{
			kind: ErrorKind::ValueUnrepresentable{value, destination_ty},
			message,
			annotation: None
		}
	}
}

impl<T> Into<Result<T>> for Error {
	/// Converts an `Error` into a `Result<T, Error>`.
	/// 
	/// This might be useful to prevent some parentheses spams
	/// because it replaces `Err(my_error)` with `my_error.into()`.
	/// 
	/// On the other hand it might be an abuse of the trait ...
	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()
	}
}

/// The `Result` type used in `ApInt`.
pub type Result<T> = result::Result<T, Error>;