mod common;
use zenquirer::{CrosstermBackend, QuestionnaireBuilder, QuestionnaireExt, 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 tomorrow = {
use std::time::{Duration, SystemTime};
let now = SystemTime::now();
let tomorrow_time = now + Duration::from_secs(86400);
let days_since_epoch = tomorrow_time
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_else(|_| Duration::from_secs(0))
.as_secs()
/ 86400;
let year = 1970 + (days_since_epoch / 365) as u32;
let day_of_year = (days_since_epoch % 365) as u32;
let month = (day_of_year / 30).min(11) + 1;
let day = (day_of_year % 30) + 1;
format!("{year:04}-{month:02}-{day:02}")
};
let questionnaire = QuestionnaireBuilder::new()
.header("Comprehensive Validator Demo - Press Enter to use defaults")
.input("email", "Enter your email address")
.validator(Validator::email())
.default("user@example.com")
.hint("Must be a valid email address (uses regex validation)")
.input("url", "Enter a website URL")
.validator(Validator::url())
.default("https://example.com")
.hint("Must be a valid URL starting with http:// or https://")
.input("number", "Enter a decimal number")
.validator(Validator::numeric())
.default("42.5")
.hint("Must be a valid number (integer or decimal)")
.input("integer", "Enter an integer")
.validator(Validator::integer())
.default("100")
.hint("Must be a whole number with no decimals")
.input("lowercase", "Enter text in lowercase")
.validator(Validator::lowercase())
.default("hello world")
.hint("All characters must be lowercase")
.input("uppercase", "Enter text in uppercase")
.validator(Validator::uppercase())
.default("HELLO WORLD")
.hint("All characters must be uppercase")
.input("username", "Choose a username")
.validator(
Validator::alphanumeric_underscore()
.rule(zenquirer::types::ValidationRule::MinLength(3))
.rule(zenquirer::types::ValidationRule::MaxLength(20)),
)
.default("cool_user_123")
.hint("Only letters, numbers, and underscores (3-20 chars)")
.input("hex_color", "Enter a hex color code")
.validator(
Validator::hex()
.rule(zenquirer::types::ValidationRule::MinLength(6))
.rule(zenquirer::types::ValidationRule::MaxLength(6)),
)
.default("FF5733")
.hint("6-digit hexadecimal color code (without #)")
.input("ip_address", "Enter an IP address")
.validator(Validator::ip_address())
.default("192.168.1.1")
.hint("Valid IPv4 or IPv6 address")
.input("port", "Enter a port number")
.validator(Validator::port())
.default("8080")
.hint("Must be between 1 and 65535")
.input("hostname", "Enter a hostname")
.validator(Validator::hostname())
.default("api.example.com")
.hint("Valid domain name or hostname")
.input("mac_address", "Enter a MAC address")
.validator(Validator::mac_address())
.default("00:1B:63:84:45:E6")
.hint("Format: XX:XX:XX:XX:XX:XX or XX-XX-XX-XX-XX-XX")
.input("age", "Enter your age")
.validator(
Validator::numeric().rule(zenquirer::types::ValidationRule::Range {
min: Some(0.0),
max: Some(150.0),
}),
)
.default("25")
.hint("Must be between 0 and 150")
.input("discount_code", "Enter discount code")
.validator(Validator::equals("SAVE20"))
.default("SAVE20")
.hint("Must be exactly 'SAVE20'")
.input("cargo_file", "Enter path to Cargo.toml")
.validator(Validator::file_exists())
.default("Cargo.toml")
.hint("File must exist in the filesystem")
.input("source_dir", "Enter path to src directory")
.validator(Validator::directory_exists())
.default("src")
.hint("Directory must exist")
.input("rust_file", "Enter path to a Rust file")
.validator(Validator::extension(["rs"]))
.default("src/lib.rs")
.hint("Must have .rs extension")
.input("future_date", "Enter a future date")
.validator(Validator::future_date())
.default(&tomorrow)
.hint("Date must be in the future (YYYY-MM-DD)")
.input("weekday", "Enter a weekday date")
.validator(Validator::weekday_only())
.default("2025-01-13")
.hint("Must be Monday-Friday (Jan 13, 2025 is a Monday)")
.input("date_after", "Enter a date after 2025-01-01")
.validator(Validator::date_after("2025-01-01"))
.default("2025-06-15")
.hint("Must be after January 1, 2025")
.password("strong_password", "Create a strong password")
.validator(Validator::password_strong())
.hint("Min 8 chars with uppercase, lowercase, digit, and special char")
.input("no_repeat", "Enter text without repeating characters")
.validator(Validator::max_consecutive_chars(2))
.default("abcdef123")
.hint("No more than 2 consecutive identical characters")
.input("json", "Enter valid JSON")
.validator(Validator::json())
.default(r#"{"key":"value","number":42}"#)
.hint("Must be valid JSON format")
.input("uuid", "Enter a UUID")
.validator(Validator::uuid())
.default("550e8400-e29b-41d4-a716-446655440000")
.hint("Standard UUID format (8-4-4-4-12 hex digits)")
.input("base64", "Enter base64 encoded text")
.validator(Validator::base64())
.default("SGVsbG8gV29ybGQ=")
.hint("Valid base64 encoding")
.input("contains_rust", "Enter text containing 'Rust'")
.validator(Validator::contains("Rust"))
.default("I love Rust programming!")
.hint("Must contain the word 'Rust'")
.input("starts_http", "Enter text starting with 'http'")
.validator(Validator::starts_with("http"))
.default("https://docs.rs")
.hint("Must start with 'http'")
.input("ends_rs", "Enter text ending with '.rs'")
.validator(Validator::ends_with(".rs"))
.default("main.rs")
.hint("Must end with '.rs'")
.input("no_spaces", "Enter text without spaces")
.validator(Validator::no_whitespace())
.default("no_spaces_here")
.hint("Must not contain any whitespace")
.input("ascii_only", "Enter ASCII text only")
.validator(Validator::ascii())
.default("Hello ASCII!")
.hint("Only ASCII characters allowed")
.input("short_text", "Enter 3-5 words")
.validator(Validator::word_count(Some(3), Some(5)))
.default("This is four words")
.hint("Must contain between 3 and 5 words")
.select("language", "Choose your favorite programming language")
.options(["Python", "JavaScript", "Rust", "Go", "Java"])
.validator(Validator::one_of(["Rust"]).message("You must select Rust!"))
.hint("Try selecting anything other than Rust to see validation error")
.footer("All validators demonstrated! Defaults provided for easy testing.")
.build();
let _ = questionnaire.ask_all_async(backend, None).await;
Ok(())
}