ghactions_toolcache/
builder.rs1use std::path::PathBuf;
28#[cfg(feature = "download")]
29use std::sync::{Arc, OnceLock};
30
31use crate::{
32 ToolCache, ToolCacheArch, ToolPlatform,
33 cache::{RETRY_COUNT, get_tool_cache_path},
34};
35
36#[derive(Debug, Clone, Default)]
40pub struct ToolCacheBuilder {
41 pub(crate) tool_cache: Option<PathBuf>,
42 pub(crate) arch: Option<crate::ToolCacheArch>,
43 pub(crate) platform: Option<crate::platform::ToolPlatform>,
44
45 pub(crate) retry_count: Option<u8>,
46 #[cfg(feature = "download")]
47 pub(crate) client: Option<reqwest::Client>,
48}
49
50impl ToolCacheBuilder {
51 pub fn new() -> Self {
53 Self::default()
54 }
55
56 pub fn tool_cache(mut self, path: impl Into<PathBuf>) -> Self {
61 self.tool_cache = Some(path.into());
62 self
63 }
64
65 pub fn arch(mut self, arch: crate::ToolCacheArch) -> Self {
70 self.arch = Some(arch);
71 self
72 }
73
74 pub fn platform(mut self, platform: crate::platform::ToolPlatform) -> Self {
79 self.platform = Some(platform);
80 self
81 }
82
83 pub fn retry_count(mut self, count: u8) -> Self {
88 self.retry_count = Some(count);
89 self
90 }
91
92 #[cfg(feature = "download")]
97 pub fn client(mut self, client: reqwest::Client) -> Self {
98 self.client = Some(client);
99 self
100 }
101
102 pub fn build(&self) -> ToolCache {
104 let tool_cache = self.tool_cache.clone().unwrap_or_else(get_tool_cache_path);
105 let arch = self.arch.unwrap_or(match std::env::consts::ARCH {
106 "x86_64" | "amd64" => ToolCacheArch::X64,
107 "aarch64" => ToolCacheArch::ARM64,
108 _ => ToolCacheArch::Any,
109 });
110
111 let platform = self.platform.unwrap_or_else(ToolPlatform::from_current_os);
112
113 #[cfg(feature = "download")]
114 let client = {
115 let client = OnceLock::new();
116 if let Some(reqwest_client) = self.client.clone() {
117 let _ = client.set(reqwest_client);
118 }
119 Arc::new(client)
120 };
121
122 ToolCache {
123 tool_cache,
124 arch,
125 platform,
126 retry_count: self.retry_count.unwrap_or(RETRY_COUNT),
127 #[cfg(feature = "download")]
128 client,
129 }
130 }
131}