Lines
85.71 %
Functions
81.25 %
Branches
100 %
use server::command::{Argument, CmdError, CmdResult, config::SelectColumn};
use std::collections::HashMap;
use std::fmt::Debug;
use std::future::Future;
use std::pin::Pin;
use thiserror::Error;
pub trait CliRunnable: Debug + Send {
fn run<'a>(
&'a self,
args: &'a HashMap<&str, &Argument>,
) -> Pin<Box<dyn Future<Output = Result<Option<CmdResult>, CommandError>> + Send + 'a>>;
}
#[derive(Debug)]
pub struct CommandNode {
pub name: String,
pub is_leaf: bool,
pub subcommands: Vec<CommandNode>,
#[derive(Debug, Error)]
pub enum CommandError {
#[error("{0}")]
Command(String),
#[error("Arguments error: {0}")]
Argument(String),
#[error("Execution: {0}")]
Execution(#[from] CmdError),
#[error("{}", .0.join("\n"))]
FormInvalid(Vec<String>),
pub trait CliCommand: Debug + Send {
fn node() -> CommandNode;
pub struct CliGetConfig;
impl CliCommand for CliGetConfig {
fn node() -> CommandNode {
CommandNode {
name: "get".to_string(),
is_leaf: true,
subcommands: vec![],
pub struct CliSetConfig;
impl CliCommand for CliSetConfig {
name: "set".to_string(),
pub struct CliVersion;
impl CliCommand for CliVersion {
name: "version".to_string(),
pub struct CliSelectColumn;
impl CliRunnable for CliSelectColumn {
) -> Pin<Box<dyn Future<Output = Result<Option<CmdResult>, CommandError>> + Send + 'a>> {
Box::pin(async move {
match (args.get("field"), args.get("table")) {
(Some(Argument::String(field)), Some(Argument::String(table))) => {
Ok(SelectColumn::new()
.field(field.clone())
.table(table.clone())
.run()
.await?)
_ => Err(CommandError::Argument(
"No column or table provided".to_string(),
)),
})
pub struct CliCommodityCreate;
impl CliCommand for CliCommodityCreate {
name: "create".to_string(),
pub struct CliCommodityList;
impl CliCommand for CliCommodityList {
name: "list".to_string(),
pub struct CliAccountCreate;
impl CliCommand for CliAccountCreate {
pub struct CliAccountList;
impl CliCommand for CliAccountList {
pub struct CliTransactionCreate;
impl CliCommand for CliTransactionCreate {
pub struct CliTransactionList;
impl CliCommand for CliTransactionList {
pub struct CliAccountBalance;
impl CliCommand for CliAccountBalance {
name: "balance".to_string(),
pub struct CliListDataFindings;
impl CliRunnable for CliListDataFindings {
let mut cmd = server::command::data_findings::ListDataFindings::new();
if let Some(Argument::String(kind)) = args.get("kind") {
cmd = cmd.kind(kind.clone());
Ok(cmd.run().await?)
pub struct CliSshKeyRemove;
impl CliCommand for CliSshKeyRemove {
name: "remove".to_string(),
#[cfg(test)]
mod tests {
use super::*;
fn block_on<F: Future>(f: F) -> F::Output {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("runtime")
.block_on(f)
#[test]
fn select_column_rejects_missing_table() {
let field = Argument::String("foo".to_string());
let mut args: HashMap<&str, &Argument> = HashMap::new();
args.insert("field", &field);
let err = block_on(CliSelectColumn.run(&args)).expect_err("missing table should error");
assert!(matches!(err, CommandError::Argument(_)));