Skip to main content

proa_cli/config/
model.rs

1// Copyright Proa Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4// Shared by the CLI and the host executable that evaluates proa.config.rs.
5use std::{borrow::Borrow, collections::BTreeMap};
6
7use serde::{Deserialize, Serialize};
8
9pub const CONFIG_FILE: &str = "proa.config.rs";
10pub const DEFAULT_PUBLIC_DIRECTORY: &str = "public";
11pub const DEFAULT_DEV_COMMAND: &str = "proa dev";
12pub const DEFAULT_BUILD_COMMAND: &str = "proa build";
13
14/// Text storage for authored config. String literals work with the default type.
15pub trait ConfigText: Borrow<str> + Ord + ConfigPath {}
16impl<T: Borrow<str> + Ord + ConfigPath> ConfigText for T {}
17
18/// Path storage for authored config. Use an owned type for computed paths.
19pub trait ConfigPath: Default + From<&'static str> {}
20impl<T: Default + From<&'static str>> ConfigPath for T {}
21
22fn text_is_empty(text: &impl ConfigText) -> bool {
23    text.borrow().is_empty()
24}
25
26#[derive(Debug, Clone, Deserialize, Serialize)]
27#[serde(default)]
28pub struct Config<Text: ConfigText = &'static str, Path: ConfigPath = Text> {
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub application: Option<ApplicationConfig<Text>>,
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub database: Option<DatabaseConfig<Text, Path>>,
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub deploy: Option<DeployConfig<Text>>,
35    pub scripts: ScriptsConfig<Text, Path>,
36    pub paths: ProjectPaths<Path>,
37    pub registries: RegistriesConfig<Text>,
38    pub build: BuildConfig<Text, Path>,
39}
40
41#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
42#[serde(deny_unknown_fields)]
43pub struct RegistriesConfig<Text: ConfigText = &'static str> {
44    #[serde(rename = "@proa", skip_serializing_if = "Option::is_none")]
45    pub proa: Option<Text>,
46    #[serde(rename = "@proa-pro", skip_serializing_if = "Option::is_none")]
47    pub proa_pro: Option<Text>,
48}
49
50impl<Text: ConfigText> RegistriesConfig<Text> {
51    pub fn public_only() -> Self {
52        Self {
53            proa: Some("https://ui.proa.so/r/{name}.json".into()),
54            proa_pro: None,
55        }
56    }
57
58    pub fn get(&self, alias: &str) -> Option<&str> {
59        match alias {
60            "@proa" => self.proa.as_ref(),
61            "@proa-pro" => self.proa_pro.as_ref(),
62            _ => None,
63        }
64        .map(Text::borrow)
65    }
66
67    pub fn iter(&self) -> impl Iterator<Item = (&str, &str)> {
68        [
69            ("@proa", self.proa.as_ref()),
70            ("@proa-pro", self.proa_pro.as_ref()),
71        ]
72        .into_iter()
73        .filter_map(|(alias, url)| url.map(|url| (alias, url.borrow())))
74    }
75}
76
77impl<Text: ConfigText> Default for RegistriesConfig<Text> {
78    fn default() -> Self {
79        Self {
80            proa_pro: Some("https://console.proa.so/r/{name}.json".into()),
81            ..Self::public_only()
82        }
83    }
84}
85
86#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
87#[serde(default, deny_unknown_fields)]
88pub struct ApplicationConfig<Text: ConfigText = &'static str> {
89    #[serde(skip_serializing_if = "text_is_empty")]
90    pub binary: Text,
91}
92
93#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize, Serialize)]
94#[serde(rename_all = "lowercase")]
95pub enum DatabaseDriver {
96    #[default]
97    None,
98    Sqlite,
99}
100
101impl DatabaseDriver {
102    pub const fn as_str(self) -> &'static str {
103        match self {
104            Self::None => "none",
105            Self::Sqlite => "sqlite",
106        }
107    }
108}
109
110#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
111#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
112pub struct DatabaseConfig<Text: ConfigText = &'static str, Path: ConfigPath = Text> {
113    pub driver: DatabaseDriver,
114    #[serde(skip_serializing_if = "Option::is_none")]
115    pub migrations: Option<Path>,
116    #[serde(skip_serializing_if = "text_is_empty")]
117    pub integration: Text,
118}
119
120#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
121#[serde(default, deny_unknown_fields)]
122pub struct DeployConfig<Text: ConfigText = &'static str> {
123    #[serde(skip_serializing_if = "text_is_empty")]
124    pub provider: Text,
125    #[serde(skip_serializing_if = "Option::is_none")]
126    pub cdn: Option<CdnConfig<Text>>,
127}
128
129#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
130#[serde(tag = "provider", rename_all = "lowercase", deny_unknown_fields)]
131pub enum CdnConfig<Text: ConfigText = &'static str> {
132    Cloudflare {
133        #[serde(rename = "zoneId")]
134        zone_id: Text,
135        hostname: Text,
136    },
137    Command {
138        command: Vec<Text>,
139    },
140}
141
142#[derive(Debug, Clone, Deserialize, Serialize)]
143#[serde(default)]
144pub struct ScriptsConfig<Text: ConfigText = &'static str, Path: ConfigPath = Text> {
145    /// Shell command for `proa run dev`. Defaults to `proa dev`.
146    pub dev: Text,
147    /// Shell command for `proa run build`. Defaults to `proa build`.
148    pub build: Text,
149    /// Command that builds `styles.css` in the public directory.
150    #[serde(skip_serializing_if = "text_is_empty")]
151    pub css: Text,
152    #[serde(skip_serializing_if = "ScriptHooks::is_empty")]
153    pub before: ScriptHooks<Text>,
154    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
155    pub named: BTreeMap<Text, ScriptConfig<Text, Path>>,
156}
157
158/// Named tasks to run before each linked lifecycle.
159#[derive(Debug, Clone, PartialEq, Eq, Default, Deserialize, Serialize)]
160#[serde(default)]
161pub struct ScriptHooks<Text: ConfigText = &'static str> {
162    #[serde(skip_serializing_if = "Vec::is_empty")]
163    pub dev: Vec<Text>,
164    #[serde(skip_serializing_if = "Vec::is_empty")]
165    pub build: Vec<Text>,
166    #[serde(skip_serializing_if = "Vec::is_empty")]
167    pub test: Vec<Text>,
168}
169
170impl<Text: ConfigText> ScriptHooks<Text> {
171    pub(super) fn is_empty(&self) -> bool {
172        self.dev.is_empty() && self.build.is_empty() && self.test.is_empty()
173    }
174}
175
176#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
177#[serde(default, rename_all = "camelCase")]
178pub struct ScriptConfig<Text: ConfigText = &'static str, Path: ConfigPath = Text> {
179    pub description: Text,
180    pub command: Vec<Text>,
181    pub cwd: Path,
182    #[serde(alias = "dependencies")]
183    pub depends_on: Vec<Text>,
184    pub inputs: Vec<Text>,
185    pub outputs: Vec<Path>,
186    pub env: BTreeMap<Text, Text>,
187}
188
189#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
190#[serde(default)]
191pub struct ProjectPaths<Path: ConfigPath = &'static str> {
192    pub components: Path,
193    pub assets: Path,
194    /// Public directory for managed assets. Set to `None` to disable publication.
195    pub public: Option<Path>,
196}
197
198#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
199#[serde(default)]
200pub struct BuildConfig<Text: ConfigText = &'static str, Path: ConfigPath = Text> {
201    /// Minify the generated RSJS modules and runtimes served by Axum. Defaults to true.
202    pub minify_rsjs: bool,
203    /// `None` when the key is absent, so a Tailwind asset can be inferred.
204    /// `Some(vec![])` is an explicit "build no assets" and is left alone --
205    /// with a plain `Vec` the two were indistinguishable and an empty array
206    /// silently grew a Tailwind step back.
207    #[serde(skip_serializing_if = "Option::is_none")]
208    pub assets: Option<Vec<AssetConfig<Text, Path>>>,
209}
210
211impl<Text: ConfigText, Path: ConfigPath> BuildConfig<Text, Path> {
212    pub fn assets(&self) -> &[AssetConfig<Text, Path>] {
213        self.assets.as_deref().unwrap_or_default()
214    }
215}
216
217#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
218#[serde(default)]
219pub struct AssetConfig<Text: ConfigText = &'static str, Path: ConfigPath = Text> {
220    pub kind: Text,
221    pub command: Vec<Text>,
222    pub input: Path,
223    pub output: Path,
224    #[serde(skip_serializing_if = "Option::is_none")]
225    pub config: Option<Path>,
226}
227
228impl<Text: ConfigText, Path: ConfigPath> Default for Config<Text, Path> {
229    fn default() -> Self {
230        Self {
231            application: None,
232            database: None,
233            deploy: None,
234            scripts: ScriptsConfig::default(),
235            paths: ProjectPaths::default(),
236            registries: RegistriesConfig::default(),
237            build: BuildConfig::default(),
238        }
239    }
240}
241
242impl<Text: ConfigText, Path: ConfigPath> Default for ScriptsConfig<Text, Path> {
243    fn default() -> Self {
244        Self {
245            dev: DEFAULT_DEV_COMMAND.into(),
246            build: DEFAULT_BUILD_COMMAND.into(),
247            css: "".into(),
248            before: ScriptHooks::default(),
249            named: BTreeMap::new(),
250        }
251    }
252}
253
254impl<Text: ConfigText, Path: ConfigPath> Default for ScriptConfig<Text, Path> {
255    fn default() -> Self {
256        Self {
257            description: Text::default(),
258            command: Vec::new(),
259            cwd: ".".into(),
260            depends_on: Vec::new(),
261            inputs: Vec::new(),
262            outputs: Vec::new(),
263            env: BTreeMap::new(),
264        }
265    }
266}
267
268impl<Path: ConfigPath> Default for ProjectPaths<Path> {
269    fn default() -> Self {
270        Self {
271            components: Path::from("src/components"),
272            // Must sit under the directory the generated server actually serves.
273            // `static/` is the URL prefix, not the folder: the axum scaffold mounts
274            // `public/` at `/static`, so assets written to `static/proa` were never
275            // reachable and every documented `/static/proa/...` link 404'd.
276            assets: Path::from("public/proa"),
277            public: Some(Path::from(DEFAULT_PUBLIC_DIRECTORY)),
278        }
279    }
280}
281
282impl<Text: ConfigText, Path: ConfigPath> Default for BuildConfig<Text, Path> {
283    fn default() -> Self {
284        Self {
285            minify_rsjs: true,
286            assets: None,
287        }
288    }
289}
290
291impl<Text: ConfigText, Path: ConfigPath> Default for AssetConfig<Text, Path> {
292    fn default() -> Self {
293        Self {
294            kind: "command".into(),
295            command: Vec::new(),
296            input: Path::default(),
297            output: Path::default(),
298            config: None,
299        }
300    }
301}