Skip to content

Commit

Permalink
Convert errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tjpalmer committed Dec 7, 2018
1 parent 45e4d9f commit 5db07df
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions 2018/01/rust/repeat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashSet;
use std::io::{self, BufRead, BufReader};
use std::fs::File;

fn main() -> io::Result<()> {
fn main() -> Try<()> {
let values = read_values()?;
let mut prevs = HashSet::new();
let mut total = 0;
Expand All @@ -19,11 +19,30 @@ fn main() -> io::Result<()> {
Ok(())
}

fn read_values() -> io::Result<Vec<i32>> {
fn read_values() -> Try<Vec<i32>> {
let file = File::open("../../input/input.txt")?;
let mut values = Vec::new();
for line in BufReader::new(file).lines() {
values.push(line.unwrap().parse::<i32>().unwrap());
values.push(line?.parse::<i32>()?);
}
Ok(values)
}

#[derive(Debug)]
pub struct Err {
pub message: String,
}

type Try<Value> = Result<Value, Err>;

impl From<io::Error> for Err {
fn from(_: io::Error) -> Err {
Err{message: "io error".to_string()}
}
}

impl From<std::num::ParseIntError> for Err {
fn from(_: std::num::ParseIntError) -> Err {
Err{message: "parse int error".to_string()}
}
}

0 comments on commit 5db07df

Please sign in to comment.