oxyde_cloud_common/
config.rs1use anyhow::{Context, Result};
2
3use serde::{Deserialize, Serialize};
4use std::path::PathBuf;
5use toml::Table;
6
7#[derive(Serialize, Deserialize, Debug, Clone)]
8pub struct CloudConfig {
9 pub app: AppConfig,
10
11 pub env: Table,
12
13 #[serde(skip)]
14 pub leptos_config: Option<leptos_config::ConfFile>,
15}
16
17#[derive(Serialize, Deserialize, Debug, Clone)]
18pub struct AppConfig {
19 pub slug: String,
20}
21
22impl AppConfig {
23 pub const ALLOWED_CHARS: [char; 38] = [
24 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
25 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
26 '-', '_',
27 ];
28
29 pub const MIN_SLUG_LENGTH: usize = 5;
30
31 pub fn slug_requirements() -> String {
32 format!("App slug must be at least {} characters long, lower case alphanumeric and can contain underscores or dashes.", Self::MIN_SLUG_LENGTH)
33 }
34
35 pub fn is_valid_slug(slug: impl AsRef<str>) -> bool {
36 slug.as_ref().len() >= Self::MIN_SLUG_LENGTH
37 && slug
38 .as_ref()
39 .chars()
40 .next()
41 .map(|c| c.is_ascii_lowercase())
42 .unwrap_or(false)
43 && slug
44 .as_ref()
45 .chars()
46 .all(|c| Self::ALLOWED_CHARS.contains(&c))
47 }
48}
49
50impl CloudConfig {
53 pub async fn load(path: &PathBuf) -> Result<Self> {
54 let contents = std::fs::read_to_string(path)
55 .with_context(|| format!("Failed to read config file: {}", path.display()))?;
56
57 let mut config: Self = toml::from_str(&contents)
58 .with_context(|| format!("Failed to parse TOML config file: {}", path.display()))?;
59
60 config.leptos_config = Some(
61 leptos_config::get_configuration(Some("Cargo.toml"))
62 .context("Failed to load Leptos configuration")?,
63 );
64
65 Ok(config)
66 }
67
68 pub fn deployed_url(&self) -> String {
69 format!("https://{}.oxydecloud.com", self.app.slug)
70 }
71}