use std::sync::{PoisonError, RwLock};
use lighty_core::QueryError;
use once_cell::sync::Lazy;
pub const BASE_URL: &str = "https://api.curseforge.com/v1";
pub const PROVIDER: &str = "curseforge";
static API_KEY: Lazy<RwLock<Option<String>>> = Lazy::new(|| RwLock::new(None));
pub fn set_api_key(key: impl Into<String>) {
let mut guard = API_KEY
.write()
.unwrap_or_else(PoisonError::into_inner);
*guard = Some(key.into());
}
pub fn read_api_key() -> Result<String, QueryError> {
let guard = API_KEY
.read()
.unwrap_or_else(PoisonError::into_inner);
guard.clone().ok_or_else(|| QueryError::Conversion {
message: "CurseForge API key not configured. Call \
`lighty_modsloader::curseforge::set_api_key(...)` \
before launching an instance with `.with_curseforge(...)`"
.to_string(),
})
}
pub fn url_encode(input: &str) -> String {
let mut out = String::with_capacity(input.len() + 8);
for byte in input.bytes() {
match byte {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
out.push(byte as char);
}
_ => out.push_str(&format!("%{:02X}", byte)),
}
}
out
}