Skip to content

Commit

Permalink
refactor: Move confirmation into prompt.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-way committed May 7, 2024
1 parent e0c4dd4 commit 245408f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
9 changes: 8 additions & 1 deletion crates/knope/src/prompt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Display;

use inquire::{InquireError, Password, Select};
use inquire::{Confirm, InquireError, Password, Select};
use miette::{Diagnostic, Result};

pub(crate) fn select<T: Display>(items: Vec<T>, prompt: &str) -> Result<T, Error> {
Expand All @@ -15,6 +15,13 @@ pub(crate) fn get_input(prompt: &str) -> Result<String, Error> {
.map_err(Error)
}

pub(crate) fn confirm(prompt: &str) -> Result<bool, Error> {
Confirm::new(prompt)
.with_default(true)
.prompt()
.map_err(Error)
}

#[derive(Debug, Diagnostic, thiserror::Error)]
#[error("Failed to get user input")]
#[diagnostic(
Expand Down
17 changes: 7 additions & 10 deletions crates/knope/src/step/confirm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use inquire::Confirm;
use miette::Diagnostic;

use crate::{app_config, RunType};
use crate::{prompt, RunType};

pub(crate) fn confirm(
mut run_type: RunType,
Expand All @@ -21,24 +20,22 @@ pub(crate) fn confirm(
return Ok(run_type);
}

let confirmation = Confirm::new(message).with_default(true).prompt();
let confirmation = prompt::confirm(message)?;

match confirmation {
Ok(true) => Ok(run_type),
Ok(false) => Err(Error::Confirm),
Err(err) => Err(Error::Prompt(err)),
if confirmation {
Ok(run_type)
} else {
Err(Error::Confirm)
}
}

#[derive(Debug, Diagnostic, thiserror::Error)]
pub(crate) enum Error {
#[error("User did not confirm")]
Confirm,
#[error(transparent)]
Prompt(#[from] inquire::InquireError),
#[error("Unable to write to stdout: {0}")]
Stdout(#[from] std::io::Error),
#[error(transparent)]
#[diagnostic(transparent)]
AppConfig(#[from] app_config::Error),
Prompt(#[from] prompt::Error),
}

0 comments on commit 245408f

Please sign in to comment.