use std::path::PathBuf;
#[cfg(feature = "download")]
use std::sync::{Arc, OnceLock};
use crate::{
ToolCache, ToolCacheArch, ToolPlatform,
cache::{RETRY_COUNT, get_tool_cache_path},
};
#[derive(Debug, Clone, Default)]
pub struct ToolCacheBuilder {
pub(crate) tool_cache: Option<PathBuf>,
pub(crate) arch: Option<crate::ToolCacheArch>,
pub(crate) platform: Option<crate::platform::ToolPlatform>,
pub(crate) retry_count: Option<u8>,
#[cfg(feature = "download")]
pub(crate) client: Option<reqwest::Client>,
}
impl ToolCacheBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn tool_cache(mut self, path: impl Into<PathBuf>) -> Self {
self.tool_cache = Some(path.into());
self
}
pub fn arch(mut self, arch: crate::ToolCacheArch) -> Self {
self.arch = Some(arch);
self
}
pub fn platform(mut self, platform: crate::platform::ToolPlatform) -> Self {
self.platform = Some(platform);
self
}
pub fn retry_count(mut self, count: u8) -> Self {
self.retry_count = Some(count);
self
}
#[cfg(feature = "download")]
pub fn client(mut self, client: reqwest::Client) -> Self {
self.client = Some(client);
self
}
pub fn build(&self) -> ToolCache {
let tool_cache = self.tool_cache.clone().unwrap_or_else(get_tool_cache_path);
let arch = self.arch.unwrap_or(match std::env::consts::ARCH {
"x86_64" | "amd64" => ToolCacheArch::X64,
"aarch64" => ToolCacheArch::ARM64,
_ => ToolCacheArch::Any,
});
let platform = self.platform.unwrap_or_else(ToolPlatform::from_current_os);
#[cfg(feature = "download")]
let client = {
let client = OnceLock::new();
if let Some(reqwest_client) = self.client.clone() {
let _ = client.set(reqwest_client);
}
Arc::new(client)
};
ToolCache {
tool_cache,
arch,
platform,
retry_count: self.retry_count.unwrap_or(RETRY_COUNT),
#[cfg(feature = "download")]
client,
}
}
}