Macro optics::optics

source ·
macro_rules! optics {
    () => { ... };
    ($single:tt) => { ... };
    ($head:tt $(. $tail:tt)*) => { ... };
}
Expand description

Easy composition for optics. See also Compose.

let val: Result<(bool, Option<u32>), &str> = Ok((true, Some(42)));
assert_eq!(optics!(_Ok._1._Some).preview(val), Ok(42));

Compose implement Debug and Display in human readable format:

assert_eq!(format!("{}", optics!(_Ok._1._Some)), "Ok.1.Some".to_string());
assert_eq!(
    format!("{:?}", optics!(_Ok._1._Some)),
    "Result::Ok\
    .{(T0, T1),(T0, T1, T2),(T0, T1, T2, T3)}::1\
    .Option::Some".to_string()
);

Compose implements provides error types through the OpticsFallible interface. The error type implements Debug and Display, indicating the shortest prefix of this Composed optics responsible for this error.

let val: Result<(bool, Option<u32>), &str> = Err("top-level mismatch");
assert_eq!(optics!(_Ok._1._Some).preview(val).unwrap_err().to_string(), "Ok");
let val: Result<(bool, Option<u32>), &str> = Ok((true, None));
assert_eq!(optics!(_Ok._1._Some).preview(val).unwrap_err().to_string(), "Ok.1.Some");