From 0c6a4e01a3dcd378f308e10c5580ff7bf4037935 Mon Sep 17 00:00:00 2001 From: Leandro Lisboa Penz Date: Sat, 16 Dec 2023 20:35:47 +0000 Subject: [PATCH] Import ok_or_eyre from future eyre (not in color_eyre yet) --- aoc/src/lib.rs | 23 +++++++++++++++++++++-- day16/src/bin/day16b.rs | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/aoc/src/lib.rs b/aoc/src/lib.rs index daf2ea1..a154022 100644 --- a/aoc/src/lib.rs +++ b/aoc/src/lib.rs @@ -2,8 +2,9 @@ // This file is subject to the terms and conditions defined in // file 'LICENSE', which is part of this source code package. +use std::fmt::{Debug, Display}; pub use std::io::{stdin, BufRead}; -pub use std::time::Instant; +use std::time::Instant; pub use color_eyre::eyre::eyre; pub use color_eyre::Report; @@ -35,7 +36,25 @@ pub mod parser { } } -pub fn do_main Result, T: std::fmt::Display>(f: F) -> Result<()> { +pub trait OptionExt { + fn ok_or_eyre(self, message: M) -> Result + where + M: Debug + Display + Send + Sync + 'static; +} + +impl OptionExt for Option { + fn ok_or_eyre(self, message: M) -> Result + where + M: Debug + Display + Send + Sync + 'static, + { + match self { + Some(ok) => Ok(ok), + None => Err(Report::msg(message)), + } + } +} + +pub fn do_main Result, T: Display>(f: F) -> Result<()> { color_eyre::install()?; let start = Instant::now(); println!("{}", f()?); diff --git a/day16/src/bin/day16b.rs b/day16/src/bin/day16b.rs index 2dba3ac..0f5e0b6 100644 --- a/day16/src/bin/day16b.rs +++ b/day16/src/bin/day16b.rs @@ -20,7 +20,7 @@ fn process(size: u16, bufin: impl BufRead) -> Result { }) .map(|start| calc_energized(size, &grid, &mut cache, start)) .max() - .ok_or(eyre!("max not found")) + .ok_or_eyre("max not found") } #[test]