From 0fc36ff992d4ea1a7fe5240da8c5c63f7c602a6e Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Tue, 30 Nov 2021 18:44:19 -0800 Subject: Initial, based on last year --- lib/Cargo.toml | 11 +++++++ lib/src/error.rs | 52 +++++++++++++++++++++++++++++++++ lib/src/lib.rs | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/src/prelude.rs | 4 +++ 4 files changed, 153 insertions(+) create mode 100644 lib/Cargo.toml create mode 100644 lib/src/error.rs create mode 100644 lib/src/lib.rs create mode 100644 lib/src/prelude.rs (limited to 'lib') diff --git a/lib/Cargo.toml b/lib/Cargo.toml new file mode 100644 index 0000000..7785a81 --- /dev/null +++ b/lib/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "advent_lib" +version = "0.1.0" +authors = ["Irene Knapp "] +edition = "2018" + +[dependencies] +lalrpop-util = "0.19" + +[build-dependencies] +lalrpop = { version = "0.19", features = [ "lexer" ] } diff --git a/lib/src/error.rs b/lib/src/error.rs new file mode 100644 index 0000000..4f594b7 --- /dev/null +++ b/lib/src/error.rs @@ -0,0 +1,52 @@ +#[derive(Debug)] +pub enum Error { + IO(std::io::Error), + Parse, +} + +impl std::error::Error for Error { } + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::IO(e) => e.fmt(f), + Error::Parse => f.write_str("Parse error"), + } + } +} + +impl std::cmp::PartialEq for Error { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (Error::IO(_), Error::IO(_)) => + false, + (Error::Parse, Error::Parse) => + true, + _ => + false, + } + } +} + +impl From for Error { + fn from(e: std::io::Error) -> Error { + Error::IO(e) + } +} + +impl From for Error { + fn from(_: std::num::ParseIntError) -> Error { + Error::Parse + } +} + +impl From, + &str>> for Error +{ + fn from(_: lalrpop_util::ParseError, + &str>) -> Error + { + Error::Parse + } +} + diff --git a/lib/src/lib.rs b/lib/src/lib.rs new file mode 100644 index 0000000..8798359 --- /dev/null +++ b/lib/src/lib.rs @@ -0,0 +1,86 @@ +pub mod error; +pub mod prelude; + +pub use crate::prelude::Result; + +use std::fs::File; +use std::io::BufReader; +use std::io::prelude::*; + + +pub fn greeting() -> Result<()> { + println!("Hello, Irenes!"); + + Ok(()) +} + +pub fn read_lines_file(filename: &str) -> Result> { + let file = File::open(filename)?; + let mut reader = BufReader::new(file); + let mut buffer = String::new(); + + let mut input = Vec::new(); + loop { + reader.read_line(&mut buffer)?; + if buffer.len() == 0 { + break; + } + + let mut line_copy = String::new(); + match buffer.strip_suffix("\n") { + Some(stripped) => { + line_copy.push_str(stripped); + } + None => { + line_copy.push_str(&buffer); + } + } + input.push(line_copy); + + buffer.clear(); + } + + Ok(input) +} + +pub fn group_lines_by_blanks(lines: Vec) -> Vec> { + let mut all_groups = Vec::new(); + let mut current_group = Vec::new(); + + for line in lines { + if line.len() == 0 { + all_groups.push(current_group); + current_group = Vec::new(); + } else { + current_group.push(line); + } + } + + if current_group.len() > 0 { + all_groups.push(current_group); + } + + all_groups +} + +pub fn read_int_file(filename: &str) -> Result> { + let file = File::open(filename)?; + let mut reader = BufReader::new(file); + let mut buffer = String::new(); + + let mut input: Vec = Vec::new(); + loop { + reader.read_line(&mut buffer)?; + if buffer.len() == 0 { + break; + } + + let item = buffer.trim().parse::()?; + + buffer.clear(); + + input.push(item); + } + + Ok(input) +} diff --git a/lib/src/prelude.rs b/lib/src/prelude.rs new file mode 100644 index 0000000..a4e81eb --- /dev/null +++ b/lib/src/prelude.rs @@ -0,0 +1,4 @@ +use crate::error::Error; + +pub type Result = std::result::Result; + -- cgit 1.4.1