diff --git a/src/args/errors.rs b/src/args/errors.rs index 255b6d2..dfa27c4 100644 --- a/src/args/errors.rs +++ b/src/args/errors.rs @@ -16,9 +16,17 @@ along with this program. If not, see . */ +use crate::commands::errors::CommandError; + /// Parsing errors #[derive(Debug)] pub enum ParsingError { /// The command is not recognized UnknownCommand, } + +impl From for ParsingError { + fn from(_: CommandError) -> Self { + Self::UnknownCommand + } +} diff --git a/src/args/mod.rs b/src/args/mod.rs index 2e14220..51a830d 100644 --- a/src/args/mod.rs +++ b/src/args/mod.rs @@ -18,12 +18,15 @@ mod errors; +use std::convert::TryFrom; + use clap::App; use clap::Arg; use clap::SubCommand; use self::errors::ParsingError; use super::commands::Command; +use super::commands::StorageType; /// Parse the command line, returning the necessary command. pub fn parse() -> Result { @@ -86,6 +89,14 @@ pub fn parse() -> Result { match matches.subcommand() { ("create", _) => Ok(Command::add_account(account_name.into())), ("remove", _) => Ok(Command::remove_account(account_name.into())), + ("storage", Some(args)) => { + let storage = + args.subcommand_name().ok_or(ParsingError::UnknownCommand)?; + Ok(Command::add_storage( + account_name.into(), + StorageType::try_from(storage)?, + )) + } _ => Err(ParsingError::UnknownCommand), } } diff --git a/src/commands/errors.rs b/src/commands/errors.rs index acf7483..fb398c6 100644 --- a/src/commands/errors.rs +++ b/src/commands/errors.rs @@ -29,6 +29,9 @@ pub enum CommandError { /// The requested account does not exist NoSuchAccount, + + /// The storage type requested does not exist + NoSuchStorage, } impl From for CommandError { diff --git a/src/commands/mod.rs b/src/commands/mod.rs index c5ec7ea..945d974 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -18,6 +18,7 @@ pub mod errors; +use std::convert::TryFrom; use std::io; use std::io::prelude::*; @@ -41,6 +42,19 @@ pub enum StorageType { Joplin, } +impl TryFrom<&str> for StorageType { + type Error = errors::CommandError; + + fn try_from(s: &str) -> Result { + match s { + "markdown" => Ok(StorageType::Markdown), + "org" => Ok(StorageType::Org), + "joplin" => Ok(StorageType::Joplin), + _ => Err(Self::Error::NoSuchStorage), + } + } +} + /// Available commands pub enum Command { /// Add a new account