Refactor: Extract confirm into a new mod

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-10 12:43:01 +10:00
parent f53680c497
commit c1809d41fa
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
2 changed files with 26 additions and 18 deletions

View file

@ -1,10 +1,11 @@
use std::{
borrow::Cow,
io::{stderr, stdin, Write},
path::{Path, PathBuf},
};
use cargo_toml::Manifest;
use log::{debug, info};
use log::debug;
use reqwest::Method;
use serde::Serialize;
use tinytemplate::TinyTemplate;
@ -18,6 +19,9 @@ pub use async_extracter::extract_archive_stream;
mod auto_abort_join_handle;
pub use auto_abort_join_handle::AutoAbortJoinHandle;
mod confirm;
pub use confirm::confirm;
mod extracter;
mod readable_rx;
@ -129,23 +133,6 @@ pub fn get_install_path<P: AsRef<Path>>(install_path: Option<P>) -> Option<PathB
dir
}
pub fn confirm() -> Result<(), BinstallError> {
loop {
info!("Do you wish to continue? yes/[no]");
eprint!("? ");
stderr().flush().ok();
let mut input = String::new();
stdin().read_line(&mut input).unwrap();
match input.as_str().trim() {
"yes" | "y" | "YES" | "Y" => break Ok(()),
"no" | "n" | "NO" | "N" | "" => break Err(BinstallError::UserAbort),
_ => continue,
}
}
}
pub trait Template: Serialize {
fn render(&self, template: &str) -> Result<String, BinstallError>
where

21
src/helpers/confirm.rs Normal file
View file

@ -0,0 +1,21 @@
use log::info;
use std::io::{stderr, stdin, Write};
use crate::BinstallError;
pub fn confirm() -> Result<(), BinstallError> {
loop {
info!("Do you wish to continue? yes/[no]");
eprint!("? ");
stderr().flush().ok();
let mut input = String::new();
stdin().read_line(&mut input).unwrap();
match input.as_str().trim() {
"yes" | "y" | "YES" | "Y" => break Ok(()),
"no" | "n" | "NO" | "N" | "" => break Err(BinstallError::UserAbort),
_ => continue,
}
}
}