ysono / pancake

7 stars 1 forks source link

Sort directory scan by filename. Add storage::utils mod. #16

Closed ysono closed 3 years ago

ysono commented 3 years ago

Sorting files from a dir scan.

I learned that we can collect() an iterable of Result<T> into a Result<Vec<T>>, hence early-terminating the iteration.

fn t_to_u(t: &T) -> Result<U> { ... }
let tt: Vec<T> = vec![];
let result_of_all: Result<Vec<U>> = tt.iter().map(t_to_u).collect();
let result_of_each: Vec<Result<U>> = tt.iter().map(t_to_u).collect();

And also the type signature is partially inferrable:

let result_of_all: Result<Vec<_>> = tt.iter().map(t_to_u).collect();
let result_of_each: Vec<Result<_>> = tt.iter().map(t_to_u).collect();

What I still don't understand is the best practice for converting between different error types, so we don't ever have to worry about specifying the E generic type of anyhow::Result. Googlling came up with thiserror (https://docs.rs/thiserror/1.0.26/thiserror/) which may be worth looking into.