summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Cargo.toml11
-rw-r--r--lib/src/error.rs52
-rw-r--r--lib/src/lib.rs86
-rw-r--r--lib/src/prelude.rs4
4 files changed, 153 insertions, 0 deletions
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 <ireneista@gmail.com>"]
+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<std::io::Error> for Error {
+  fn from(e: std::io::Error) -> Error {
+    Error::IO(e)
+  }
+}
+
+impl From<std::num::ParseIntError> for Error {
+  fn from(_: std::num::ParseIntError) -> Error {
+    Error::Parse
+  }
+}
+
+impl From<lalrpop_util::ParseError<usize, lalrpop_util::lexer::Token<'_>,
+  &str>> for Error
+{
+  fn from(_: lalrpop_util::ParseError<usize, lalrpop_util::lexer::Token<'_>,
+              &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<Vec<String>> {
+  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<String>) -> Vec<Vec<String>> {
+  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<Vec<i64>> {
+  let file = File::open(filename)?;
+  let mut reader = BufReader::new(file);
+  let mut buffer = String::new();
+
+  let mut input: Vec<i64> = Vec::new();
+  loop {
+    reader.read_line(&mut buffer)?;
+    if buffer.len() == 0 {
+      break;
+    }
+
+    let item = buffer.trim().parse::<i64>()?;
+
+    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<T> = std::result::Result<T, Error>;
+