Compare commits

..

No commits in common. "b756e8ca81f9941c107dd9674309ab66763fa5fa" and "27e1691854e807cbdc6ec63e86c1a06ec7514dd4" have entirely different histories.

7 changed files with 77 additions and 78 deletions

14
Cargo.lock generated
View File

@ -861,6 +861,13 @@ dependencies = [
"miniz_oxide 0.7.1",
]
[[package]]
name = "flow"
version = "0.1.0"
dependencies = [
"thiserror",
]
[[package]]
name = "fluent"
version = "0.16.0"
@ -3112,13 +3119,6 @@ dependencies = [
"winreg",
]
[[package]]
name = "result-extended"
version = "0.1.0"
dependencies = [
"thiserror",
]
[[package]]
name = "rsa"
version = "0.9.2"

View File

@ -8,6 +8,7 @@ members = [
"dashboard",
"emseries",
"file-service",
"flow",
"fluent-ergonomics",
"geo-types",
"gm-control-panel",
@ -16,8 +17,7 @@ members = [
"kifu/core",
"kifu/gtk",
"memorycache",
"nom-training",
"result-extended",
"screenplay",
"sgf",
"nom-training",
]

View File

@ -11,6 +11,7 @@ RUST_ALL_TARGETS=(
"dashboard"
"emseries"
"file-service"
"flow"
"fluent-ergonomics"
"geo-types"
"gm-control-panel"
@ -19,8 +20,6 @@ RUST_ALL_TARGETS=(
"kifu-core"
"kifu-gtk"
"memorycache"
"nom-training"
"result-extended"
"screenplay"
"sgf"
)

View File

@ -1,5 +1,5 @@
[package]
name = "result-extended"
name = "flow"
version = "0.1.0"
edition = "2021"
license = "GPL-3.0-only"

View File

@ -33,84 +33,84 @@ use std::{error::Error, fmt};
/// statement.
pub trait FatalError: Error {}
/// Result<A, FE, E> represents a return value that might be a success, might be a fatal error, or
/// Flow<A, FE, E> represents a return value that might be a success, might be a fatal error, or
/// might be a normal handleable error.
pub enum Result<A, E, FE> {
pub enum Flow<A, FE, E> {
/// The operation was successful
Ok(A),
/// Ordinary errors. These should be handled and the application should recover gracefully.
Err(E),
/// The operation encountered a fatal error. These should be bubbled up to a level that can
/// safely shut the application down.
Fatal(FE),
/// Ordinary errors. These should be handled and the application should recover gracefully.
Err(E),
}
impl<A, E, FE> Result<A, E, FE> {
impl<A, FE, E> Flow<A, FE, E> {
/// Apply an infallible function to a successful value.
pub fn map<B, O>(self, mapper: O) -> Result<B, E, FE>
pub fn map<B, O>(self, mapper: O) -> Flow<B, FE, E>
where
O: FnOnce(A) -> B,
{
match self {
Result::Ok(val) => Result::Ok(mapper(val)),
Result::Err(err) => Result::Err(err),
Result::Fatal(err) => Result::Fatal(err),
Flow::Ok(val) => Flow::Ok(mapper(val)),
Flow::Fatal(err) => Flow::Fatal(err),
Flow::Err(err) => Flow::Err(err),
}
}
/// Apply a potentially fallible function to a successful value.
///
/// Like `Result.and_then`, the mapping function can itself fail.
pub fn and_then<B, O>(self, handler: O) -> Result<B, E, FE>
pub fn and_then<B, O>(self, handler: O) -> Flow<B, FE, E>
where
O: FnOnce(A) -> Result<B, E, FE>,
O: FnOnce(A) -> Flow<B, FE, E>,
{
match self {
Result::Ok(val) => handler(val),
Result::Err(err) => Result::Err(err),
Result::Fatal(err) => Result::Fatal(err),
Flow::Ok(val) => handler(val),
Flow::Fatal(err) => Flow::Fatal(err),
Flow::Err(err) => Flow::Err(err),
}
}
/// 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
/// work with `Result`, so you will likely need to use this a lot.
pub fn map_err<F, O>(self, mapper: O) -> Result<A, F, FE>
/// work with `Flow`, so you will likely need to use this a lot.
pub fn map_err<F, O>(self, mapper: O) -> Flow<A, FE, F>
where
O: FnOnce(E) -> F,
{
match self {
Result::Ok(val) => Result::Ok(val),
Result::Err(err) => Result::Err(mapper(err)),
Result::Fatal(err) => Result::Fatal(err),
Flow::Ok(val) => Flow::Ok(val),
Flow::Fatal(err) => Flow::Fatal(err),
Flow::Err(err) => Flow::Err(mapper(err)),
}
}
/// Provide a function to use to recover from (or simply re-throw) an error.
pub fn or_else<O, F>(self, handler: O) -> Result<A, F, FE>
pub fn or_else<O, F>(self, handler: O) -> Flow<A, FE, F>
where
O: FnOnce(E) -> Result<A, F, FE>,
O: FnOnce(E) -> Flow<A, FE, F>,
{
match self {
Result::Ok(val) => Result::Ok(val),
Result::Err(err) => handler(err),
Result::Fatal(err) => Result::Fatal(err),
Flow::Ok(val) => Flow::Ok(val),
Flow::Fatal(err) => Flow::Fatal(err),
Flow::Err(err) => handler(err),
}
}
}
/// 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`.
impl<A, E, FE> From<std::result::Result<A, E>> for Result<A, E, FE> {
fn from(r: std::result::Result<A, E>) -> Self {
/// Convert from a normal `Result` type to a `Flow` type. The error condition for a `Result` will
/// be treated as `Flow::Err`, never `Flow::Fatal`.
impl<A, FE, E> From<Result<A, E>> for Flow<A, FE, E> {
fn from(r: Result<A, E>) -> Self {
match r {
Ok(val) => Result::Ok(val),
Err(err) => Result::Err(err),
Ok(val) => Flow::Ok(val),
Err(err) => Flow::Err(err),
}
}
}
impl<A, E, FE> fmt::Debug for Result<A, E, FE>
impl<A, FE, E> fmt::Debug for Flow<A, FE, E>
where
A: fmt::Debug,
FE: fmt::Debug,
@ -118,14 +118,14 @@ where
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Result::Ok(val) => f.write_fmt(format_args!("Result::Ok {:?}", val)),
Result::Err(err) => f.write_fmt(format_args!("Result::Err {:?}", err)),
Result::Fatal(err) => f.write_fmt(format_args!("Result::Fatal {:?}", err)),
Flow::Ok(val) => f.write_fmt(format_args!("Flow::Ok {:?}", val)),
Flow::Err(err) => f.write_fmt(format_args!("Flow::Err {:?}", err)),
Flow::Fatal(err) => f.write_fmt(format_args!("Flow::Fatal {:?}", err)),
}
}
}
impl<A, E, FE> PartialEq for Result<A, E, FE>
impl<A, FE, E> PartialEq for Flow<A, FE, E>
where
A: PartialEq,
FE: PartialEq,
@ -133,27 +133,27 @@ where
{
fn eq(&self, rhs: &Self) -> bool {
match (self, rhs) {
(Result::Ok(val), Result::Ok(rhs)) => val == rhs,
(Result::Err(_), Result::Err(_)) => true,
(Result::Fatal(_), Result::Fatal(_)) => true,
(Flow::Ok(val), Flow::Ok(rhs)) => val == rhs,
(Flow::Err(_), Flow::Err(_)) => true,
(Flow::Fatal(_), Flow::Fatal(_)) => true,
_ => false,
}
}
}
/// Convenience function to create an ok value.
pub fn ok<A, E: Error, FE: FatalError>(val: A) -> Result<A, E, FE> {
Result::Ok(val)
pub fn ok<A, FE: FatalError, E: Error>(val: A) -> Flow<A, FE, E> {
Flow::Ok(val)
}
/// Convenience function to create an error value.
pub fn error<A, E: Error, FE: FatalError>(err: E) -> Result<A, E, FE> {
Result::Err(err)
pub fn error<A, FE: FatalError, E: Error>(err: E) -> Flow<A, FE, E> {
Flow::Err(err)
}
/// Convenience function to create a fatal value.
pub fn fatal<A, E: Error, FE: FatalError>(err: FE) -> Result<A, E, FE> {
Result::Fatal(err)
pub fn fatal<A, FE: FatalError, E: Error>(err: FE) -> Flow<A, FE, E> {
Flow::Fatal(err)
}
/// Return early from the current function if the value is a fatal error.
@ -161,9 +161,9 @@ pub fn fatal<A, E: Error, FE: FatalError>(err: FE) -> Result<A, E, FE> {
macro_rules! return_fatal {
($x:expr) => {
match $x {
Result::Fatal(err) => return Result::Fatal(err),
Result::Err(err) => Err(err),
Result::Ok(val) => Ok(val),
Flow::Fatal(err) => return Flow::Fatal(err),
Flow::Err(err) => Err(err),
Flow::Ok(val) => Ok(val),
}
};
}
@ -173,9 +173,9 @@ macro_rules! return_fatal {
macro_rules! return_error {
($x:expr) => {
match $x {
Result::Ok(val) => val,
Result::Err(err) => return Result::Err(err),
Result::Fatal(err) => return Result::Fatal(err),
Flow::Ok(val) => val,
Flow::Err(err) => return Flow::Err(err),
Flow::Fatal(err) => return Flow::Fatal(err),
}
};
}
@ -210,45 +210,45 @@ mod test {
#[test]
fn it_can_map_things() {
let success: Result<i32, Error, FatalError> = ok(15);
let success: Flow<i32, FatalError, Error> = ok(15);
assert_eq!(ok(16), success.map(|v| v + 1));
}
#[test]
fn it_can_chain_success() {
let success: Result<i32, Error, FatalError> = ok(15);
let success: Flow<i32, FatalError, Error> = ok(15);
assert_eq!(ok(16), success.and_then(|v| ok(v + 1)));
}
#[test]
fn it_can_handle_an_error() {
let failure: Result<i32, Error, FatalError> = error(Error::Error);
let failure: Flow<i32, FatalError, Error> = error(Error::Error);
assert_eq!(
ok::<i32, Error, FatalError>(16),
ok::<i32, FatalError, Error>(16),
failure.or_else(|_| ok(16))
);
}
#[test]
fn early_exit_on_fatal() {
fn ok_func() -> Result<i32, Error, FatalError> {
let value = return_fatal!(ok::<i32, Error, FatalError>(15));
fn ok_func() -> Flow<i32, FatalError, Error> {
let value = return_fatal!(ok::<i32, FatalError, Error>(15));
match value {
Ok(_) => ok(14),
Err(err) => error(err),
}
}
fn err_func() -> Result<i32, Error, FatalError> {
let value = return_fatal!(error::<i32, Error, FatalError>(Error::Error));
fn err_func() -> Flow<i32, FatalError, Error> {
let value = return_fatal!(error::<i32, FatalError, Error>(Error::Error));
match value {
Ok(_) => panic!("shouldn't have gotten here"),
Err(_) => ok(0),
}
}
fn fatal_func() -> Result<i32, Error, FatalError> {
let _ = return_fatal!(fatal::<i32, Error, FatalError>(FatalError::FatalError));
fn fatal_func() -> Flow<i32, FatalError, Error> {
return_fatal!(fatal::<i32, FatalError, Error>(FatalError::FatalError));
panic!("failed to bail");
}
@ -259,19 +259,19 @@ mod test {
#[test]
fn it_can_early_exit_on_all_errors() {
fn ok_func() -> Result<i32, Error, FatalError> {
let value = return_error!(ok::<i32, Error, FatalError>(15));
fn ok_func() -> Flow<i32, FatalError, Error> {
let value = return_error!(ok::<i32, FatalError, Error>(15));
assert_eq!(value, 15);
ok(14)
}
fn err_func() -> Result<i32, Error, FatalError> {
return_error!(error::<i32, Error, FatalError>(Error::Error));
fn err_func() -> Flow<i32, FatalError, Error> {
return_error!(error::<i32, FatalError, Error>(Error::Error));
panic!("failed to bail");
}
fn fatal_func() -> Result<i32, Error, FatalError> {
return_error!(fatal::<i32, Error, FatalError>(FatalError::FatalError));
fn fatal_func() -> Flow<i32, FatalError, Error> {
return_error!(fatal::<i32, FatalError, Error>(FatalError::FatalError));
panic!("failed to bail");
}