Skip to content

Commit

Permalink
Import ok_or_eyre from future eyre (not in color_eyre yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpenz committed Dec 16, 2023
1 parent a096444 commit 0c6a4e0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
23 changes: 21 additions & 2 deletions aoc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,7 +36,25 @@ pub mod parser {
}
}

pub fn do_main<F: Fn() -> Result<T>, T: std::fmt::Display>(f: F) -> Result<()> {
pub trait OptionExt<T> {
fn ok_or_eyre<M>(self, message: M) -> Result<T>
where
M: Debug + Display + Send + Sync + 'static;
}

impl<T> OptionExt<T> for Option<T> {
fn ok_or_eyre<M>(self, message: M) -> Result<T>
where
M: Debug + Display + Send + Sync + 'static,
{
match self {
Some(ok) => Ok(ok),
None => Err(Report::msg(message)),
}
}
}

pub fn do_main<F: Fn() -> Result<T>, T: Display>(f: F) -> Result<()> {
color_eyre::install()?;
let start = Instant::now();
println!("{}", f()?);
Expand Down
2 changes: 1 addition & 1 deletion day16/src/bin/day16b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn process(size: u16, bufin: impl BufRead) -> Result<usize> {
})
.map(|start| calc_energized(size, &grid, &mut cache, start))
.max()
.ok_or(eyre!("max not found"))
.ok_or_eyre("max not found")
}

#[test]
Expand Down

0 comments on commit 0c6a4e0

Please sign in to comment.