Render a file list from the filesystem #24

Merged
savanni merged 9 commits from track-list into main 2023-02-26 03:17:01 +00:00
1 changed files with 39 additions and 26 deletions
Showing only changes of commit 0d00b17cb4 - Show all commits

View File

@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with Lum
//! Where the sled.rs library uses `Result<Result<A, Error>, FatalError>`, these are a little hard to //! 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 //! 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. //! nested errors in the same way as a regular Result.
use std::error::Error; use std::{error::Error, fmt};
/// Implement this trait for the application's fatal errors. /// Implement this trait for the application's fatal errors.
/// ///
@ -110,6 +110,37 @@ impl<A, FE, E> From<Result<A, E>> for Flow<A, FE, E> {
} }
} }
impl<A, FE, E> fmt::Debug for Flow<A, FE, E>
where
A: fmt::Debug,
FE: fmt::Debug,
E: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
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, FE, E> PartialEq for Flow<A, FE, E>
where
A: PartialEq,
FE: PartialEq,
E: PartialEq,
{
fn eq(&self, rhs: &Self) -> bool {
match (self, rhs) {
(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. /// Convenience function to create an ok value.
pub fn ok<A, FE: FatalError, E: Error>(val: A) -> Flow<A, FE, E> { pub fn ok<A, FE: FatalError, E: Error>(val: A) -> Flow<A, FE, E> {
Flow::Ok(val) Flow::Ok(val)
@ -177,43 +208,25 @@ mod test {
} }
} }
impl PartialEq for Flow<i32, FatalError, Error> {
fn eq(&self, rhs: &Self) -> bool {
match (self, rhs) {
(Flow::Ok(val), Flow::Ok(rhs)) => val == rhs,
(Flow::Err(_), Flow::Err(_)) => true,
(Flow::Fatal(_), Flow::Fatal(_)) => true,
_ => false,
}
}
}
impl std::fmt::Debug for Flow<i32, FatalError, Error> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
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)),
}
}
}
#[test] #[test]
fn it_can_map_things() { fn it_can_map_things() {
let success = ok(15); let success: Flow<i32, FatalError, Error> = ok(15);
assert_eq!(ok(16), success.map(|v| v + 1)); assert_eq!(ok(16), success.map(|v| v + 1));
} }
#[test] #[test]
fn it_can_chain_success() { fn it_can_chain_success() {
let success = ok(15); let success: Flow<i32, FatalError, Error> = ok(15);
assert_eq!(ok(16), success.and_then(|v| ok(v + 1))); assert_eq!(ok(16), success.and_then(|v| ok(v + 1)));
} }
#[test] #[test]
fn it_can_handle_an_error() { fn it_can_handle_an_error() {
let failure = error(Error::Error); let failure: Flow<i32, FatalError, Error> = error(Error::Error);
assert_eq!(ok(16), failure.or_else(|_| ok(16))); assert_eq!(
ok::<i32, FatalError, Error>(16),
failure.or_else(|_| ok(16))
);
} }
#[test] #[test]