use std::{env, path::Path, fs};
use crate::errors::CfgBoostError;
#[cfg(test)]
#[path = "../tests/unit/config.rs"]
mod unit_tests;
pub(crate) const ENV_KEY_PREDICATE : &str = "cfg_boost_predicate-"; pub(crate) const ENV_KEY_ALIAS : &str = "cfg_boost-"; pub(crate) const PREDICATE_PLACEHOLDER : &str = "{}"; const AUTO_DOC_KEY : &str = "cfg_boost_autodoc"; const MODIFIER_BEHAVIOUR_KEY : &str = "cfg_boost_release_modifier_behaviour"; const CFG_BOOST_CARGO_CACHE : &str = "CFG_BOOST_ATTR_DOC_SET"; const CFG_BOOST_DOCRS_TAG : &str = "[package.metadata.docs.rs]"; const CARGO_MANIFEST_DIR : &str = "CARGO_MANIFEST_DIR"; const CARGO_MANIFEST_NAME : &str = "Cargo.toml"; pub(crate) const DOC_ALIAS : &str = "doc";
pub(crate) const ALIASES : [(&str, &str); 12] = [
("linux", "linux:os"), ("unix", "unix:_"), ("windows", "windows:_"), ("macos", "macos:os"), ("android", "android:os"), ("ios", "ios:os"), ("wasm", "wasm:_"), (DOC_ALIAS, "doc:_"), ("test", "test:_"), ("debug", "debug_assertions:_"), ("desktop", "linux:os | windows:_ | macos:os"), ("mobile", "android:os | ios:os") ];
pub(crate) const PREDICATES : [(&str, &str); 12] = [
("ar", "target_arch = \"{}\""), ("tf", "target_feature = \"{}\""), ("os", "target_os = \"{}\""), ("fm", "target_family = \"{}\""), ("ev", "target_env = \"{}\""), ("ed", "target_endian = \"{}\""), ("pw", "target_pointer_width = \"{}\""), ("vn", "target_vendor = \"{}\""), ("at", "target_has_atomic = \"{}\""), ("pn", "panic = \"{}\""), ("ft", "feature = \"{}\""), ("_", PREDICATE_PLACEHOLDER) ];
pub(crate) enum ReleaseModifierBehaviour {
Panic,
Ignore,
}
#[allow(dead_code)]
pub(crate) fn get_release_modifier_behaviour() -> ReleaseModifierBehaviour{
match std::env::var(MODIFIER_BEHAVIOUR_KEY) {
Ok(value) => match value.as_str() {
"panic" => ReleaseModifierBehaviour::Panic,
"ignore" => ReleaseModifierBehaviour::Ignore,
_ => ReleaseModifierBehaviour::Panic, },
Err(_) => ReleaseModifierBehaviour::Panic, }
}
#[inline(always)]
pub(crate) fn is_cfg_boost_autodoc() -> bool {
match std::env::var(AUTO_DOC_KEY) {
Ok(value) => match value.as_str() {
"true" => true,
"false" => false,
_ => true, },
Err(_) => true, }
}
#[inline(always)]
pub(crate) fn if_docsrs_enabled() -> bool {
match env::var(CFG_BOOST_CARGO_CACHE) {
Ok(value) => {
value.eq("true")
},
Err(_) => {
let str_path = format!("{}/{}", env::var(CARGO_MANIFEST_DIR).unwrap(), CARGO_MANIFEST_NAME);
let file_path = Path::new(&str_path);
match fs::read_to_string(file_path){
Ok(content) => {
match content.find(CFG_BOOST_DOCRS_TAG){
Some(_) => {
env::set_var(CFG_BOOST_CARGO_CACHE, "true"); true
},
None => {
env::set_var(CFG_BOOST_CARGO_CACHE, "false"); false
},
}
},
Err(_) => {
env::set_var(CFG_BOOST_CARGO_CACHE, "false");
false
},
}
}
}
}
#[inline(always)]
pub fn get_cfg_boost_predicate(tokens : &str) -> Result<String, CfgBoostError> {
match tokens.find(":") {
Some(position) => {
let label = tokens[0..position].trim();
let cfg_opt = tokens[position + 1..].trim();
match env::var(format!("{}{}", ENV_KEY_PREDICATE, cfg_opt)) {
Ok(cfg_value) => Ok(String::from(cfg_value.replace(PREDICATE_PLACEHOLDER, label))),
Err(_) => {
match PREDICATES.iter().find(|p| p.0.eq(cfg_opt)){
Some(pred) => Ok(String::from(pred.1.replace(PREDICATE_PLACEHOLDER, label))),
None => Err(CfgBoostError::InvalidConfigurationPredicate(String::from(cfg_opt))),
}
},
}
},
None => Err(CfgBoostError::InvalidConfigurationPredicate(String::from(tokens))),
}
}
#[inline(always)]
pub fn get_cfg_boost_alias(label : &str) -> Result<String, CfgBoostError> {
match env::var(format!("{}{}", ENV_KEY_ALIAS, label)) {
Ok(alias) => Ok(alias.clone()),
Err(_e) => {
match ALIASES.iter().find(|a| a.0.eq(label)){
Some(alias) => Ok(String::from(alias.1)),
None => Err(CfgBoostError::AliasNotFound(String::from(label))),
}
},
}
}