From bd45d26601c2e64c970f42e9212344101b2c6719 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Thu, 24 Jun 2021 12:55:25 -0300 Subject: [PATCH] Moved from dyn to full Enum --- src/commands/addaccount.rs | 71 --------------------------- src/commands/errors.rs | 16 ++++++- src/commands/mod.rs | 90 +++++++++++++++++++++++++++++++++-- src/commands/removeaccount.rs | 40 ---------------- src/main.rs | 2 +- 5 files changed, 102 insertions(+), 117 deletions(-) delete mode 100644 src/commands/addaccount.rs delete mode 100644 src/commands/removeaccount.rs diff --git a/src/commands/addaccount.rs b/src/commands/addaccount.rs deleted file mode 100644 index 208a99e..0000000 --- a/src/commands/addaccount.rs +++ /dev/null @@ -1,71 +0,0 @@ -/* - DOWNFAV - Download Favourites - Copyright (C) 2020 Julio Biason - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -use std::io; -use std::io::prelude::*; - -use elefren::helpers::cli; -use elefren::prelude::*; - -use super::errors::CommandError; -use super::Command; -use crate::config::config::Config; -use crate::config::errors::ConfigError; - -pub struct AddAccount { - name: String, -} - -impl AddAccount { - pub fn new(name: &str) -> Self { - Self { name: name.into() } - } -} - -impl Command for AddAccount { - fn execute(&self) -> Result<&str, CommandError> { - let mut server = String::new(); - - print!("Your server URL: "); - io::stdout().flush().unwrap(); - io::stdin() - .read_line(&mut server) - .expect("you need to ender your server URL"); - let registration = Registration::new(server.trim()) - .client_name("Downfav") - .build()?; - let connection = cli::authenticate(registration)?.data; - - let mut config = Config::open()?; - config.add_account(&self.name, connection); - config.save()?; - Ok("Account created") - } -} - -impl From for CommandError { - fn from(_: elefren::Error) -> CommandError { - CommandError::ConnectError - } -} - -impl From for CommandError { - fn from(_: ConfigError) -> CommandError { - CommandError::ConfigError - } -} diff --git a/src/commands/errors.rs b/src/commands/errors.rs index ab9d680..acf7483 100644 --- a/src/commands/errors.rs +++ b/src/commands/errors.rs @@ -16,6 +16,8 @@ along with this program. If not, see . */ +use crate::config::errors::ConfigError; + /// Errors for the commands #[derive(Debug)] pub enum CommandError { @@ -23,8 +25,20 @@ pub enum CommandError { ConnectError, /// Configuration file is broken - ConfigError, + ConfigError(ConfigError), /// The requested account does not exist NoSuchAccount, } + +impl From for CommandError { + fn from(_: elefren::Error) -> CommandError { + CommandError::ConnectError + } +} + +impl From for CommandError { + fn from(e: ConfigError) -> CommandError { + CommandError::ConfigError(e) + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index a67110a..c5ec7ea 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -16,12 +16,94 @@ along with this program. If not, see . */ -pub mod addaccount; pub mod errors; -pub mod removeaccount; + +use std::io; +use std::io::prelude::*; + +use elefren::helpers::cli; +use elefren::prelude::*; use self::errors::CommandError; +use crate::config::config::Config; + +type CommandResult = Result<(), CommandError>; + +/// Available Storages +pub enum StorageType { + /// Store in the filesystem, as Markdown + Markdown, + + /// Store in the filesystem, as Org-Mode + Org, + + /// Store in Joplin + Joplin, +} + +/// Available commands +pub enum Command { + /// Add a new account + AddAccount(String), + + /// Remove an account + RemoveAccount(String), + + /// Add a storage in an account + AddStorage(String, StorageType), +} + +impl Command { + pub fn add_account(name: &str) -> Self { + Command::AddAccount(name.into()) + } + + pub fn remove_account(name: &str) -> Self { + Command::RemoveAccount(name.into()) + } + + pub fn add_storage(account: &str, storage: StorageType) -> Self { + Command::AddStorage(account.into(), storage) + } + + /// Execute the command, based on its value + pub fn execute(&self) -> CommandResult { + match self { + Command::AddAccount(name) => add_account(name), + Command::RemoveAccount(name) => remove_account(name), + Command::AddStorage(account, storage) => { + add_storage(account, storage) + } + } + } +} + +fn add_account(name: &str) -> CommandResult { + let mut server = String::new(); + + print!("Your server URL: "); + io::stdout().flush().unwrap(); + io::stdin() + .read_line(&mut server) + .expect("you need to ender your server URL"); + let registration = Registration::new(server.trim()) + .client_name("Downfav") + .build()?; + let connection = cli::authenticate(registration)?.data; + + let mut config = Config::open()?; + config.add_account(&name, connection); + config.save()?; + Ok(()) +} + +fn remove_account(name: &str) -> CommandResult { + let mut config = Config::open()?; + config.remove_account(&name); + config.save()?; + Ok(()) +} -pub trait Command { - fn execute(&self) -> Result<&str, CommandError>; +fn add_storage(account: &str, storage: &StorageType) -> CommandResult { + Ok(()) } diff --git a/src/commands/removeaccount.rs b/src/commands/removeaccount.rs deleted file mode 100644 index 4fa9d9e..0000000 --- a/src/commands/removeaccount.rs +++ /dev/null @@ -1,40 +0,0 @@ -/* - DOWNFAV - Download Favourites - Copyright (C) 2020 Julio Biason - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -use super::errors::CommandError; -use super::Command; -use crate::config::config::Config; - -pub struct RemoveAccount { - name: String, -} - -impl RemoveAccount { - pub fn new(name: &str) -> Self { - Self { name: name.into() } - } -} - -impl Command for RemoveAccount { - fn execute(&self) -> Result<&str, CommandError> { - let mut config = Config::open()?; - config.remove_account(&self.name); - config.save()?; - Ok("Account removed") - } -} diff --git a/src/main.rs b/src/main.rs index 0c33508..1dc14d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,7 @@ fn main() { env_logger::init(); match args::parse() { - Ok(command) => println!("{}", command.execute().unwrap()), + Ok(command) => command.execute().unwrap(), Err(e) => println!("Error: {:?}", e), } }