Browse Source

Do the parsing for adding storage

master
Julio Biason 3 years ago
parent
commit
7137ff20ad
  1. 8
      src/args/errors.rs
  2. 11
      src/args/mod.rs
  3. 3
      src/commands/errors.rs
  4. 14
      src/commands/mod.rs

8
src/args/errors.rs

@ -16,9 +16,17 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::commands::errors::CommandError;
/// Parsing errors
#[derive(Debug)]
pub enum ParsingError {
/// The command is not recognized
UnknownCommand,
}
impl From<CommandError> for ParsingError {
fn from(_: CommandError) -> Self {
Self::UnknownCommand
}
}

11
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<Command, ParsingError> {
@ -86,6 +89,14 @@ pub fn parse() -> Result<Command, ParsingError> {
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),
}
}

3
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<elefren::Error> for CommandError {

14
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<Self, Self::Error> {
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

Loading…
Cancel
Save