Skip to content

Commit 0040754

Browse files
committed
feat: add helpers module
1 parent 75aa23d commit 0040754

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/helpers.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Use this file if you want to extract sections of your solutions.
2+
// Example import: `use aoc::helpers::example_fn;`

src/lib.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
// This file contains template helpers.
2+
// Prefer `./helpers.rs` if you want to extract code from your solutions.
13
use std::env;
24
use std::fs;
35

6+
pub mod helpers;
7+
48
pub const ANSI_ITALIC: &str = "\x1b[3m";
59
pub const ANSI_BOLD: &str = "\x1b[1m";
610
pub const ANSI_RESET: &str = "\x1b[0m";
@@ -52,7 +56,7 @@ pub fn parse_exec_time(output: &str) -> f64 {
5256
} else {
5357
let timing = l.split("(elapsed: ").last().unwrap();
5458
// use `contains` istd. of `ends_with`: string may contain ANSI escape sequences.
55-
// 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).
59+
// for possible time formats, see: https://github.com/rust-lang/rust/blob/1.64.0/library/core/src/time.rs#L1176-L1200
5660
if timing.contains("ns)") {
5761
acc // range below rounding precision.
5862
} else if timing.contains("µs)") {
@@ -68,23 +72,24 @@ pub fn parse_exec_time(output: &str) -> f64 {
6872
})
6973
}
7074

75+
/// copied from: https://github.com/rust-lang/rust/blob/1.64.0/library/std/src/macros.rs#L328-L333
76+
#[cfg(test)]
77+
macro_rules! assert_approx_eq {
78+
($a:expr, $b:expr) => {{
79+
let (a, b) = (&$a, &$b);
80+
assert!(
81+
(*a - *b).abs() < 1.0e-6,
82+
"{} is not approximately equal to {}",
83+
*a,
84+
*b
85+
);
86+
}};
87+
}
88+
7189
#[cfg(test)]
7290
mod tests {
7391
use super::*;
7492

75-
/// 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)
76-
macro_rules! assert_approx_eq {
77-
($a:expr, $b:expr) => {{
78-
let (a, b) = (&$a, &$b);
79-
assert!(
80-
(*a - *b).abs() < 1.0e-6,
81-
"{} is not approximately equal to {}",
82-
*a,
83-
*b
84-
);
85-
}};
86-
}
87-
8893
#[test]
8994
fn test_parse_exec_time() {
9095
assert_approx_eq!(

0 commit comments

Comments
 (0)