spacetimedb_data_structures/error_stream.rs
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
//! Types, traits, and macros for working with non-empty streams of errorrs.
//!
//! The `ErrorStream<_>` type provides a collection that stores a non-empty, unordered stream of errors.
//! This is valuable for collecting as many errors as possible before returning them to the user,
//! which allows the user to work through the errors in the order of their choosing.
//! This is particularly useful for CLI tools.
//!
//! Example usage:
//! ```
//! use spacetimedb_data_structures::error_stream::{
//! ErrorStream,
//! CombineErrors,
//! CollectAllErrors
//! };
//! use std::collections::HashSet;
//!
//! enum MyError { /* ... */ };
//! type MyErrors = ErrorStream<MyError>;
//!
//! type Name =
//! /* ... */
//! # String
//! ;
//!
//! type Age =
//! /* ... */
//! # i32
//! ;
//!
//! fn validate_name(name: String) -> Result<Name, MyErrors> {
//! // ...
//! # Ok(name)
//! }
//!
//! fn validate_age(age: i32) -> Result<Age, MyErrors> {
//! // ...
//! # Ok(age)
//! }
//!
//! fn validate_person(
//! name: String,
//! age: i32,
//! friends: Vec<String>
//! ) -> Result<(Name, Age, HashSet<Name>), MyErrors> {
//! // First, we perform some validation on various pieces
//! // of input data, WITHOUT using `?`.
//! let name: Result<Name, MyErrors> = validate_name(name);
//! let age: Result<Age, MyErrors> = validate_age(age);
//!
//! // If we have multiple pieces of data, we can use
//! // `collect_all_errors` to build an arbitrary collection from them.
//! // If there are any errors, all of these errors
//! // will be returned in a single ErrorStream.
//! let friends: Result<HashSet<Name>, MyErrors> = friends
//! .into_iter()
//! .map(validate_name)
//! .collect_all_errors();
//!
//! // Now, we can combine the results into a single result.
//! // If there are any errors, they will be returned in a
//! // single ErrorStream.
//! let (name, age, friends): (Name, Age, HashSet<Name>) =
//! (name, age, friends).combine_errors()?;
//!
//! Ok((name, age, friends))
//! }
//! ```
//!
//! ## Best practices
//!
//! ### Use `ErrorStream` everywhere
//! It is best to use `ErrorStream` everywhere in a multiple-error module, even
//! for methods that return only a single error. `CombineAllErrors` and `CollectAllErrors`
//! can only be implemented for types built from `Result<_, ErrorStream<_>>` due to trait conflicts.
//! `ErrorStream` uses a `smallvec::SmallVec` internally, so it is efficient for single errors.
//!
//! You can convert an `E` to an `ErrorStream<E>` using `.into()`.
//!
//! ### Not losing any errors
//! When using this module, it is best to avoid using `?` until as late as possible,
//! and to completely avoid using the `Result<Collection<_>, _>::collect` method.
//! Both of these may result in errors being discarded.
//!
//! Prefer using `Result::and_then` for chaining operations that may fail,
//! and `CollectAllErrors::collect_all_errors` for collecting errors from iterators.
use crate::map::HashSet;
use std::{fmt, hash::Hash};
/// A non-empty stream of errors.
///
/// Logically, this type is unordered, and it is not guaranteed that the errors will be returned in the order they were added.
/// Attach identifying information to your errors if you want to sort them.
///
/// This struct is intended to be used with:
/// - The [CombineErrors] trait, which allows you to combine a tuples of results.
/// - The [CollectAllErrors] trait, which allows you to collect errors from an iterator of results.
///
/// To create an `ErrorStream` from a single error, you can use `from` or `into`:
/// ```
/// use spacetimedb_data_structures::error_stream::ErrorStream;
///
/// enum MyError {
/// A(u32),
/// B
/// }
///
/// let error: ErrorStream<MyError> = MyError::A(1).into();
/// // or
/// let error = ErrorStream::from(MyError::A(1));
/// ```
///
/// This does not allocate (unless your error allocates, of course).
#[derive(thiserror::Error, Debug, Clone, Default, PartialEq, Eq)]
pub struct ErrorStream<E>(smallvec::SmallVec<[E; 1]>);
impl<E> ErrorStream<E> {
/// Build an error stream from a non-empty collection.
/// If the collection is empty, panic.
pub fn expect_nonempty<I: IntoIterator<Item = E>>(errors: I) -> Self {
let mut errors = errors.into_iter();
let first = errors.next().expect("expected at least one error");
let mut stream = Self::from(first);
stream.extend(errors);
stream
}
/// Add some extra errors to a result.
///
/// If there are no errors, the result is not modified.
/// If there are errors, and the result is `Err`, the errors are added to the stream.
/// If there are errors, and the result is `Ok`, the `Ok` value is discarded, and the errors are returned in a stream.
pub fn add_extra_errors<T>(
result: Result<T, ErrorStream<E>>,
extra_errors: impl IntoIterator<Item = E>,
) -> Result<T, ErrorStream<E>> {
match result {
Ok(value) => {
let errors: SmallVec<[E; 1]> = extra_errors.into_iter().collect();
if errors.is_empty() {
Ok(value)
} else {
Err(ErrorStream(errors))
}
}
Err(mut errors) => {
errors.extend(extra_errors);
Err(errors)
}
}
}
/// Returns an iterator over the errors in the stream.
pub fn iter(&self) -> impl Iterator<Item = &E> {
self.0.iter()
}
/// Returns a mutable iterator over the errors in the stream.
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut E> {
self.0.iter_mut()
}
/// Returns an iterator over the errors in the stream, consuming the stream.
pub fn drain(&mut self) -> impl Iterator<Item = E> + '_ {
self.0.drain(..)
}
/// Push an error onto the stream.
pub fn push(&mut self, error: E) {
self.0.push(error);
}
/// Extend the stream with another stream.
pub fn extend(&mut self, other: impl IntoIterator<Item = E>) {
self.0.extend(other);
}
/// Unpack an error into `self`, returning `None` if there was an error.
/// This is not exposed because `CombineErrors` is more convenient.
#[inline(never)] // don't optimize this too much
#[cold]
fn unpack<T, ES: Into<ErrorStream<E>>>(&mut self, result: Result<T, ES>) -> Option<T> {
match result {
Ok(value) => Some(value),
Err(error) => {
self.0.extend(error.into().0);
None
}
}
}
}
impl<E: fmt::Display> fmt::Display for ErrorStream<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "Errors occurred:")?;
for error in self.iter() {
writeln!(f, "{}\n", error)?;
}
Ok(())
}
}
impl<E: Ord + Eq> ErrorStream<E> {
/// Sort and deduplicate the errors in the error stream.
pub fn sort_deduplicate(mut self) -> Self {
self.0.sort_unstable();
self.0.dedup();
self
}
}
impl<E: Eq + Hash> ErrorStream<E> {
/// Hash and deduplicate the errors in the error stream.
/// The resulting error stream has an arbitrary order.
pub fn hash_deduplicate(mut self) -> Self {
let set = self.0.drain(..).collect::<HashSet<_>>();
self.0.extend(set);
self
}
}
impl<E> IntoIterator for ErrorStream<E> {
type Item = <smallvec::SmallVec<[E; 1]> as IntoIterator>::Item;
type IntoIter = <smallvec::SmallVec<[E; 1]> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<E> From<E> for ErrorStream<E> {
fn from(error: E) -> Self {
Self(smallvec::smallvec![error])
}
}
/// A trait for converting a tuple of `Result<_, ErrorStream<_>>`s
/// into a `Result` of a tuple or combined `ErrorStream`.
pub trait CombineErrors {
/// The type of the output if all results are `Ok`.
type Ok;
/// The type of the output if any result is `Err`.
type Error;
/// Combine errors from multiple places into one.
/// This can be thought of as a kind of parallel `?`.
///
/// If your goal is to show the user as many errors as possible, you should
/// call this as late as possible, on as wide a tuple as you can.
///
/// Example usage:
///
/// ```
/// use spacetimedb_data_structures::error_stream::{ErrorStream, CombineErrors};
///
/// struct MyError { cause: String };
///
/// fn age() -> Result<i32, ErrorStream<MyError>> {
/// //...
/// # Ok(1)
/// }
///
/// fn name() -> Result<String, ErrorStream<MyError>> {
/// // ...
/// # Ok("hi".into())
/// }
///
/// fn likes_dogs() -> Result<bool, ErrorStream<MyError>> {
/// // ...
/// # Ok(false)
/// }
///
/// fn description() -> Result<String, ErrorStream<MyError>> {
/// // A typical usage of the API:
/// // Collect multiple `Result`s in parallel, only using
/// // `.combine_errors()?` once no more progress can be made.
/// let (age, name, likes_dogs) =
/// (age(), name(), likes_dogs()).combine_errors()?;
///
/// Ok(format!(
/// "{} is {} years old and {}",
/// name,
/// age,
/// if likes_dogs { "likes dogs" } else { "does not like dogs" }
/// ))
/// }
/// ```
fn combine_errors(self) -> Result<Self::Ok, ErrorStream<Self::Error>>;
}
macro_rules! tuple_combine_errors {
($($T:ident),*) => {
impl<$($T,)* E> CombineErrors for ($(Result<$T, ErrorStream<E>>,)*) {
type Ok = ($($T,)*);
type Error = E;
#[allow(non_snake_case)]
fn combine_errors(self) -> Result<Self::Ok, ErrorStream<Self::Error>> {
let mut errors = ErrorStream(Default::default());
let ($($T,)* ) = self;
$(
let $T = errors.unpack($T);
)*
if errors.0.is_empty() {
// correctness: none of these pushed an error to `errors`, so by the contract of `unpack`, they must all be `Some`.
Ok(($($T.unwrap(),)*))
} else {
Err(errors)
}
}
}
};
}
tuple_combine_errors!(T1, T2);
tuple_combine_errors!(T1, T2, T3);
tuple_combine_errors!(T1, T2, T3, T4);
tuple_combine_errors!(T1, T2, T3, T4, T5);
tuple_combine_errors!(T1, T2, T3, T4, T5, T6);
tuple_combine_errors!(T1, T2, T3, T4, T5, T6, T7);
tuple_combine_errors!(T1, T2, T3, T4, T5, T6, T7, T8);
tuple_combine_errors!(T1, T2, T3, T4, T5, T6, T7, T8, T9);
tuple_combine_errors!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
tuple_combine_errors!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
tuple_combine_errors!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);
/// A trait for collecting errors from an iterator of results,
/// returning all errors if anything failed.
pub trait CollectAllErrors {
/// The item type we are aggregating.
type Item;
/// The error type we are aggregating.
type Error;
/// Collect errors from an iterator of results into a single error stream.
/// If all results are `Ok`, returns the collected values. Otherwise,
/// combine all errors into a single error stream, and return it.
///
/// You CANNOT use the standard library function `Result<T, ErrorStream<E>>::collect()` for this,
/// as it will return the FIRST error it encounters, rather than collecting all errors!
///
/// The collection can be anything that implements `FromIterator`.
///
/// Example usage:
/// ```
/// use spacetimedb_data_structures::error_stream::{
/// ErrorStream,
/// CollectAllErrors
/// };
/// use std::collections::HashSet;
///
/// enum MyError { /* ... */ }
///
/// fn operation(
/// data: String,
/// checksum: u32
/// ) -> Result<i32, ErrorStream<MyError>> {
/// /* ... */
/// # Ok(1)
/// }
///
/// fn many_operations(
/// data: Vec<(String, u32)>
/// ) -> Result<HashSet<i32>, ErrorStream<MyError>> {
/// data
/// .into_iter()
/// .map(|(data, checksum)| operation(data, checksum))
/// .collect_all_errors::<HashSet<_>>()
/// }
/// ```
fn collect_all_errors<C: FromIterator<Self::Item>>(self) -> Result<C, ErrorStream<Self::Error>>;
}
impl<T, E, I: Iterator<Item = Result<T, ErrorStream<E>>>> CollectAllErrors for I {
type Item = T;
type Error = E;
fn collect_all_errors<Collection: FromIterator<Self::Item>>(self) -> Result<Collection, ErrorStream<Self::Error>> {
// not in a valid state: contains no errors!
let mut all_errors = ErrorStream(Default::default());
let collection = self
.filter_map(|result| match result {
Ok(value) => Some(value),
Err(errors) => {
all_errors.extend(errors);
None
}
})
.collect::<Collection>();
if all_errors.0.is_empty() {
// invalid state is not returned.
Ok(collection)
} else {
// not empty, so we're good to return it.
Err(all_errors)
}
}
}
/// Helper macro to match against an error stream, expecting a specific error.
/// For use in tests.
/// Panics if a matching error is not found.
/// Multiple matches are allowed.
///
/// Parameters:
/// - `$result` must be a `Result<_, ErrorStream<E>>`.
/// - `$expected` is a pattern to match against the error.
/// - `$cond` is an optional expression that should evaluate to `true` if the error matches.
/// Variables from `$expected` are bound in `$cond` behind references.
/// Do not use any asserts in `$cond` as it may be called against multiple errors.
///
/// ```
/// use spacetimedb_data_structures::error_stream::{
/// ErrorStream,
/// CollectAllErrors,
/// expect_error_matching
/// };
///
/// #[derive(PartialEq, Eq, Clone, Copy, Debug)]
/// struct CaseRef(u32);
///
/// #[derive(Debug)]
/// enum MyError {
/// InsufficientSwag { amount: u32 },
/// TooMuchSwag { reason: String, precedent: CaseRef },
/// SomethingElse(String)
/// }
///
/// let result: Result<(), ErrorStream<MyError>> = vec![
/// Err(MyError::TooMuchSwag {
/// reason: "sunglasses indoors".into(),
/// precedent: CaseRef(37)
/// }.into()),
/// Err(MyError::TooMuchSwag {
/// reason: "fur coat".into(),
/// precedent: CaseRef(55)
/// }.into()),
/// Err(MyError::SomethingElse(
/// "non-service animals forbidden".into()
/// ).into())
/// ].into_iter().collect_all_errors();
///
/// // This will panic if the error stream does not contain
/// // an error matching `MyError::SomethingElse`.
/// expect_error_matching!(
/// result,
/// MyError::SomethingElse(_)
/// );
///
/// // This will panic if the error stream does not contain
/// // an error matching `MyError::TooMuchSwag`, plus some
/// // extra conditions.
/// expect_error_matching!(
/// result,
/// MyError::TooMuchSwag { reason, precedent } =>
/// precedent == &CaseRef(37) && reason.contains("sunglasses")
/// );
/// ```
#[macro_export]
macro_rules! expect_error_matching (
($result:expr, $expected:pat => $cond:expr) => {
let result: &::std::result::Result<
_,
$crate::error_stream::ErrorStream<_>
> = &$result;
match result {
Ok(_) => panic!("expected error, but got Ok"),
Err(errors) => {
let err = errors.iter().find(|error|
if let $expected = error {
$cond
} else {
false
}
);
if let None = err {
panic!("expected error matching `{}` satisfying `{}`,\n but got {:#?}", stringify!($expected), stringify!($cond), errors);
}
}
}
};
($result:expr, $expected:pat) => {
let result: &::std::result::Result<
_,
$crate::error_stream::ErrorStream<_>
> = &$result;
match result {
Ok(_) => panic!("expected error, but got Ok"),
Err(errors) => {
let err = errors.iter().find(|error| matches!(error, $expected));
if let None = err {
panic!("expected error matching `{}`,\n but got {:#?}", stringify!($expected), errors);
}
}
}
};
);
// Make available in this module as well as crate root.
pub use expect_error_matching;
use smallvec::SmallVec;
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, PartialEq)]
enum MyError {
A(u32),
B,
}
type Result<T> = std::result::Result<T, ErrorStream<MyError>>;
#[test]
fn combine_errors() {
type ResultTuple = (Result<i32>, Result<String>, Result<u8>);
let tuple_1: ResultTuple = (Ok(1), Ok("hi".into()), Ok(3));
assert_eq!(tuple_1.combine_errors(), Ok((1, "hi".into(), 3)));
let tuple_2: ResultTuple = (Err(MyError::A(1).into()), Ok("hi".into()), Ok(3));
assert_eq!(tuple_2.combine_errors(), Err(MyError::A(1).into()));
let tuple_3: ResultTuple = (Err(MyError::A(1).into()), Err(MyError::A(2).into()), Ok(3));
assert_eq!(
tuple_3.combine_errors(),
Err(ErrorStream(smallvec::smallvec![MyError::A(1), MyError::A(2)]))
);
let tuple_4: ResultTuple = (
Err(MyError::A(1).into()),
Err(MyError::A(2).into()),
Err(MyError::A(3).into()),
);
assert_eq!(
tuple_4.combine_errors(),
Err(ErrorStream(smallvec::smallvec![
MyError::A(1),
MyError::A(2),
MyError::A(3)
]))
);
}
#[test]
fn collect_all_errors() {
let data: Vec<Result<i32>> = vec![Ok(1), Ok(2), Ok(3)];
assert_eq!(data.into_iter().collect_all_errors::<Vec<_>>(), Ok(vec![1, 2, 3]));
let data = vec![Ok(1), Err(MyError::A(0).into()), Ok(3)];
assert_eq!(
data.into_iter().collect_all_errors::<Vec<_>>(),
Err(ErrorStream([MyError::A(0)].into()))
);
let data: Vec<Result<i32>> = vec![
Err(MyError::A(1).into()),
Err(MyError::A(2).into()),
Err(MyError::A(3).into()),
];
assert_eq!(
data.into_iter().collect_all_errors::<Vec<_>>(),
Err(ErrorStream(smallvec::smallvec![
MyError::A(1),
MyError::A(2),
MyError::A(3)
]))
);
}
#[test]
#[should_panic]
fn expect_error_matching_without_cond_panics() {
let data: Result<()> = Err(ErrorStream(vec![MyError::B].into()));
expect_error_matching!(data, MyError::A(_));
}
#[test]
#[should_panic]
fn expect_error_matching_with_cond_panics() {
let data: Result<()> = Err(ErrorStream(vec![MyError::A(5), MyError::A(10)].into()));
expect_error_matching!(data, MyError::A(n) => n == &12);
}
}