pub mod append;
pub mod create_journal;
pub mod enumerate;
pub mod foldl;
pub mod load;
pub mod version;

use database::Database;
use endpoint::execute::append::execute_append;
use endpoint::execute::create_journal::execute_create_journal;
use endpoint::execute::enumerate::execute_enumerate;
use endpoint::execute::foldl::execute_foldl;
use endpoint::execute::load::execute_load;
use endpoint::execute::version::execute_version;
use endpoint::message::*;

use std::error;
use std::io;

pub type ExecuteError = Box<'static + error::Error>;

pub fn execute_command<R>(command: Command, database: &Database, respond: R)
                          -> Result<(), ExecuteError>
    where R: for<'a> FnMut(&'a [u8]) -> io::Result<()> {
    use self::Command::*;
    match command {
        Append{journal, entry} => execute_append(journal, entry, database),
        CreateJournal{journal} => execute_create_journal(journal, database),
        Enumerate{journal} => execute_enumerate(journal, database, respond),
        Foldl{journal, using, extension} =>
            execute_foldl(journal, using, extension, database, respond),
        Load{extension, path} => execute_load(extension, path, database),
        Version => execute_version(respond),
    }
}
