use std::sync::RwLock;
pub const DEFAULT: &str = concat!(
"datui/",
env!("CARGO_PKG_VERSION"),
" (+https://github.com/derekwisong/datui)"
);
static CONFIGURED: RwLock<Option<String>> = RwLock::new(None);
pub fn is_valid(value: &str) -> bool {
value.chars().all(|c| (' '..='~').contains(&c))
}
pub fn configure(setting: &str) {
let value = setting.trim();
*CONFIGURED.write().unwrap_or_else(|e| e.into_inner()) =
(!value.is_empty()).then(|| value.to_string());
}
pub fn get() -> String {
match CONFIGURED
.read()
.unwrap_or_else(|e| e.into_inner())
.as_deref()
{
Some(value) => value.to_string(),
None => DEFAULT.to_string(),
}
}
#[cfg(any(feature = "http", feature = "cloud"))]
pub fn ureq_config() -> ureq::config::ConfigBuilder<ureq::typestate::AgentScope> {
ureq::Agent::config_builder().user_agent(get())
}
#[cfg(feature = "cloud")]
pub const CLIENT_KEY: object_store::ClientConfigKey = object_store::ClientConfigKey::UserAgent;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_default_names_datui_its_version_and_home_and_nothing_else() {
assert_eq!(
DEFAULT,
format!(
"datui/{} (+https://github.com/derekwisong/datui)",
env!("CARGO_PKG_VERSION")
)
);
assert!(is_valid(DEFAULT));
}
#[test]
fn control_characters_are_not_a_header() {
assert!(is_valid("my-tool/1.0 (+https://example.com)"));
assert!(!is_valid("a\r\nX-Injected: 1"));
assert!(!is_valid("café"));
}
}