Skip to main content

doido_generators/
new_options.rs

1//! Typed options for `doido new` — shared between the CLI and the project generator.
2
3use clap::ValueEnum;
4use doido_core::{anyhow, Result};
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
7pub enum DatabaseBackend {
8    Sqlite,
9    Postgres,
10    Mysql,
11}
12
13impl DatabaseBackend {
14    pub fn as_str(self) -> &'static str {
15        match self {
16            Self::Sqlite => "sqlite",
17            Self::Postgres => "postgres",
18            Self::Mysql => "mysql",
19        }
20    }
21}
22
23#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
24pub enum CacheBackend {
25    Memory,
26    Redis,
27    Memcache,
28}
29
30impl CacheBackend {
31    pub fn as_str(self) -> &'static str {
32        match self {
33            Self::Memory => "memory",
34            Self::Redis => "redis",
35            Self::Memcache => "memcache",
36        }
37    }
38}
39
40#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
41pub enum JobsBackend {
42    Memory,
43    Db,
44    Redis,
45}
46
47impl JobsBackend {
48    pub fn as_str(self) -> &'static str {
49        match self {
50            Self::Memory => "memory",
51            Self::Db => "db",
52            Self::Redis => "redis",
53        }
54    }
55}
56
57pub fn parse_database(s: &str) -> Result<DatabaseBackend> {
58    match s.trim().to_ascii_lowercase().as_str() {
59        "sqlite" => Ok(DatabaseBackend::Sqlite),
60        "postgres" | "postgresql" => Ok(DatabaseBackend::Postgres),
61        "mysql" => Ok(DatabaseBackend::Mysql),
62        other => Err(anyhow::anyhow!(
63            "Unknown database: {other}. Use sqlite, postgres, or mysql."
64        )),
65    }
66}
67
68pub fn parse_cache(s: &str) -> Result<CacheBackend> {
69    match s.trim().to_ascii_lowercase().as_str() {
70        "memory" => Ok(CacheBackend::Memory),
71        "redis" => Ok(CacheBackend::Redis),
72        "memcache" | "memcached" => Ok(CacheBackend::Memcache),
73        other => Err(anyhow::anyhow!(
74            "Unknown cache backend: {other}. Use memory, redis, or memcache."
75        )),
76    }
77}
78
79pub fn parse_jobs(s: &str) -> Result<JobsBackend> {
80    match s.trim().to_ascii_lowercase().as_str() {
81        "memory" | "inmemory" | "in_memory" => Ok(JobsBackend::Memory),
82        "db" | "database" | "sql" => Ok(JobsBackend::Db),
83        "redis" => Ok(JobsBackend::Redis),
84        other => Err(anyhow::anyhow!(
85            "Unknown jobs backend: {other}. Use memory, db, or redis."
86        )),
87    }
88}