use std::str::FromStr;
pub(crate) struct TgqeConfig {
#[cfg(feature = "renderer")]
pub render_limit: u64,
#[cfg(feature = "renderer")]
pub compact: bool,
#[cfg(feature = "store-to-db")]
pub db_file_format: String,
#[cfg(feature = "store-to-db")]
pub db_max_connections: u32,
}
impl TgqeConfig {
pub(crate) fn load() -> Self {
dotenvy::dotenv().ok();
Self {
#[cfg(feature = "renderer")]
render_limit: Self::env_parsed(
"TGQE_RENDER_LIMIT",
30u64,
),
#[cfg(feature = "renderer")]
compact: Self::env_parsed(
"TGQE_COMPACT",
false,
),
#[cfg(feature = "store-to-db")]
db_file_format: Self::env_string(
"TGQE_DB_FILE_FORMAT",
"tgqe_record_%h-%M-%s",
),
#[cfg(feature = "store-to-db")]
db_max_connections:
Self::env_parsed(
"TGQE_DB_MAX_CONNECTIONS",
5u32,
),
}
}
#[cfg(feature = "store-to-db")]
pub(crate) fn env_string(
key: &str,
default: &str,
) -> String {
std::env::var(key).unwrap_or_else(
|_| default.to_string(),
)
}
pub(crate) fn env_parsed<T: FromStr>(
key: &str,
default: T,
) -> T {
std::env::var(key)
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(default)
}
}