#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos",
target_os = "windows",
target_os = "linux",
target_os = "android"
))]
use mistralrs::TokenSource;
const BUILD_TIME_HF_TOKEN: Option<&str> = option_env!("HF_TOKEN");
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos",
target_os = "windows",
target_os = "linux",
target_os = "android"
))]
pub fn hf_token_source() -> TokenSource {
match BUILD_TIME_HF_TOKEN {
Some(token) if !token.is_empty() => {
log::debug!("Using build-time HF_TOKEN for Hugging Face authentication.");
TokenSource::Literal(token.to_string())
}
_ => {
log::debug!(
"No build-time HF_TOKEN found; falling back to cached token \
(~/.cache/huggingface/token)."
);
TokenSource::CacheToken
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn build_time_token_is_option() {
let _: Option<&str> = BUILD_TIME_HF_TOKEN;
}
#[test]
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "visionos",
target_os = "watchos",
target_os = "windows",
target_os = "linux",
target_os = "android"
))]
fn token_source_returns_valid_variant() {
let source = hf_token_source();
let display = format!("{}", source);
assert!(
display.starts_with("literal:") || display == "cache",
"Unexpected token source: {display}"
);
}
}