1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use serde::{Deserialize, Serialize};

use crate::cache::config_path;

fn default_assets_serve_location() -> String {
    "dist/".to_string()
}

/// The configuration for collecting assets
#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
    #[serde(default = "default_assets_serve_location")]
    assets_serve_location: String,
}

impl Config {
    /// The location where assets will be served from. On web applications, this should be the URL path to the directory where assets are served from.
    pub fn with_assets_serve_location(&self, assets_serve_location: impl Into<String>) -> Self {
        Self {
            assets_serve_location: assets_serve_location.into(),
        }
    }

    /// The location where assets will be served from. On web applications, this should be the URL path to the directory where assets are served from.
    pub fn assets_serve_location(&self) -> &str {
        &self.assets_serve_location
    }

    /// Returns the current config
    pub fn current() -> Self {
        std::fs::read(config_path())
            .ok()
            .and_then(|config| toml::from_str(&String::from_utf8_lossy(&config)).ok())
            .unwrap_or_default()
    }

    /// Saves the config globally. This must be run before compiling the application you are collecting assets from.
    ///
    /// The assets macro will read the config from the global config file and set the assets serve location to the value in the config.
    pub fn save(&self) {
        let config = toml::to_string(&self).unwrap();
        let config_path = config_path();
        std::fs::create_dir_all(config_path.parent().unwrap()).unwrap();
        std::fs::write(config_path, config).unwrap();
    }
}

impl Default for Config {
    fn default() -> Self {
        Self {
            assets_serve_location: default_assets_serve_location(),
        }
    }
}