taskforge 0.2.0

Task management shared functions and structures for the taskforge family of tools.
Documentation
// Copyright 2018 Mathew Robinson <chasinglogic@gmail.com>. All rights reserved. Use of this source code is
// governed by the Apache-2.0 license that can be found in the LICENSE file.

//! Definition of the Error type and List trait used to store and
//! retrieve tasks

#[cfg(test)]
#[macro_use]
mod test_utils;

use query::ast::AST;
use std::fmt;
use std::io::Error as IOError;
use std::io::ErrorKind;
use task::{Note, Task};

/// Error is returned by Lists who convert specific errors into
/// relevant Human readable errors using this type.
#[derive(Debug)]
pub enum Error {
    NotFound,
    IO(IOError),
    BadRequest(String),
}

impl From<IOError> for Error {
    fn from(e: IOError) -> Error {
        Error::IO(e)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::IO(s) => write!(f, "IO: {}", s),
            Error::BadRequest(s) => write!(f, "BadRequest: {}", s),
            Error::NotFound => write!(f, "No task matching that query found."),
        }
    }
}

impl Error {
    pub fn into_io_err(self) -> IOError {
        match self {
            Error::IO(e) => e,
            Error::NotFound => {
                IOError::new(ErrorKind::NotFound, "No task matching that query found.")
            }
            Error::BadRequest(s) => IOError::new(ErrorKind::Other, s),
        }
    }
}

/// List is implemented by any struct that can maintain tasks
pub trait List {
    /// Add a task to the List
    fn add(&mut self, task: Task) -> Result<(), Error>;

    /// Add multiple tasks to the List, should be more efficient resource
    /// utilization.
    fn add_multiple(&mut self, task: Vec<Task>) -> Result<(), Error>;

    /// Find a task by ID
    fn find_by_id(&mut self, id: String) -> Result<Task, Error>;

    /// Return the current task, meaning the oldest uncompleted task in
    /// the List
    fn current(&mut self) -> Result<Task, Error>;

    /// Return the contexts used in this list
    fn contexts(&mut self) -> Result<Vec<String>, Error>;

    /// Complete a task by id
    fn complete(&mut self, id: String) -> Result<(), Error>;

    /// Update a task in the list, finding the original by the ID of
    /// the given task
    fn update(&mut self, task: Task) -> Result<(), Error>;

    /// Add note to a task by ID
    fn add_note(&mut self, id: String, note: Note) -> Result<(), Error>;

    /// Interpret the ast and search for tasks which match
    fn search(&mut self, ast: AST) -> Result<Vec<Task>, Error>;
}

pub mod sqlite;
pub use self::sqlite::SQLiteList;