use zenquirer::{
CrosstermBackend, QuestionnaireBuilder, QuestionnaireExt, ValidationRule, Validator,
};
fn main() {
let result = zenquirer::smol::block_on(async { run().await });
if let Err(e) = result {
eprintln!("Error: {e}");
}
}
async fn run() -> Result<(), zenquirer::Error> {
let backend = CrosstermBackend::new()?;
backend.clear_screen()?;
let questionnaire = QuestionnaireBuilder::new()
.header("Validator Combinators Demo")
.input("contact", "Email or phone number")
.validator(Validator::any_of(vec![
#[cfg(feature = "validators-regex")]
ValidationRule::EmailRegex,
#[cfg(feature = "validators-regex")]
ValidationRule::Regex(r"^\+?1?\d{10,14}$".to_string()),
]).message("Must be a valid email address OR a phone number (10-14 digits)"))
.hint("Enter email like user@example.com or phone like +15551234567")
.input("username", "Choose a username")
.validator(Validator::new()
.rule(ValidationRule::MinLength(3))
.rule(ValidationRule::MaxLength(20))
.rule(ValidationRule::AlphanumericWithUnderscore)
.rule(ValidationRule::Lowercase)
.rule(ValidationRule::Not(Box::new(
ValidationRule::StartsWith("admin".to_string())
)))
.message("Username: 3-20 chars, lowercase alphanumeric/underscore, cannot start with 'admin'"))
.hint("e.g., john_doe or user123")
.input("password", "Create a strong password")
.validator(Validator::all_of(vec![
ValidationRule::MinLength(12),
#[cfg(feature = "validators-regex")]
ValidationRule::Regex(r".*[A-Z].*".to_string()),
#[cfg(feature = "validators-regex")]
ValidationRule::Regex(r".*[a-z].*".to_string()),
#[cfg(feature = "validators-regex")]
ValidationRule::Regex(r".*[0-9].*".to_string()),
#[cfg(feature = "validators-regex")]
ValidationRule::Regex(r".*[^a-zA-Z0-9].*".to_string()),
]).message("Password must be 12+ chars with uppercase, lowercase, digit, and special character"))
.hint("Fails on first unmet requirement, fix iteratively")
.input("identifier", "Company ID")
.validator(Validator::any_of(vec![
#[cfg(feature = "validators-regex")]
ValidationRule::AllOf(vec![
ValidationRule::EmailRegex,
ValidationRule::EndsWith("@company.com".to_string()),
]),
ValidationRule::AllOf(vec![
ValidationRule::StartsWith("admin_".to_string()),
ValidationRule::AlphanumericWithUnderscore,
ValidationRule::MinLength(6),
]),
]).message("Must be company email (@company.com) OR admin username (admin_XXX, 6+ chars)"))
.hint("e.g., john@company.com or admin_ops")
.input("project_name", "Project name")
.validator(Validator::new()
.rule(ValidationRule::MinLength(3))
.rule(ValidationRule::MaxLength(50))
.rule(ValidationRule::AlphanumericWithUnderscore)
.rule(ValidationRule::Not(Box::new(
ValidationRule::Contains("test".to_string())
)))
.rule(ValidationRule::Not(Box::new(
ValidationRule::Contains("tmp".to_string())
)))
.message("Project name: 3-50 chars, alphanumeric/underscore, no 'test' or 'tmp'"))
.hint("e.g., my_awesome_project")
.footer("Combinator validators demo - shows AnyOf, AllOf, and Not in action")
.build();
let _ = questionnaire.ask_all_async(backend, None).await;
Ok(())
}