Skip to main content

proa_cli/capabilities/
mod.rs

1// Copyright Proa Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Deterministic capability resolution for application-first site scaffolds.
5
6use anyhow::{bail, Result};
7use clap::ValueEnum;
8use serde::Serialize;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, ValueEnum)]
11#[serde(rename_all = "snake_case")]
12pub enum SiteTemplate {
13    App,
14    Marketing,
15    Docs,
16    Minimal,
17    Origin,
18}
19
20impl SiteTemplate {
21    pub fn parse_compat(value: &str) -> Result<Self> {
22        match value {
23            "app" | "basic" => Ok(Self::App),
24            "marketing" => Ok(Self::Marketing),
25            "docs" => Ok(Self::Docs),
26            "minimal" => Ok(Self::Minimal),
27            "origin" => Ok(Self::Origin),
28            other => {
29                bail!("unknown template `{other}` (expected app, marketing, docs, minimal, or origin)")
30            }
31        }
32    }
33
34    pub const fn as_str(self) -> &'static str {
35        match self {
36            Self::App => "app",
37            Self::Marketing => "marketing",
38            Self::Docs => "docs",
39            Self::Minimal => "minimal",
40            Self::Origin => "origin",
41        }
42    }
43}
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, ValueEnum)]
46#[serde(rename_all = "snake_case")]
47pub enum DatabaseChoice {
48    Sqlite,
49    None,
50}
51
52impl DatabaseChoice {
53    pub const fn as_str(self) -> &'static str {
54        match self {
55            Self::Sqlite => "sqlite",
56            Self::None => "none",
57        }
58    }
59}
60
61#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, ValueEnum)]
62#[serde(rename_all = "snake_case")]
63pub enum UiChoice {
64    Proa,
65    None,
66    #[value(skip)]
67    ProaDocs,
68}
69
70impl UiChoice {
71    pub const fn as_str(self) -> &'static str {
72        match self {
73            Self::Proa => "proa",
74            Self::None => "none",
75            Self::ProaDocs => "proa-docs",
76        }
77    }
78}
79
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, ValueEnum)]
81#[serde(rename_all = "snake_case")]
82pub enum StylingChoice {
83    Tailwind,
84    Css,
85    #[value(skip)]
86    Docs,
87}
88
89impl StylingChoice {
90    pub const fn as_str(self) -> &'static str {
91        match self {
92            Self::Tailwind => "tailwind",
93            Self::Css => "css",
94            Self::Docs => "docs",
95        }
96    }
97}
98
99#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, ValueEnum)]
100#[serde(rename_all = "snake_case")]
101pub enum StarterChoice {
102    Guided,
103    Blank,
104}
105
106impl StarterChoice {
107    pub const fn as_str(self) -> &'static str {
108        match self {
109            Self::Guided => "guided",
110            Self::Blank => "blank",
111        }
112    }
113}
114
115#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, ValueEnum)]
116#[serde(rename_all = "snake_case")]
117pub enum DeployChoice {
118    None,
119    Docker,
120    Railway,
121    Coolify,
122}
123
124impl DeployChoice {
125    pub const fn as_str(self) -> &'static str {
126        match self {
127            Self::None => "none",
128            Self::Docker => "docker",
129            Self::Railway => "railway",
130            Self::Coolify => "coolify",
131        }
132    }
133}
134
135#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
136#[serde(rename_all = "camelCase")]
137pub struct CapabilitySelection {
138    pub template: SiteTemplate,
139    pub database: DatabaseChoice,
140    pub ui: UiChoice,
141    pub styling: StylingChoice,
142    pub starter: StarterChoice,
143    pub deploy: DeployChoice,
144    pub markdown: bool,
145}
146
147#[derive(Debug, Clone, Copy, Default)]
148pub struct CapabilityOverrides {
149    pub database: Option<DatabaseChoice>,
150    pub ui: Option<UiChoice>,
151    pub styling: Option<StylingChoice>,
152    pub starter: Option<StarterChoice>,
153    pub deploy: Option<DeployChoice>,
154}
155
156#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
157#[serde(rename_all = "camelCase")]
158pub struct CapabilityId {
159    pub id: &'static str,
160    pub plan_version: u32,
161}
162
163impl CapabilitySelection {
164    pub fn resolve(template: SiteTemplate, overrides: CapabilityOverrides) -> Result<Self> {
165        let defaults = match template {
166            SiteTemplate::App => Self {
167                template,
168                database: DatabaseChoice::Sqlite,
169                ui: UiChoice::Proa,
170                styling: StylingChoice::Tailwind,
171                starter: StarterChoice::Guided,
172                deploy: DeployChoice::None,
173                markdown: true,
174            },
175            SiteTemplate::Marketing => Self {
176                template,
177                database: DatabaseChoice::None,
178                ui: UiChoice::Proa,
179                styling: StylingChoice::Tailwind,
180                starter: StarterChoice::Guided,
181                deploy: DeployChoice::None,
182                markdown: true,
183            },
184            SiteTemplate::Docs => Self {
185                template,
186                database: DatabaseChoice::None,
187                ui: UiChoice::ProaDocs,
188                styling: StylingChoice::Docs,
189                starter: StarterChoice::Guided,
190                deploy: DeployChoice::None,
191                markdown: true,
192            },
193            SiteTemplate::Minimal => Self {
194                template,
195                database: DatabaseChoice::None,
196                ui: UiChoice::None,
197                styling: StylingChoice::Css,
198                starter: StarterChoice::Blank,
199                deploy: DeployChoice::None,
200                markdown: false,
201            },
202            SiteTemplate::Origin => Self {
203                template,
204                database: DatabaseChoice::None,
205                ui: UiChoice::None,
206                styling: StylingChoice::Tailwind,
207                starter: StarterChoice::Guided,
208                deploy: DeployChoice::None,
209                markdown: true,
210            },
211        };
212
213        if template == SiteTemplate::Origin
214            && (overrides.database.is_some()
215                || overrides.ui.is_some()
216                || overrides.styling.is_some()
217                || overrides.starter.is_some())
218        {
219            bail!(
220                "the origin preset owns its application content; only --deploy can be overridden"
221            );
222        }
223
224        let selection = Self {
225            database: overrides.database.unwrap_or(defaults.database),
226            ui: overrides.ui.unwrap_or(defaults.ui),
227            styling: overrides.styling.unwrap_or(defaults.styling),
228            starter: overrides.starter.unwrap_or(defaults.starter),
229            deploy: overrides.deploy.unwrap_or(defaults.deploy),
230            ..defaults
231        };
232        if selection.template == SiteTemplate::Docs && selection.ui == UiChoice::Proa {
233            bail!("the docs preset owns its proa-docs UI; --ui proa is not supported yet");
234        }
235        Ok(selection)
236    }
237
238    pub fn capabilities(self) -> Vec<CapabilityId> {
239        let mut values = vec![
240            CapabilityId {
241                id: "base:site",
242                plan_version: 1,
243            },
244            CapabilityId {
245                id: "server:axum",
246                plan_version: 1,
247            },
248        ];
249        if self.database == DatabaseChoice::Sqlite {
250            values.push(CapabilityId {
251                id: "database:sqlite",
252                plan_version: 1,
253            });
254        }
255        if self.markdown {
256            values.push(CapabilityId {
257                id: "representation:markdown",
258                plan_version: 1,
259            });
260        }
261        if self.ui == UiChoice::Proa {
262            values.push(CapabilityId {
263                id: "ui:proa",
264                plan_version: 1,
265            });
266        }
267        if self.ui == UiChoice::ProaDocs {
268            values.push(CapabilityId {
269                id: "content:docs",
270                plan_version: 1,
271            });
272        }
273        values.push(CapabilityId {
274            id: match self.styling {
275                StylingChoice::Tailwind => "styling:tailwind",
276                StylingChoice::Css => "styling:css",
277                StylingChoice::Docs => "styling:docs",
278            },
279            plan_version: 1,
280        });
281        values.push(CapabilityId {
282            id: match self.starter {
283                StarterChoice::Guided => "starter:guided",
284                StarterChoice::Blank => "starter:blank",
285            },
286            plan_version: 1,
287        });
288        if self.deploy != DeployChoice::None {
289            values.push(CapabilityId {
290                id: "deploy:docker",
291                plan_version: 1,
292            });
293            if self.deploy == DeployChoice::Railway {
294                values.push(CapabilityId {
295                    id: "deploy:railway",
296                    plan_version: 1,
297                });
298            }
299            if self.deploy == DeployChoice::Coolify {
300                values.push(CapabilityId {
301                    id: "deploy:coolify",
302                    plan_version: 1,
303                });
304            }
305        }
306        values
307    }
308}
309
310#[cfg(test)]
311mod tests {
312    use super::*;
313
314    #[test]
315    fn app_defaults_match_the_application_first_contract() {
316        let selected =
317            CapabilitySelection::resolve(SiteTemplate::App, CapabilityOverrides::default())
318                .unwrap();
319        assert_eq!(selected.database, DatabaseChoice::Sqlite);
320        assert_eq!(selected.ui, UiChoice::Proa);
321        assert_eq!(selected.styling, StylingChoice::Tailwind);
322        assert_eq!(selected.starter, StarterChoice::Guided);
323        assert!(selected.markdown);
324    }
325
326    #[test]
327    fn basic_is_the_only_template_alias() {
328        assert_eq!(
329            SiteTemplate::parse_compat("basic").unwrap(),
330            SiteTemplate::App
331        );
332        assert!(SiteTemplate::parse_compat("dashboard").is_err());
333    }
334}