From 00407542e4623bc85b1bb438958b58779b422a93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Mon, 17 Oct 2022 11:19:08 +0200 Subject: [PATCH] feat: add helpers module --- src/helpers.rs | 2 ++ src/lib.rs | 33 +++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 14 deletions(-) create mode 100644 src/helpers.rs diff --git a/src/helpers.rs b/src/helpers.rs new file mode 100644 index 0000000..e2db615 --- /dev/null +++ b/src/helpers.rs @@ -0,0 +1,2 @@ +// Use this file if you want to extract sections of your solutions. +// Example import: `use aoc::helpers::example_fn;` diff --git a/src/lib.rs b/src/lib.rs index c55ec67..89bd78d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,10 @@ +// This file contains template helpers. +// Prefer `./helpers.rs` if you want to extract code from your solutions. use std::env; use std::fs; +pub mod helpers; + pub const ANSI_ITALIC: &str = "\x1b[3m"; pub const ANSI_BOLD: &str = "\x1b[1m"; pub const ANSI_RESET: &str = "\x1b[0m"; @@ -52,7 +56,7 @@ pub fn parse_exec_time(output: &str) -> f64 { } else { let timing = l.split("(elapsed: ").last().unwrap(); // use `contains` istd. of `ends_with`: string may contain ANSI escape sequences. - // possible time formats: see [rust/library/core/src/time.rs](https://github.com/rust-lang/rust/blob/1.57.0/library/core/src/time.rs#L1225-L1249). + // for possible time formats, see: https://github.com/rust-lang/rust/blob/1.64.0/library/core/src/time.rs#L1176-L1200 if timing.contains("ns)") { acc // range below rounding precision. } else if timing.contains("µs)") { @@ -68,23 +72,24 @@ pub fn parse_exec_time(output: &str) -> f64 { }) } +/// copied from: https://github.com/rust-lang/rust/blob/1.64.0/library/std/src/macros.rs#L328-L333 +#[cfg(test)] +macro_rules! assert_approx_eq { + ($a:expr, $b:expr) => {{ + let (a, b) = (&$a, &$b); + assert!( + (*a - *b).abs() < 1.0e-6, + "{} is not approximately equal to {}", + *a, + *b + ); + }}; +} + #[cfg(test)] mod tests { use super::*; - /// copied from: [rust/library/std/src/macros.rs](https://github.com/rust-lang/rust/blob/1.57.0/library/std/src/macros.rs#L311-L316) - macro_rules! assert_approx_eq { - ($a:expr, $b:expr) => {{ - let (a, b) = (&$a, &$b); - assert!( - (*a - *b).abs() < 1.0e-6, - "{} is not approximately equal to {}", - *a, - *b - ); - }}; - } - #[test] fn test_parse_exec_time() { assert_approx_eq!(