Browse Source

Moved from dyn to full Enum

master
Julio Biason 3 years ago
parent
commit
bd45d26601
  1. 71
      src/commands/addaccount.rs
  2. 16
      src/commands/errors.rs
  3. 90
      src/commands/mod.rs
  4. 40
      src/commands/removeaccount.rs
  5. 2
      src/main.rs

71
src/commands/addaccount.rs

@ -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 <https://www.gnu.org/licenses/>.
*/
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<elefren::Error> for CommandError {
fn from(_: elefren::Error) -> CommandError {
CommandError::ConnectError
}
}
impl From<ConfigError> for CommandError {
fn from(_: ConfigError) -> CommandError {
CommandError::ConfigError
}
}

16
src/commands/errors.rs

@ -16,6 +16,8 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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<elefren::Error> for CommandError {
fn from(_: elefren::Error) -> CommandError {
CommandError::ConnectError
}
}
impl From<ConfigError> for CommandError {
fn from(e: ConfigError) -> CommandError {
CommandError::ConfigError(e)
}
}

90
src/commands/mod.rs

@ -16,12 +16,94 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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(())
}

40
src/commands/removeaccount.rs

@ -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 <https://www.gnu.org/licenses/>.
*/
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")
}
}

2
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),
}
}

Loading…
Cancel
Save