Skip to content

Commit

Permalink
Chore: No need for common.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Mar 27, 2024
1 parent 2c85383 commit 721a9d6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
30 changes: 0 additions & 30 deletions src/common.rs

This file was deleted.

41 changes: 39 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use clap::Parser;
use std::fs;
use std::io;
use std::path::Path;
use std::path::PathBuf;

/// Solver for advent of code exercises
Expand All @@ -22,11 +25,31 @@ struct Args {
input: Option<PathBuf>,
}

mod common;
/// Returns a `String` input to use with a test.
///
/// If no argument is provided, the input will be read from STDIN.
///
/// # Arguments
/// * `file_path` - File input to read from.
pub fn fetch_input(file_path: Option<impl AsRef<Path>>) -> Result<String, std::io::Error> {
match file_path {
Some(filename) => fs::read_to_string(filename),
None => io::read_to_string(io::stdin()),
}
}

/// Software entry point
///
/// The arguments are taken from the command line inputs, parsed by Clap.
///
/// Arguments
/// * `year` - The year of the exercise, from 2015 to today.
/// * `day` - The day of the exercise, from 1 to 25.
/// * `part` - The part of the exercise, 1 or 2.
/// * `input` - Input file path (will read from STDIN if empty).
fn main() {
let args = Args::parse();
let input: String = match common::fetch_input(args.input) {
let input: String = match fetch_input(args.input) {
Ok(input_data) => input_data,
Err(error) => {
eprintln!("advent-rs: {}", error);
Expand All @@ -44,3 +67,17 @@ fn main() {
}
};
}

#[cfg(test)]
mod tests {
use super::*;
use std::io::ErrorKind;
use std::path::PathBuf;

#[test]
fn fetch_input_from_inexisting_file() {
let path: Option<PathBuf> = Some("foo.txt".into());
let error = fetch_input(path).unwrap_err();
assert_eq!(error.kind(), ErrorKind::NotFound);
}
}

0 comments on commit 721a9d6

Please sign in to comment.