use std::env;
use std::ffi::OsString;
use std::path::PathBuf;
use std::time::Duration;
use tracing::debug;
pub const CAIRO_LS_DB_REPLACE_INTERVAL: &'_ str = "CAIRO_LS_DB_REPLACE_INTERVAL";
pub const CAIRO_LS_DB_REPLACE_MUTATIONS: &'_ str = "CAIRO_LS_DB_REPLACE_MUTATIONS";
pub const CAIRO_LS_LOG: &'_ str = "CAIRO_LS_LOG";
pub const CAIRO_LS_PROFILE: &'_ str = "CAIRO_LS_PROFILE";
pub const SCARB: &'_ str = "SCARB";
pub const SCARB_CACHE: &'_ str = "SCARB_CACHE";
pub const SCARB_TARGET_DIR: &'_ str = "SCARB_TARGET_DIR";
pub fn db_replace_interval() -> Duration {
const DEFAULT: u64 = 15 * 60;
env::var(CAIRO_LS_DB_REPLACE_INTERVAL)
.ok()
.and_then(|v| v.parse().ok())
.map(Duration::from_secs)
.unwrap_or_else(|| Duration::from_secs(DEFAULT))
}
pub fn db_replace_mutations() -> u64 {
const DEFAULT: u64 = 5_000;
env::var(CAIRO_LS_DB_REPLACE_MUTATIONS).ok().and_then(|v| v.parse().ok()).unwrap_or(DEFAULT)
}
pub fn log_env_filter() -> String {
env::var(CAIRO_LS_LOG).unwrap_or_default()
}
pub fn tracing_profile() -> bool {
env::var_os(CAIRO_LS_PROFILE).map(env_to_bool).unwrap_or_default()
}
pub fn scarb_path() -> Option<PathBuf> {
env::var_os(SCARB).map(PathBuf::from)
}
pub fn scarb_cache_path() -> Option<PathBuf> {
env::var_os(SCARB_CACHE).map(PathBuf::from)
}
pub fn scarb_target_path() -> Option<PathBuf> {
env::var_os(SCARB_TARGET_DIR).map(PathBuf::from)
}
pub fn report_to_logs() {
debug!("{CAIRO_LS_DB_REPLACE_INTERVAL}={:?}", db_replace_interval());
debug!("{CAIRO_LS_LOG}={}", log_env_filter());
debug!("{CAIRO_LS_PROFILE}={}", tracing_profile());
debug!("{SCARB}={}", scarb_path().map(|p| p.display().to_string()).unwrap_or_default());
}
fn env_to_bool(os: OsString) -> bool {
matches!(os.to_str(), Some("1") | Some("true"))
}