use anyhow::{Context, Result};
use dialoguer::theme::ColorfulTheme;
use dialoguer::{Confirm, Input, MultiSelect};
pub const OPTIONAL_FEATURES: &[(&str, &str)] = &[
(
"db",
"Embedded key-value store (redb) - pure-Rust, no C toolchain needed at build time; \
separate from the SQL database, for app-local structured data like feature flags or \
offline caches",
),
(
"permissions",
"Roles & permissions (spatie/laravel-permission equivalent, plus @can/@role \
template directives)",
),
(
"reverb",
"WebSocket pub/sub broadcasting - arbitrary JSON events to subscribed clients",
),
(
"sanctum",
"API bearer-token authentication for non-browser clients",
),
(
"shield",
"Resource-scoped permission bundles (view/create/update/delete per resource) on top \
of `permissions`, filament-shield-inspired - implies `permissions`",
),
("sitemap", "XML sitemap builder"),
(
"socialite",
"OAuth \"Sign in with GitHub/Google\" social login",
),
];
pub struct Answers {
pub path: String,
pub auth: bool,
pub tauri: bool,
pub features: Vec<String>,
}
pub fn run() -> Result<Answers> {
let theme = ColorfulTheme::default();
println!(
r"
--------------------------------------------
__ _
/ / __ _ _ __ _ _ ___| |_
/ / / _` | '__| | | / __| __|
/ /__| (_| | | | |_| \__ \ |_
\____/\__,_|_| \___,_|___/\__|
By Wallaby Designs - wallabydesigns.com
--------------------------------------------
"
);
println!("Let's create a new Larust application.\n");
let path: String = Input::with_theme(&theme)
.with_prompt("Project directory")
.default("my-app".to_string())
.interact_text()
.context("reading project directory")?;
let auth = Confirm::with_theme(&theme)
.with_prompt("Include session-based authentication (User model, register/login/logout)?")
.default(false)
.interact()
.context("reading authentication choice")?;
let feature_labels: Vec<String> = OPTIONAL_FEATURES
.iter()
.map(|(name, desc)| format!("{name} - {desc}"))
.collect();
let selected_indices = MultiSelect::with_theme(&theme)
.with_prompt("Optional features (space to toggle, enter to confirm)")
.items(&feature_labels)
.interact()
.context("reading feature selection")?;
let features = selected_indices
.into_iter()
.map(|i| OPTIONAL_FEATURES[i].0.to_string())
.collect();
let tauri = Confirm::with_theme(&theme)
.with_prompt(
"Also scaffold Tauri support for a desktop build (src-tauri/, DEPLOY_TYPE=app)?",
)
.default(false)
.interact()
.context("reading Tauri choice")?;
Ok(Answers {
path,
auth,
tauri,
features,
})
}
pub fn validate_feature_names(features: &[String]) -> Result<()> {
for feature in features {
anyhow::ensure!(
OPTIONAL_FEATURES.iter().any(|(name, _)| name == feature),
"unknown feature `{feature}` - valid features are: {}",
OPTIONAL_FEATURES
.iter()
.map(|(name, _)| *name)
.collect::<Vec<_>>()
.join(", ")
);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn validate_feature_names_accepts_every_real_feature() {
let names: Vec<String> = OPTIONAL_FEATURES
.iter()
.map(|(name, _)| name.to_string())
.collect();
assert!(validate_feature_names(&names).is_ok());
}
#[test]
fn validate_feature_names_rejects_a_typo() {
let err = validate_feature_names(&["permisions".to_string()]).unwrap_err();
assert!(err.to_string().contains("unknown feature `permisions`"));
assert!(err.to_string().contains("permissions"));
}
#[test]
fn validate_feature_names_accepts_an_empty_list() {
assert!(validate_feature_names(&[]).is_ok());
}
}