Skip to content

Commit 18ce262

Browse files
committed
move try_unwrap.rs and print.rs into utils.rs
1 parent 101b712 commit 18ce262

File tree

6 files changed

+48
-36
lines changed

6 files changed

+48
-36
lines changed

src/input.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
//! Tools used to fetch input contents from adventofcode.com.
2+
13
use std::error::Error;
24
use std::fs::{create_dir_all, read_to_string, File};
35
use std::io::Write;
46
use std::io::{stdin, stdout};
57
use std::path::PathBuf;
68
use std::time::Instant;
79

8-
use crate::print::Line;
10+
use crate::utils::Line;
911

1012
const BASE_URL: &str = "https://adventofcode.com";
1113
const INPUT_DIR: &str = "input";

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
pub mod input;
22
pub mod parse;
3-
pub mod print;
43
pub mod run;
5-
pub mod try_unwrap;
4+
pub mod utils;
65

76
use std::path::PathBuf;
87

src/parse.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Macro used to parse input tokens.
2+
13
// Call `apply` macro on this generated form of token tree;
24
// $ctx, { day DAY { { gen GENERATOR } { { sol SOLUTION } { sol SOLUTION } } } }
35
#[macro_export]

src/run.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Generate code to run solutions.
2+
13
#[macro_export]
24
macro_rules! run_day {
35
(
@@ -41,7 +43,7 @@ macro_rules! run_gen {
4143

4244
// Run generator
4345
( $day: ident, $data: expr, { gen $generator: ident } ) => {{
44-
use $crate::print::Line;
46+
use $crate::utils::Line;
4547

4648
let start = Instant::now();
4749
let input = $day::$generator($data);
@@ -53,8 +55,7 @@ macro_rules! run_gen {
5355
// Run fallible generator
5456
( $day: ident, $data: expr, { gen_fallible $generator: ident } ) => {{
5557
use $crate::colored::*;
56-
use $crate::print::Line;
57-
use $crate::try_unwrap::TryUnwrap;
58+
use $crate::utils::{Line, TryUnwrap};
5859

5960
let start = Instant::now();
6061
let result = $day::$generator($data);
@@ -83,7 +84,7 @@ macro_rules! run_sol {
8384
// Run solution
8485
( $day: ident, $input: expr, { sol $solution: ident } ) => {{
8586
use $crate::colored::*;
86-
use $crate::print::Line;
87+
use $crate::utils::Line;
8788

8889
let start = Instant::now();
8990
let response = $day::$solution($input);
@@ -100,8 +101,7 @@ macro_rules! run_sol {
100101
// Run fallible solution
101102
( $day: ident, $input: expr, { sol_fallible $solution: ident } ) => {{
102103
use $crate::colored::*;
103-
use $crate::print::Line;
104-
use $crate::try_unwrap::TryUnwrap;
104+
use $crate::utils::{Line, TryUnwrap};
105105

106106
let start = Instant::now();
107107
let response = $day::$solution($input);
@@ -126,7 +126,7 @@ macro_rules! run_sol {
126126
macro_rules! skip_sol {
127127
({ $kind: tt $solution: ident }) => {{
128128
use $crate::colored::*;
129-
use $crate::print::Line;
129+
use $crate::utils::Line;
130130

131131
println!(
132132
" - {}",

src/try_unwrap.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/print.rs renamed to src/utils.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,45 @@
1+
//! General purpose utilities.
2+
13
use colored::*;
24

35
use std::cmp::min;
46
use std::fmt;
57
use std::iter;
68
use std::time::Duration;
79

10+
// ---
11+
// --- TryUnwrap: wrapper for Option and Result
12+
// ---
13+
14+
pub trait TryUnwrap {
15+
type Val;
16+
fn try_unwrap(self) -> Result<Self::Val, String>;
17+
}
18+
19+
impl<T> TryUnwrap for Option<T> {
20+
type Val = T;
21+
22+
fn try_unwrap(self) -> Result<Self::Val, String> {
23+
if let Some(val) = self {
24+
Ok(val)
25+
} else {
26+
Err("empty output".to_string())
27+
}
28+
}
29+
}
30+
31+
impl<T, E: fmt::Display> TryUnwrap for Result<T, E> {
32+
type Val = T;
33+
34+
fn try_unwrap(self) -> Result<Self::Val, String> {
35+
self.map_err(|err| format!("{}", err))
36+
}
37+
}
38+
39+
// ---
40+
// --- Line: helper struct for printing
41+
// ---
42+
843
const DEFAULT_WIDTH: usize = 30;
944

1045
/// Simple helper struct used to display lines with a dotted separator.

0 commit comments

Comments
 (0)