use crate::scaffold::find_workspace_root;
use anyhow::{Context, Result};
use dialoguer::theme::ColorfulTheme;
use dialoguer::{Confirm, Input, MultiSelect};
use std::path::Path;
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 workspace: Option<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 workspace = detect_or_prompt_workspace(&theme)?;
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,
workspace,
})
}
fn detect_or_prompt_workspace(theme: &ColorfulTheme) -> Result<Option<String>> {
let cwd = std::env::current_dir().context("reading current directory")?;
let cwd = cwd
.canonicalize()
.with_context(|| format!("resolving {}", cwd.display()))?;
if find_workspace_root(&cwd)?.is_some() {
return Ok(None);
}
println!(
"This directory doesn't look like it's inside a Larust workspace checkout - Larust \
crates aren't published to crates.io yet, so a new app needs one nearby to resolve \
them as local path dependencies (see docs/getting-started/installation.md in the \
checkout if you haven't cloned one yet).\n"
);
loop {
let input: String = Input::with_theme(theme)
.with_prompt("Path to your Larust workspace checkout")
.interact_text()
.context("reading workspace checkout path")?;
let candidate = match Path::new(&input).canonicalize() {
Ok(candidate) => candidate,
Err(error) => {
println!("Couldn't resolve `{input}`: {error} - try again.");
continue;
}
};
match find_workspace_root(&candidate)? {
Some(root) => return Ok(Some(root.display().to_string())),
None => println!(
"`{input}` doesn't look like a Larust workspace checkout (no ancestor \
`Cargo.toml` with a `[workspace]` table found) - try again."
),
}
}
}
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());
}
}