oxyde_cloud_common/
config.rs

1use 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 is_valid_slug(slug: impl AsRef<str>) -> bool {
32        slug.as_ref().len() >= Self::MIN_SLUG_LENGTH
33            && slug
34                .as_ref()
35                .chars()
36                .next()
37                .map(|c| c.is_ascii_lowercase())
38                .unwrap_or(false)
39            && slug
40                .as_ref()
41                .chars()
42                .all(|c| Self::ALLOWED_CHARS.contains(&c))
43    }
44}
45
46// Error type replaced with anyhow::Result for better error handling
47
48impl CloudConfig {
49    pub async fn load(path: &PathBuf) -> Result<Self> {
50        let contents = std::fs::read_to_string(path)
51            .with_context(|| format!("Failed to read config file: {}", path.display()))?;
52
53        let mut config: Self = toml::from_str(&contents)
54            .with_context(|| format!("Failed to parse TOML config file: {}", path.display()))?;
55
56        config.leptos_config = Some(
57            leptos_config::get_configuration(Some("Cargo.toml"))
58                .context("Failed to load Leptos configuration")?,
59        );
60
61        Ok(config)
62    }
63
64    pub fn deployed_url(&self) -> String {
65        format!("https://{}.oxydecloud.com", self.app.slug)
66    }
67}