|
1 | | -use axoasset::SourceFile; |
2 | | -use camino::Utf8PathBuf; |
3 | | -use schemars::JsonSchema; |
4 | | -use serde::{Deserialize, Serialize}; |
5 | | - |
6 | | -use crate::errors::*; |
7 | | - |
8 | | -use super::{BuildLayer, ComponentLayer, MarketingLayer, ProjectLayer, StyleLayer, WorkspaceLayer}; |
9 | | - |
10 | | -/// Configuration for `oranda` (typically stored in oranda.json) |
11 | | -#[derive(Debug, Serialize, Deserialize, JsonSchema)] |
12 | | -#[serde(deny_unknown_fields)] |
13 | | -pub struct OrandaLayer { |
14 | | - /// Info about the project/application you're making a site for |
15 | | - /// |
16 | | - /// All of these values should automatically be sourced from your Cargo.toml or package.json |
17 | | - /// whenever possible. You should only need to set these if you want to override the value. |
18 | | - pub project: Option<ProjectLayer>, |
19 | | - /// Settings for the build/output of the site |
20 | | - pub build: Option<BuildLayer>, |
21 | | - /// Settings for social/marketing/analytics |
22 | | - pub marketing: Option<MarketingLayer>, |
23 | | - /// Settings for themes/styles of the site |
24 | | - pub styles: Option<StyleLayer>, |
25 | | - /// Additional optional components |
26 | | - pub components: Option<ComponentLayer>, |
27 | | - /// Workspace configuration |
28 | | - pub workspace: Option<WorkspaceLayer>, |
29 | | - /// Field that text-editors can use to fetch the schema for this struct |
30 | | - /// |
31 | | - /// We never use this, but we don't want to error out if its set. |
32 | | - #[serde(rename = "$schema")] |
33 | | - pub _schema: Option<String>, |
34 | | -} |
35 | | - |
36 | | -impl OrandaLayer { |
37 | | - pub fn load(config_path: &Utf8PathBuf) -> Result<Option<OrandaLayer>> { |
38 | | - let config_result = SourceFile::load_local(config_path.as_path()); |
39 | | - |
40 | | - match config_result { |
41 | | - Ok(config) => { |
42 | | - let data: OrandaLayer = config.deserialize_json()?; |
43 | | - Ok(Some(data)) |
44 | | - } |
45 | | - Err(_) => { |
46 | | - tracing::debug!("No config found, using default values"); |
47 | | - Ok(None) |
48 | | - } |
49 | | - } |
50 | | - } |
51 | | -} |
| 1 | +use axoasset::SourceFile; |
| 2 | +use camino::Utf8PathBuf; |
| 3 | +use schemars::JsonSchema; |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | + |
| 6 | +use crate::errors::*; |
| 7 | + |
| 8 | +use super::{BuildLayer, ComponentLayer, MarketingLayer, ProjectLayer, StyleLayer, WorkspaceLayer}; |
| 9 | + |
| 10 | +/// Configuration for `oranda` (typically stored in oranda.json) |
| 11 | +#[derive(Debug, Serialize, Deserialize, JsonSchema)] |
| 12 | +#[serde(deny_unknown_fields)] |
| 13 | +pub struct OrandaLayer { |
| 14 | + /// Info about the project/application you're making a site for |
| 15 | + /// |
| 16 | + /// All of these values should automatically be sourced from your Cargo.toml or package.json |
| 17 | + /// whenever possible. You should only need to set these if you want to override the value. |
| 18 | + pub project: Option<ProjectLayer>, |
| 19 | + /// Settings for the build/output of the site |
| 20 | + pub build: Option<BuildLayer>, |
| 21 | + /// Settings for social/marketing/analytics |
| 22 | + pub marketing: Option<MarketingLayer>, |
| 23 | + /// Settings for themes/styles of the site |
| 24 | + pub styles: Option<StyleLayer>, |
| 25 | + /// Additional optional components |
| 26 | + pub components: Option<ComponentLayer>, |
| 27 | + /// Workspace configuration |
| 28 | + pub workspace: Option<WorkspaceLayer>, |
| 29 | + /// Field that text-editors can use to fetch the schema for this struct |
| 30 | + /// |
| 31 | + /// We never use this, but we don't want to error out if its set. |
| 32 | + #[serde(rename = "$schema")] |
| 33 | + pub _schema: Option<String>, |
| 34 | +} |
| 35 | + |
| 36 | +impl OrandaLayer { |
| 37 | + pub fn load(config_path: &Utf8PathBuf) -> Result<Option<OrandaLayer>> { |
| 38 | + let mut config_path = config_path.to_owned(); |
| 39 | + if config_path.extension() == Some("json") { |
| 40 | + if config_path.exists() { |
| 41 | + let config = SourceFile::load_local(config_path.as_path())?; |
| 42 | + return Ok(Some(config.deserialize_json()?)); |
| 43 | + } else { |
| 44 | + // Temporary hack |
| 45 | + config_path.set_extension("toml"); |
| 46 | + } |
| 47 | + } |
| 48 | + if !config_path.exists() { |
| 49 | + tracing::debug!("No config found, using default values"); |
| 50 | + return Ok(None); |
| 51 | + } |
| 52 | + if config_path.extension() == Some("toml") { |
| 53 | + tracing::warn!("!!!Using toml config!!!!"); |
| 54 | + let config = SourceFile::load_local(config_path.as_path())?; |
| 55 | + return Ok(Some(config.deserialize_toml()?)); |
| 56 | + } |
| 57 | + |
| 58 | + tracing::debug!("No config found, using default values"); |
| 59 | + Ok(None) |
| 60 | + } |
| 61 | +} |
0 commit comments