use std::io::{self, IsTerminal};
use dialoguer::theme::ColorfulTheme;
use dialoguer::{Input, Password, Select};
use taskfast_client::UserProfile;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WalletMode {
Byow,
Generate,
Skip,
}
pub trait Prompter {
fn pat(&self, accounts_url: &str) -> io::Result<String>;
fn wallet_mode(&self) -> io::Result<WalletMode>;
fn wallet_address(&self) -> io::Result<String>;
fn wallet_password(&self) -> io::Result<String>;
}
pub struct DialoguerPrompter;
impl Prompter for DialoguerPrompter {
fn pat(&self, accounts_url: &str) -> io::Result<String> {
eprintln!("Get a PAT at: {accounts_url}");
Password::with_theme(&ColorfulTheme::default())
.with_prompt("Paste your PAT")
.interact()
.map_err(dialoguer_err)
}
fn wallet_mode(&self) -> io::Result<WalletMode> {
let choices = [
"Use existing wallet (BYOW)",
"Generate new wallet",
"Skip for now",
];
let idx = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Wallet setup")
.items(&choices)
.default(0)
.interact()
.map_err(dialoguer_err)?;
Ok(match idx {
0 => WalletMode::Byow,
1 => WalletMode::Generate,
_ => WalletMode::Skip,
})
}
fn wallet_address(&self) -> io::Result<String> {
Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("Wallet address")
.validate_with(|s: &String| validate_wallet_address(s))
.interact_text()
.map_err(dialoguer_err)
}
fn wallet_password(&self) -> io::Result<String> {
Password::with_theme(&ColorfulTheme::default())
.with_prompt("Keystore password")
.with_confirmation("Confirm password", "Passwords don't match")
.interact()
.map_err(dialoguer_err)
}
}
fn dialoguer_err(e: dialoguer::Error) -> io::Error {
match e {
dialoguer::Error::IO(io) => io,
}
}
fn validate_wallet_address(s: &str) -> Result<(), &'static str> {
let trimmed = s.trim();
let rest = trimmed
.strip_prefix("0x")
.ok_or("address must start with 0x")?;
if rest.len() != 40 {
return Err("address must be 0x + 40 hex characters");
}
if !rest.bytes().all(|b| b.is_ascii_hexdigit()) {
return Err("address must be hexadecimal");
}
Ok(())
}
pub fn is_interactive() -> bool {
io::stdin().is_terminal() && io::stdout().is_terminal()
}
pub fn greeting(profile: Option<&UserProfile>) -> String {
match profile {
Some(p) => format!("Hi {} <{}>", p.name, p.email),
None => "Hi there — authenticated.".to_string(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn greeting_with_profile() {
let p = UserProfile {
name: "Alice Smith".into(),
email: "alice@example.com".into(),
};
assert_eq!(greeting(Some(&p)), "Hi Alice Smith <alice@example.com>");
}
#[test]
fn greeting_without_profile_is_neutral() {
assert_eq!(greeting(None), "Hi there — authenticated.");
}
#[test]
fn valid_wallet_address_accepted() {
assert!(validate_wallet_address("0x0123456789abcdef0123456789ABCDEF01234567").is_ok());
}
#[test]
fn wallet_address_missing_0x_rejected() {
assert!(validate_wallet_address("0123456789abcdef0123456789ABCDEF01234567").is_err());
}
#[test]
fn wallet_address_wrong_length_rejected() {
assert!(validate_wallet_address("0xabc").is_err());
}
#[test]
fn wallet_address_non_hex_rejected() {
assert!(validate_wallet_address("0xGGGG456789abcdef0123456789ABCDEF01234567").is_err());
}
#[test]
fn wallet_address_trims_whitespace() {
assert!(validate_wallet_address(" 0x0123456789abcdef0123456789ABCDEF01234567\n").is_ok());
}
}