Skip to content

Commit

Permalink
Refactor command::Submit
Browse files Browse the repository at this point in the history
  • Loading branch information
hjaremko committed Oct 27, 2021
1 parent 6c947ee commit 5a64d25
Show file tree
Hide file tree
Showing 11 changed files with 497 additions and 152 deletions.
47 changes: 47 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ serde_yaml = "0.8.21"
zip = "0.5.13"
dialoguer = "0.9.0"
time = { version = "0.3.1", features = ["serde"] }
merge = "0.1.0"

[dev-dependencies]
mockall = "0.10.2"
Expand Down
2 changes: 1 addition & 1 deletion src/api/baca_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl BacaApi for BacaService {

match resp.as_str() {
"Niezalogowany jesteś" => Err(Error::LoggedOut),
"Błąd" => Err(Error::Submit),
"Błąd" => Err(Error::TaskNotActive),
_ => Ok(()),
}
}
Expand Down
32 changes: 3 additions & 29 deletions src/command/init.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,13 @@
use crate::api;
use crate::api::baca_api::BacaApi;
use crate::command::prompt::{Input, Password, Prompt};
use crate::command::Command;
use crate::update::BacaRelease;
use crate::workspace::{ConfigObject, Workspace};
use crate::{error, workspace};
use clap::ArgMatches;
#[cfg(test)]
use mockall::{automock, predicate::*};
use tracing::{debug, info};

#[cfg_attr(test, automock)]
trait Prompt {
fn interact(&self) -> error::Result<String>;
}

struct Input(&'static str);

impl Prompt for Input {
fn interact(&self) -> error::Result<String> {
Ok(dialoguer::Input::<String>::new()
.with_prompt(self.0)
.interact()?)
}
}

struct Password;

impl Prompt for Password {
fn interact(&self) -> error::Result<String> {
Ok(dialoguer::Password::new()
.with_prompt("Password")
.interact()?)
}
}

pub struct Init {
host: Option<String>,
login: Option<String>,
Expand Down Expand Up @@ -135,12 +109,12 @@ fn save_version<W: Workspace>(workspace: &W) -> error::Result<()> {

#[cfg(test)]
mod tests {
use super::*;
use crate::api;
use crate::api::baca_api::MockBacaApi;
use crate::command::prompt::MockPrompt;
use crate::workspace::{ConnectionConfig, MockWorkspace};

use super::*;

// todo: tests::utils
fn make_mock_connection_config() -> ConnectionConfig {
ConnectionConfig {
Expand Down
4 changes: 3 additions & 1 deletion src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ use crate::error;
use crate::workspace::config_editor::ConfigEditor;
use crate::workspace::{ConnectionConfig, Workspace};
use clap::ArgMatches;
use std::convert::TryFrom;

mod details;
mod init;
mod last;
mod log;
mod prompt;
mod refresh;
mod submit;
mod tasks;
Expand All @@ -39,7 +41,7 @@ where
"refresh" => Refresh::new().execute(workspace, api),
"log" => Log::from(matches).execute(workspace, api),
"tasks" => Tasks::new().execute(workspace, api),
"submit" => Submit::from(matches).execute(workspace, api),
"submit" => Submit::try_from(matches)?.execute(workspace, api),
"last" => Last::from(matches).execute(workspace, api),
"config" => {
ConfigEditor::new().edit::<W, ConnectionConfig>(workspace)?;
Expand Down
28 changes: 28 additions & 0 deletions src/command/prompt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::error;
#[cfg(test)]
use mockall::{automock, predicate::*};

#[cfg_attr(test, automock)]
pub trait Prompt {
fn interact(&self) -> error::Result<String>;
}

pub struct Input(pub &'static str);

impl Prompt for Input {
fn interact(&self) -> error::Result<String> {
Ok(dialoguer::Input::<String>::new()
.with_prompt(self.0)
.interact()?)
}
}

pub struct Password;

impl Prompt for Password {
fn interact(&self) -> error::Result<String> {
Ok(dialoguer::Password::new()
.with_prompt("Password")
.interact()?)
}
}
Loading

0 comments on commit 5a64d25

Please sign in to comment.