use anyhow::{Error, Result};
use std::{env, fs, process};
use crate::core::RswErr;
use crate::utils::print;
pub static RSW_FILE: &'static str = "rsw.toml";
pub static RSW_DIR: &'static str = ".rsw";
pub static RSW_CRATES: &'static str = "rsw.crates";
pub static RSW_INFO: &'static str = "rsw.info";
pub static RSW_ERR: &'static str = "rsw.err";
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CrateConfig {
pub name: String,
#[serde(default = "default_root")]
pub root: Option<String>,
#[serde(default = "default_out_dir")]
pub out_dir: Option<String>,
#[serde(default = "default_false")]
pub link: Option<bool>,
#[serde(default = "default_watch")]
pub watch: Option<WatchOptions>,
#[serde(default = "default_build")]
pub build: Option<BuildOptions>,
#[serde(default = "default_target")]
pub target: Option<String>,
pub scope: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct WatchOptions {
#[serde(default = "default_true")]
pub run: Option<bool>,
#[serde(default = "default_dev")]
pub profile: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct BuildOptions {
#[serde(default = "default_true")]
pub run: Option<bool>,
#[serde(default = "default_release")]
pub profile: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct NewOptions {
#[serde(default = "default_wasmpack")]
pub using: Option<String>,
#[serde(default = "default_empty")]
pub dir: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RswConfig {
pub name: Option<String>,
pub version: Option<String>,
pub cli: Option<String>,
pub interval: Option<u64>,
#[serde(default = "default_new")]
pub new: Option<NewOptions>,
#[serde(default)]
pub crates: Vec<CrateConfig>,
}
impl Default for RswConfig {
fn default() -> Self {
Self {
name: Some("rsw".into()),
version: Some("0.0.0".into()),
interval: Some(50),
cli: Some("npm".into()),
new: default_new(),
crates: vec![],
}
}
}
impl RswConfig {
pub fn new() -> Result<RswConfig, Error> {
let rsw_file = env::current_dir().unwrap().join(RSW_FILE);
let rsw_content = fs::read_to_string(rsw_file).unwrap_or_else(|e| {
print(RswErr::Config(e));
process::exit(1);
});
let rsw_toml_parse = toml::from_str(&rsw_content).unwrap_or_else(|e| {
print(RswErr::ParseToml(e));
process::exit(1);
});
Ok(rsw_toml_parse)
}
}
fn default_root() -> Option<String> {
Some(".".into())
}
fn default_out_dir() -> Option<String> {
Some("pkg".into())
}
fn default_release() -> Option<String> {
Some("release".into())
}
fn default_dev() -> Option<String> {
Some("dev".into())
}
fn default_target() -> Option<String> {
Some("web".into())
}
fn default_true() -> Option<bool> {
Some(true)
}
fn default_false() -> Option<bool> {
Some(false)
}
fn default_wasmpack() -> Option<String> {
Some("wasm-pack".into())
}
fn default_empty() -> Option<String> {
Some("".into())
}
fn default_new() -> Option<NewOptions> {
Some(NewOptions {
using: default_wasmpack(),
dir: default_empty(),
})
}
fn default_watch() -> Option<WatchOptions> {
Some(WatchOptions {
run: default_true(),
profile: default_dev(),
})
}
fn default_build() -> Option<BuildOptions> {
Some(BuildOptions {
run: default_true(),
profile: default_release(),
})
}