refactor to a library, add integration tests

This commit is contained in:
Dustin J. Mitchell 2018-11-12 18:25:47 -05:00
parent 9f310c76bd
commit d0744d5178
8 changed files with 56 additions and 59 deletions

18
src/lib.rs Normal file
View file

@ -0,0 +1,18 @@
#![recursion_limit = "1024"]
extern crate chrono;
extern crate uuid;
#[macro_use]
extern crate error_chain;
mod tdb2;
mod task;
mod errors;
use std::io::BufRead;
pub use task::*;
pub use errors::*;
pub fn parse(filename: &str, reader: impl BufRead) -> Result<Vec<Task>> {
Ok(tdb2::parse(filename, reader)?)
}

View file

@ -1,47 +0,0 @@
#![recursion_limit = "1024"]
extern crate chrono;
extern crate uuid;
#[macro_use]
extern crate error_chain;
mod tdb2;
mod task;
mod errors;
use tdb2::parse;
use std::io::stdin;
use errors::*;
fn main() {
if let Err(ref e) = run() {
use std::io::Write;
let stderr = &mut ::std::io::stderr();
let errmsg = "Error writing to stderr";
writeln!(stderr, "error: {}", e).expect(errmsg);
for e in e.iter().skip(1) {
writeln!(stderr, "caused by: {}", e).expect(errmsg);
}
// The backtrace is not always generated. Try to run this example
// with `RUST_BACKTRACE=1`.
if let Some(backtrace) = e.backtrace() {
writeln!(stderr, "backtrace: {:?}", backtrace).expect(errmsg);
}
::std::process::exit(1);
}
}
fn run() -> Result<()> {
let input = stdin();
parse("<stdin>".to_string(), input.lock())?
.iter()
.for_each(|t| {
println!("{:?}", t);
});
Ok(())
}

View file

@ -97,6 +97,7 @@ impl TaskBuilder {
const ANNOTATION_PREFIX: &str = "annotation_";
if name.starts_with(ANNOTATION_PREFIX) {
let entry = parse_timestamp(&name[ANNOTATION_PREFIX.len()..]).unwrap();
// TODO: sort by entry time
self.annotations.push(Annotation {
entry,
description: value.to_string(),

View file

@ -10,11 +10,11 @@ use task::Task;
use self::ff4::parse_ff4;
use self::errors::*;
pub(super) fn parse(filename: String, reader: impl BufRead) -> Result<Vec<Task>> {
pub(crate) fn parse(filename: &str, reader: impl BufRead) -> Result<Vec<Task>> {
let mut tasks = vec![];
for (i, line) in reader.lines().enumerate() {
tasks.push(parse_ff4(&line?).chain_err(|| {
ErrorKind::ParseError(filename.clone(), i as u64 + 1)
ErrorKind::ParseError(filename.to_string(), i as u64 + 1)
})?);
}
Ok(tasks)