use crate::embargo_toml::{cfg_trait::ConfigFile, global_config::EmbargoGlobalConfig};
use serde::{Deserialize, Serialize};
use std::sync::LazyLock;
static GLOBAL_CONF: LazyLock<EmbargoGlobalConfig> =
LazyLock::new(EmbargoGlobalConfig::try_read);
#[derive(Serialize, Deserialize, Debug, Hash)]
pub struct EmbargoPackageConfig {
pub name: String,
pub version: String,
pub entry: String,
pub author: Option<String>,
source_path: Option<String>, build_path: Option<String>, auto_clean: Option<bool>, object_path: Option<String>, target_path_debug: Option<String>, target_path_release: Option<String>,
bin_path: Option<String>,
lib_path: Option<String>,
flags: Option<Vec<String>>,
args: Option<Vec<String>>,
}
impl EmbargoPackageConfig {
#[allow(unused)]
pub fn set_name(&mut self, name: &str) {
self.name = name.to_owned();
}
}
impl<'a> ConfigFile<'a> for EmbargoPackageConfig {
fn compiler(&'a self) -> &'a str {
GLOBAL_CONF.compiler()
}
fn linker(&'a self) -> &'a str {
GLOBAL_CONF.linker()
}
fn source_path(&'a self) -> &'a str {
if let Some(ref s) = self.source_path {
s
} else {
&GLOBAL_CONF.source_path
}
}
fn build_path(&'a self) -> &'a str {
if let Some(ref bp) = self.build_path {
bp
} else {
&GLOBAL_CONF.build_path
}
}
fn auto_clean(&self) -> bool {
self.auto_clean.unwrap_or(false)
}
fn object_path(&'a self) -> &'a str {
if let Some(ref op) = self.object_path {
op
} else {
&GLOBAL_CONF.object_path
}
}
fn target_path_debug(&'a self) -> &'a str {
if let Some(ref tpd) = self.target_path_debug {
tpd
} else {
&GLOBAL_CONF.target_path_debug
}
}
fn target_path_release(&'a self) -> &'a str {
if let Some(ref tpr) = self.target_path_release {
tpr
} else {
&GLOBAL_CONF.target_path_release
}
}
fn bin_path(&'a self) -> &'a str {
if let Some(ref bp) = self.bin_path {
bp
} else {
&GLOBAL_CONF.bin_path
}
}
fn lib_path(&'a self) -> &'a str {
if let Some(ref lp) = self.lib_path {
lp
} else {
&GLOBAL_CONF.lib_path
}
}
fn flags(&'a self) -> &'a [String] {
if let Some(ref f) = self.flags {
f
} else {
&[]
}
}
fn args(&'a self) -> &'a [String] {
if let Some(ref a) = self.args {
a
} else {
&[]
}
}
fn author(&self) -> Option<&str> {
self.author.as_deref()
}
}
impl Default for EmbargoPackageConfig {
fn default() -> Self {
Self {
name: "test".to_owned(),
version: "0.1.0".to_owned(),
entry: "main.cpp".to_owned(),
author: None,
source_path: None,
build_path: None,
auto_clean: None,
object_path: None,
target_path_debug: None,
target_path_release: None,
bin_path: None,
lib_path: None,
flags: None,
args: None,
}
}
}