2023-02-11 18:05:13 +00:00
|
|
|
/*
|
|
|
|
Copyright 2023, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
|
|
|
|
|
|
|
This file is part of the Luminescent Dreams Tools.
|
|
|
|
|
|
|
|
Luminescent Dreams Tools is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
Luminescent Dreams Tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with Lumeto. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-02-11 17:59:15 +00:00
|
|
|
//! Control Flow for Correctness-Critical applications
|
|
|
|
//!
|
|
|
|
//! https://sled.rs/errors.html
|
|
|
|
//!
|
|
|
|
//! Where the sled.rs library uses `Result<Result<A, Error>, FatalError>`, these are a little hard to
|
|
|
|
//! work with. This library works out a set of utility functions that allow us to work with the
|
|
|
|
//! nested errors in the same way as a regular Result.
|
2023-02-26 03:17:00 +00:00
|
|
|
use std::{error::Error, fmt};
|
2023-02-11 17:59:15 +00:00
|
|
|
|
|
|
|
/// Implement this trait for the application's fatal errors.
|
|
|
|
///
|
|
|
|
/// Fatal errors should be things that should trigger an application shutdown. Applications should
|
|
|
|
/// not try to recover from fatal errors, but simply bring the app to the safest shutdown point and
|
|
|
|
/// report the best possible information to the user.
|
|
|
|
///
|
|
|
|
/// Examples: database corruption, or the database is unavailable in an application that cannot
|
|
|
|
/// function without it. Graphics environment cannot be initialized in a GUI app.
|
|
|
|
///
|
|
|
|
/// Applications should generally have only one FatalError type. There are no handling utilities
|
|
|
|
/// for Fatal conditions, so Fatal conditions must be handled through an ordinary `match`
|
|
|
|
/// statement.
|
|
|
|
pub trait FatalError: Error {}
|
|
|
|
|
2024-12-01 05:51:08 +00:00
|
|
|
/// ResultExt<A, FE, E> represents a return value that might be a success, might be a fatal error, or
|
2023-02-11 17:59:15 +00:00
|
|
|
/// might be a normal handleable error.
|
2024-12-01 05:51:08 +00:00
|
|
|
pub enum ResultExt<A, E, FE> {
|
2023-02-11 17:59:15 +00:00
|
|
|
/// The operation was successful
|
|
|
|
Ok(A),
|
2023-10-19 02:13:11 +00:00
|
|
|
/// Ordinary errors. These should be handled and the application should recover gracefully.
|
|
|
|
Err(E),
|
2023-02-11 17:59:15 +00:00
|
|
|
/// The operation encountered a fatal error. These should be bubbled up to a level that can
|
|
|
|
/// safely shut the application down.
|
|
|
|
Fatal(FE),
|
|
|
|
}
|
|
|
|
|
2024-12-01 05:51:08 +00:00
|
|
|
impl<A, E, FE> ResultExt<A, E, FE> {
|
2023-02-11 17:59:15 +00:00
|
|
|
/// Apply an infallible function to a successful value.
|
2024-12-01 05:51:08 +00:00
|
|
|
pub fn map<B, O>(self, mapper: O) -> ResultExt<B, E, FE>
|
2023-02-11 17:59:15 +00:00
|
|
|
where
|
|
|
|
O: FnOnce(A) -> B,
|
|
|
|
{
|
|
|
|
match self {
|
2024-12-01 05:51:08 +00:00
|
|
|
ResultExt::Ok(val) => ResultExt::Ok(mapper(val)),
|
|
|
|
ResultExt::Err(err) => ResultExt::Err(err),
|
|
|
|
ResultExt::Fatal(err) => ResultExt::Fatal(err),
|
2023-02-11 17:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Apply a potentially fallible function to a successful value.
|
|
|
|
///
|
|
|
|
/// Like `Result.and_then`, the mapping function can itself fail.
|
2024-12-01 05:51:08 +00:00
|
|
|
pub fn and_then<B, O>(self, handler: O) -> ResultExt<B, E, FE>
|
2023-02-11 17:59:15 +00:00
|
|
|
where
|
2024-12-01 05:51:08 +00:00
|
|
|
O: FnOnce(A) -> ResultExt<B, E, FE>,
|
2023-02-11 17:59:15 +00:00
|
|
|
{
|
|
|
|
match self {
|
2024-12-01 05:51:08 +00:00
|
|
|
ResultExt::Ok(val) => handler(val),
|
|
|
|
ResultExt::Err(err) => ResultExt::Err(err),
|
|
|
|
ResultExt::Fatal(err) => ResultExt::Fatal(err),
|
2023-02-11 17:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Map a normal error from one type to another. This is useful for converting an error from
|
|
|
|
/// one type to another, especially in re-throwing an underlying error. `?` syntax does not
|
2023-10-19 02:03:43 +00:00
|
|
|
/// work with `Result`, so you will likely need to use this a lot.
|
2024-12-01 05:51:08 +00:00
|
|
|
pub fn map_err<F, O>(self, mapper: O) -> ResultExt<A, F, FE>
|
2023-02-11 17:59:15 +00:00
|
|
|
where
|
|
|
|
O: FnOnce(E) -> F,
|
|
|
|
{
|
|
|
|
match self {
|
2024-12-01 05:51:08 +00:00
|
|
|
ResultExt::Ok(val) => ResultExt::Ok(val),
|
|
|
|
ResultExt::Err(err) => ResultExt::Err(mapper(err)),
|
|
|
|
ResultExt::Fatal(err) => ResultExt::Fatal(err),
|
2023-02-11 17:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Provide a function to use to recover from (or simply re-throw) an error.
|
2024-12-01 05:51:08 +00:00
|
|
|
pub fn or_else<O, F>(self, handler: O) -> ResultExt<A, F, FE>
|
2023-02-11 17:59:15 +00:00
|
|
|
where
|
2024-12-01 05:51:08 +00:00
|
|
|
O: FnOnce(E) -> ResultExt<A, F, FE>,
|
2023-02-11 17:59:15 +00:00
|
|
|
{
|
|
|
|
match self {
|
2024-12-01 05:51:08 +00:00
|
|
|
ResultExt::Ok(val) => ResultExt::Ok(val),
|
|
|
|
ResultExt::Err(err) => handler(err),
|
|
|
|
ResultExt::Fatal(err) => ResultExt::Fatal(err),
|
2023-02-11 17:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 02:03:43 +00:00
|
|
|
/// Convert from a normal `Result` type to a `Result` type. The error condition for a `Result` will
|
|
|
|
/// be treated as `Result::Err`, never `Result::Fatal`.
|
2024-12-01 05:51:08 +00:00
|
|
|
impl<A, E, FE> From<std::result::Result<A, E>> for ResultExt<A, E, FE> {
|
2023-10-19 02:03:43 +00:00
|
|
|
fn from(r: std::result::Result<A, E>) -> Self {
|
2023-02-11 17:59:15 +00:00
|
|
|
match r {
|
2024-12-01 05:51:08 +00:00
|
|
|
Ok(val) => ResultExt::Ok(val),
|
|
|
|
Err(err) => ResultExt::Err(err),
|
2023-02-11 17:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-01 05:51:08 +00:00
|
|
|
impl<A, E, FE> fmt::Debug for ResultExt<A, E, FE>
|
2023-02-26 03:17:00 +00:00
|
|
|
where
|
|
|
|
A: fmt::Debug,
|
|
|
|
FE: fmt::Debug,
|
|
|
|
E: fmt::Debug,
|
|
|
|
{
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
2024-12-01 05:51:08 +00:00
|
|
|
ResultExt::Ok(val) => f.write_fmt(format_args!("Result::Ok {:?}", val)),
|
|
|
|
ResultExt::Err(err) => f.write_fmt(format_args!("Result::Err {:?}", err)),
|
|
|
|
ResultExt::Fatal(err) => f.write_fmt(format_args!("Result::Fatal {:?}", err)),
|
2023-02-26 03:17:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-01 05:51:08 +00:00
|
|
|
impl<A, E, FE> PartialEq for ResultExt<A, E, FE>
|
2023-02-26 03:17:00 +00:00
|
|
|
where
|
|
|
|
A: PartialEq,
|
|
|
|
FE: PartialEq,
|
|
|
|
E: PartialEq,
|
|
|
|
{
|
|
|
|
fn eq(&self, rhs: &Self) -> bool {
|
|
|
|
match (self, rhs) {
|
2024-12-01 05:51:08 +00:00
|
|
|
(ResultExt::Ok(val), ResultExt::Ok(rhs)) => val == rhs,
|
|
|
|
(ResultExt::Err(_), ResultExt::Err(_)) => true,
|
|
|
|
(ResultExt::Fatal(_), ResultExt::Fatal(_)) => true,
|
2023-02-26 03:17:00 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-11 17:59:15 +00:00
|
|
|
/// Convenience function to create an ok value.
|
2024-12-01 05:51:08 +00:00
|
|
|
pub fn ok<A, E: Error, FE: FatalError>(val: A) -> ResultExt<A, E, FE> {
|
|
|
|
ResultExt::Ok(val)
|
2023-02-11 17:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience function to create an error value.
|
2024-12-01 05:51:08 +00:00
|
|
|
pub fn error<A, E: Error, FE: FatalError>(err: E) -> ResultExt<A, E, FE> {
|
|
|
|
ResultExt::Err(err)
|
2023-02-11 17:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience function to create a fatal value.
|
2024-12-01 05:51:08 +00:00
|
|
|
pub fn fatal<A, E: Error, FE: FatalError>(err: FE) -> ResultExt<A, E, FE> {
|
|
|
|
ResultExt::Fatal(err)
|
2023-02-11 17:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return early from the current function if the value is a fatal error.
|
2023-02-11 18:05:13 +00:00
|
|
|
#[macro_export]
|
2023-02-11 17:59:15 +00:00
|
|
|
macro_rules! return_fatal {
|
|
|
|
($x:expr) => {
|
|
|
|
match $x {
|
2024-12-01 05:51:08 +00:00
|
|
|
ResultExt::Fatal(err) => return ResultExt::Fatal(err),
|
|
|
|
ResultExt::Err(err) => Err(err),
|
|
|
|
ResultExt::Ok(val) => Ok(val),
|
2023-02-11 17:59:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
/// Return early from the current function is the value is an error.
|
|
|
|
macro_rules! return_error {
|
|
|
|
($x:expr) => {
|
|
|
|
match $x {
|
2024-12-01 05:51:08 +00:00
|
|
|
ResultExt::Ok(val) => val,
|
|
|
|
ResultExt::Err(err) => return ResultExt::Err(err),
|
|
|
|
ResultExt::Fatal(err) => return ResultExt::Fatal(err),
|
2023-02-11 17:59:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
enum FatalError {
|
|
|
|
#[error("A fatal error occurred")]
|
|
|
|
FatalError,
|
|
|
|
}
|
|
|
|
impl super::FatalError for FatalError {}
|
|
|
|
impl PartialEq for FatalError {
|
2023-02-11 18:05:13 +00:00
|
|
|
fn eq(&self, _rhs: &Self) -> bool {
|
2023-02-11 17:59:15 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
enum Error {
|
|
|
|
#[error("an error occurred")]
|
|
|
|
Error,
|
|
|
|
}
|
|
|
|
impl PartialEq for Error {
|
2023-02-11 18:05:13 +00:00
|
|
|
fn eq(&self, _rhs: &Self) -> bool {
|
2023-02-11 17:59:15 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn it_can_map_things() {
|
2024-12-01 05:51:08 +00:00
|
|
|
let success: ResultExt<i32, Error, FatalError> = ok(15);
|
2023-02-11 17:59:15 +00:00
|
|
|
assert_eq!(ok(16), success.map(|v| v + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn it_can_chain_success() {
|
2024-12-01 05:51:08 +00:00
|
|
|
let success: ResultExt<i32, Error, FatalError> = ok(15);
|
2023-02-11 17:59:15 +00:00
|
|
|
assert_eq!(ok(16), success.and_then(|v| ok(v + 1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn it_can_handle_an_error() {
|
2024-12-01 05:51:08 +00:00
|
|
|
let failure: ResultExt<i32, Error, FatalError> = error(Error::Error);
|
2023-02-26 03:17:00 +00:00
|
|
|
assert_eq!(
|
2023-10-19 02:13:11 +00:00
|
|
|
ok::<i32, Error, FatalError>(16),
|
2023-02-26 03:17:00 +00:00
|
|
|
failure.or_else(|_| ok(16))
|
|
|
|
);
|
2023-02-11 17:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn early_exit_on_fatal() {
|
2024-12-01 05:51:08 +00:00
|
|
|
fn ok_func() -> ResultExt<i32, Error, FatalError> {
|
2023-10-19 02:13:11 +00:00
|
|
|
let value = return_fatal!(ok::<i32, Error, FatalError>(15));
|
2023-02-11 17:59:15 +00:00
|
|
|
match value {
|
|
|
|
Ok(_) => ok(14),
|
|
|
|
Err(err) => error(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-01 05:51:08 +00:00
|
|
|
fn err_func() -> ResultExt<i32, Error, FatalError> {
|
2023-10-19 02:13:11 +00:00
|
|
|
let value = return_fatal!(error::<i32, Error, FatalError>(Error::Error));
|
2023-02-11 17:59:15 +00:00
|
|
|
match value {
|
|
|
|
Ok(_) => panic!("shouldn't have gotten here"),
|
2023-02-11 18:05:13 +00:00
|
|
|
Err(_) => ok(0),
|
2023-02-11 17:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-01 05:51:08 +00:00
|
|
|
fn fatal_func() -> ResultExt<i32, Error, FatalError> {
|
2023-10-19 02:13:11 +00:00
|
|
|
let _ = return_fatal!(fatal::<i32, Error, FatalError>(FatalError::FatalError));
|
2023-02-11 17:59:15 +00:00
|
|
|
panic!("failed to bail");
|
|
|
|
}
|
|
|
|
|
|
|
|
fatal_func();
|
|
|
|
assert_eq!(ok_func(), ok(14));
|
|
|
|
assert_eq!(err_func(), ok(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn it_can_early_exit_on_all_errors() {
|
2024-12-01 05:51:08 +00:00
|
|
|
fn ok_func() -> ResultExt<i32, Error, FatalError> {
|
2023-10-19 02:13:11 +00:00
|
|
|
let value = return_error!(ok::<i32, Error, FatalError>(15));
|
2023-02-11 17:59:15 +00:00
|
|
|
assert_eq!(value, 15);
|
|
|
|
ok(14)
|
|
|
|
}
|
|
|
|
|
2024-12-01 05:51:08 +00:00
|
|
|
fn err_func() -> ResultExt<i32, Error, FatalError> {
|
2023-10-19 02:13:11 +00:00
|
|
|
return_error!(error::<i32, Error, FatalError>(Error::Error));
|
2023-02-11 17:59:15 +00:00
|
|
|
panic!("failed to bail");
|
|
|
|
}
|
|
|
|
|
2024-12-01 05:51:08 +00:00
|
|
|
fn fatal_func() -> ResultExt<i32, Error, FatalError> {
|
2023-10-19 02:13:11 +00:00
|
|
|
return_error!(fatal::<i32, Error, FatalError>(FatalError::FatalError));
|
2023-02-11 17:59:15 +00:00
|
|
|
panic!("failed to bail");
|
|
|
|
}
|
|
|
|
|
|
|
|
fatal_func();
|
|
|
|
assert_eq!(ok_func(), ok(14));
|
|
|
|
err_func();
|
|
|
|
}
|
|
|
|
}
|