use crate::util::{ok, run, step, CliResult};
use std::process::Command;
struct Feature {
name: &'static str,
about: &'static str,
extras: &'static [(&'static str, &'static str)],
}
const FEATURES: &[Feature] = &[
Feature { name: "sqlite", about: "database layer with the SQLite driver", extras: &[] },
Feature { name: "postgres", about: "database layer with the PostgreSQL driver", extras: &[] },
Feature { name: "mysql", about: "database layer with the MySQL driver", extras: &[] },
Feature { name: "redis", about: "Redis cache (and Redis job queue with jobs)", extras: &[] },
Feature { name: "auth", about: "sessions, JWT, Argon2, guards, CSRF, OAuth", extras: &[] },
Feature {
name: "ui",
about: "Leptos pages, file-based routing, islands, server functions",
extras: &[("leptos@0.8", "ssr,islands")],
},
Feature { name: "jobs", about: "in-memory background queues and the cron scheduler", extras: &[] },
Feature { name: "jobs-db", about: "background queues stored in the database", extras: &[] },
Feature { name: "mail", about: "SMTP mail with templates", extras: &[] },
Feature { name: "storage", about: "local, S3 and Cloudflare R2 file storage", extras: &[] },
Feature { name: "tenancy", about: "subdomain multi-tenancy", extras: &[] },
Feature { name: "full", about: "everything above", extras: &[("leptos@0.8", "ssr,islands")] },
];
pub fn list() -> CliResult {
println!("Features you can add with `chy add <feature>...`:\n");
for f in FEATURES {
println!(" {:<10} {}", f.name, f.about);
}
println!(" {:<10} test helpers (added as a dev-dependency)", "testing");
Ok(())
}
pub fn run_add(features: &[String]) -> CliResult {
if features.is_empty() {
return list();
}
let mut chy_features = Vec::new();
let mut extras: Vec<(&str, &str)> = Vec::new();
let mut testing = false;
for name in features {
let name = name.trim().to_lowercase();
if name == "testing" {
testing = true;
continue;
}
let f = FEATURES
.iter()
.find(|f| f.name == name)
.ok_or_else(|| format!("unknown feature `{name}` — run `chy add --list` to see them all"))?;
if !chy_features.contains(&f.name) {
chy_features.push(f.name);
}
for e in f.extras {
if !extras.contains(e) {
extras.push(*e);
}
}
}
if !std::path::Path::new("Cargo.toml").is_file() {
return Err("no Cargo.toml here — run `chy add` inside a Chyabrung app".into());
}
if !chy_features.is_empty() {
let list = chy_features.join(",");
step(&format!("enabling chyabrung features: {list}"));
run(Command::new("cargo").args(["add", "chyabrung", "--features", &list]))?;
}
for (krate, feats) in &extras {
step(&format!("adding {krate}"));
run(Command::new("cargo").args(["add", krate, "--features", feats]))?;
}
if testing {
step("adding chyabrung-testing as a dev-dependency");
run(Command::new("cargo").args(["add", "--dev", "chyabrung-testing"]))?;
}
ok("Cargo.toml updated — the new crates download on your next build");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_framework_feature_is_listed() {
let manifest = include_str!("../../framework/Cargo.toml");
let table: toml::Table = manifest.parse().unwrap();
let declared = table["features"].as_table().unwrap();
for f in FEATURES {
assert!(declared.contains_key(f.name), "`{}` is not a chyabrung feature", f.name);
}
}
#[test]
fn rejects_unknown_features() {
let err = run_add(&["nope".into()]).unwrap_err();
assert!(err.contains("unknown feature"));
}
}