Switch to a less verbose input error handling
This commit is contained in:
parent
0f2e4bdf47
commit
f00091cbf0
51
src/day7.rs
51
src/day7.rs
@ -2,45 +2,22 @@ extern crate regex;
|
|||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt;
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{BufRead, BufReader};
|
use std::io::{BufRead, BufReader};
|
||||||
|
use std::result;
|
||||||
|
|
||||||
use regex::{Captures, Regex};
|
use regex::{Captures, Regex};
|
||||||
|
|
||||||
|
type Result<T> = result::Result<T, Box<Error>>;
|
||||||
|
|
||||||
const INPUT: &str = "inputs/7.txt";
|
const INPUT: &str = "inputs/7.txt";
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
pub fn solve_part1() -> Result<String> {
|
||||||
struct MalformedInstruction {
|
|
||||||
details: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MalformedInstruction {
|
|
||||||
fn new(msg: &str) -> MalformedInstruction {
|
|
||||||
MalformedInstruction {
|
|
||||||
details: msg.to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for MalformedInstruction {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
write!(f, "{}", self.details)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Error for MalformedInstruction {
|
|
||||||
fn description(&self) -> &str {
|
|
||||||
&self.details
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn solve_part1() -> Result<String, Box<Error>> {
|
|
||||||
let mut instructions = read_instructions(INPUT)?;
|
let mut instructions = read_instructions(INPUT)?;
|
||||||
Ok(get_step_sequence(&mut instructions))
|
Ok(get_step_sequence(&mut instructions))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_instructions(filename: &str) -> Result<HashMap<String, Vec<String>>, Box<Error>> {
|
fn read_instructions(filename: &str) -> Result<HashMap<String, Vec<String>>> {
|
||||||
let mut instructions: HashMap<String, Vec<String>> = HashMap::new();
|
let mut instructions: HashMap<String, Vec<String>> = HashMap::new();
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref INSTRUCTION_REGEX: Regex = Regex::new(
|
static ref INSTRUCTION_REGEX: Regex = Regex::new(
|
||||||
@ -61,24 +38,22 @@ fn read_instructions(filename: &str) -> Result<HashMap<String, Vec<String>>, Box
|
|||||||
dependencies.push(dependency);
|
dependencies.push(dependency);
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
return Err(Box::new(MalformedInstruction {
|
return Err(From::from(
|
||||||
details: "Malformed instruction line, no fields could be found".to_string(),
|
"Malformed instruction line, no fields could be found",
|
||||||
}))
|
))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Ok(instructions)
|
Ok(instructions)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_captured_field(captures: &Captures, field: &str) -> Result<String, Box<Error>> {
|
fn get_captured_field(captures: &Captures, field: &str) -> Result<String> {
|
||||||
match captures.name(field) {
|
match captures.name(field) {
|
||||||
Some(capture) => Ok(String::from(capture.as_str())),
|
Some(capture) => Ok(String::from(capture.as_str())),
|
||||||
None => Err(Box::new(MalformedInstruction {
|
None => Err(From::from(format!(
|
||||||
details: format!(
|
"Malformed instruction line, field {} could not be found",
|
||||||
"Malformed instruction line, field {} could not be found",
|
field
|
||||||
field
|
))),
|
||||||
),
|
|
||||||
})),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user