From 7137ff20ad0e27d241d3e0f8421e9374848a1f2b Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Fri, 25 Jun 2021 13:18:20 -0300 Subject: [PATCH] Do the parsing for adding storage --- src/args/errors.rs | 8 ++++++++ src/args/mod.rs | 11 +++++++++++ src/commands/errors.rs | 3 +++ src/commands/mod.rs | 14 ++++++++++++++ 4 files changed, 36 insertions(+) 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