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
//! The goof library is a collection of re-usable error handling
//! structs and patterns that are meant to make error handling
//! lightweight, portable and inter-convertible.
use core::fmt::{Debug, Display};
/// Assert that the object is exactly equal to the provided test value.
///
/// # Motivation
///
/// Oftentimes one really only needs an assertion to be propagated
/// upwards. Given that try blocks are not stable, this syntax has
/// some merit. This assert can be used inside function arguments, at
/// the tops of functions to get rid of an ugly `if` and makes it
/// explicit that what you want is to do what the standard library's
/// `assert_eq!` does, but to create an error rather than panic.
///
/// # Examples
/// ```rust
/// use goof::{Mismatch, assert_eq};
///
/// fn fallible_func(thing: &[u8]) -> Result<(), Mismatch<usize>> {
/// assert_eq(&32, &thing.len())?;
///
/// Ok(())
/// }
///
/// assert_eq!(fallible_func(&[]).unwrap_err(), assert_eq(&32, &0).unwrap_err())
/// ```
pub fn assert_eq<T: Copy + Eq>(actual: &T, expected: &T) -> Result<T, Mismatch<T>> {
if expected.eq(&actual) {
Ok(*expected)
} else {
Err(Mismatch {
expected: *expected,
actual: *actual,
})
}
}
/// Assert that the object is exactly within the boundaries given by
/// the `range` operand.
///
/// # Motivation
///
/// Oftentimes one really only needs an assertion to be propagated
/// upwards. Given that try blocks are not stable, this syntax has
/// some merit. This assert can be used inside function arguments, at
/// the tops of functions to get rid of an ugly `if` and makes it
/// explicit that what you want is to do what the standard library's
/// `assert_eq!` does, but to create an error rather than panic.
///
/// # Examples
/// ```rust
/// use goof::{Outside, assert_in};
///
/// fn fallible_func(thing: &[u8]) -> Result<(), Outside<usize>> {
/// assert_in(&thing.len(), &(32..64))?;
///
/// Ok(())
/// }
///
/// assert_eq!(fallible_func(&vec![0; 32]).unwrap_err(), assert_in(&32, &0).unwrap_err())
/// ```
pub fn assert_in<T: Ord + Copy>(value: &T, range: &core::ops::Range<T>) -> Result<T, Outside<T>> {
if value > &range.start && value <= &range.end {
Ok(*value)
} else {
// TODO: isn't Range<T> supposed to be Copy?
Err(Outside {
range: range.clone(),
value: *value,
})
}
}
/// This structure should be used in cases where a value must be
/// exactly equal to another value for the process to be valid.
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct Mismatch<T: Copy + Eq> {
/// The expected return type
pub(crate) expected: T,
/// What was actually received
pub(crate) actual: T,
}
impl<T: Debug + Copy + Eq> Debug for Mismatch<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Mismatch")
.field("expected", &self.expected)
.field("actual", &self.actual)
.finish()
}
}
impl<T: Display + Copy + Eq> Display for Mismatch<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Expected {}, but got {}", self.expected, self.actual)
}
}
/// This structure should be used in cases where a value must lie
/// within a specific range
#[derive(Clone)]
pub struct Outside<T: Ord + Copy> {
/// The inclusive range into which the value must enter.
pub(crate) range: core::ops::Range<T>,
/// The value that failed to be included into the range.
pub(crate) value: T,
}
impl<T: Ord + Copy + Debug> Debug for Outside<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Outside")
.field("range", &self.range)
.field("value", &self.value)
.finish()
}
}
impl<T: Ord + Copy + Display> Display for Outside<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.value >= self.range.end {
write!(f, "Value {} exceeds maximum {}", self.value, self.range.end)
} else if self.value < self.range.start {
write!(f, "Value {} below minimum {}", self.value, self.range.start)
} else {
panic!("An invalid instance of outside was created. Aborting")
}
}
}
impl<T: PartialEq + Ord + Copy> PartialEq for Outside<T> {
fn eq(&self, other: &Self) -> bool {
self.range == other.range && self.value == other.value
}
}
/// A thing is not a known value from a list
#[derive(PartialEq, Eq, Clone)]
pub struct Unknown<'a, T: Eq>{
/// The collection of things arranged in a linear sequence
pub(crate) knowns: Option<&'a [T]>,
/// The value that is not in the list
pub(crate) value: T,
}
impl<'a, T: Eq + Copy> Copy for Unknown<'a, T> {}
impl<T: Eq + Debug> Debug for Unknown<'_, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Unknown")
.field("knowns", &self.knowns)
.field("value", &self.value)
.finish()
}
}
impl<T: Eq + Display> Display for Unknown<'_, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "The value {} is not known", self.value)?;
if let Some(knowns) = self.knowns {
write!(f, "Because it's not one of [{}]", join(&knowns, ", ")?)
} else {
f.write_str(".")
}
}
}
pub fn join<T: Display>(items: &[T], separator: &'static str) -> Result<String, core::fmt::Error> {
use core::fmt::Write;
let first_element = items[0].to_string();
let mut buffer = String::with_capacity(
(items.len() - 1) * (separator.len() + first_element.len()) + first_element.len(),
);
for idx in 1..items.len() {
buffer.push_str(separator);
buffer.write_str(&items[idx].to_string())?;
}
Ok(buffer)
}
pub fn assert_known_enum<'a, T: Eq>(knowns: &'a [T], value: T) -> Result<T, Unknown<'a, T>> {
if knowns.contains(&value) {
Ok(value)
} else {
Err(Unknown {
knowns: Some(knowns),
value,
})
}
}
pub fn assert_known<'a, T: Eq>(knowns: &'a [T], value: T) -> Result<T, Unknown<'_, T>> {
if knowns.contains(&value) {
Ok(value)
} else {
Err(Unknown {
knowns: None,
value,
})
}
}
#[cfg(test)]
pub mod tests {
use crate::{Mismatch, Outside, Unknown};
#[test]
fn usage_of_assert_eq() {
assert_eq!(crate::assert_eq(&32_u32, &32), Ok(32));
assert_eq!(
crate::assert_eq(&32_u32, &33),
Err(Mismatch {
expected: 32,
actual: 33
})
);
}
#[test]
fn usage_of_outside() {
assert_eq!(crate::assert_in(&2, &(1..5)), Ok(2));
assert_eq!(crate::assert_in(&5, &(1..5)), Ok(5));
assert_eq!(
crate::assert_in(&6, &(1..5)),
Err(Outside {
range: 1..5,
value: 6
})
);
assert_eq!(
crate::assert_in(&0, &(1..5)),
Err(Outside {
range: 1..5,
value: 0
})
);
}
#[test]
fn usage_of_unknown() {
let knowns = vec![1, 2, 4, 6, 7, 20_u32];
assert_eq!(crate::assert_known_enum(&knowns, 2), Ok(2));
assert_eq!(
crate::assert_known_enum(&knowns, 3),
Err(Unknown {
knowns: Some(&knowns),
value: 3
})
);
assert_eq!(crate::assert_known(&knowns, 2), Ok(2));
assert_eq!(
crate::assert_known(&knowns, 3),
Err(Unknown {
knowns: None,
value: 3
})
);
}
}