ghactions_toolcache/
builder.rs

1//! # ToolCacheBuilder
2//!
3//! A builder for the ToolCache struct.
4//!
5//! This allows you to customize the ToolCache instance before creating it.
6//!
7//! # Example
8//!
9//! ```no_run
10//! # #[cfg(feature = "download")] {
11//! # use anyhow::Result;
12//! use ghactions_toolcache::ToolCache;
13//!
14//! # #[tokio::main]
15//! # async fn main() -> Result<()> {
16//!
17//! // Create a new ToolCache instance using the builder
18//! let tool_cache = ToolCache::build()
19//!     .retry_count(5)
20//!     .client(reqwest::Client::new())
21//!     .build();
22//!
23//! # Ok(())
24//! # }
25//! # }
26//! ```
27use std::path::PathBuf;
28
29use crate::{
30    ToolCache, ToolCacheArch, ToolPlatform,
31    cache::{RETRY_COUNT, get_tool_cache_path},
32};
33
34/// A builder for the ToolCache struct.
35///
36/// This allows you to customize the ToolCache instance before creating it.
37#[derive(Debug, Clone, Default)]
38pub struct ToolCacheBuilder {
39    pub(crate) tool_cache: Option<PathBuf>,
40    pub(crate) arch: Option<crate::ToolCacheArch>,
41    pub(crate) platform: Option<crate::platform::ToolPlatform>,
42
43    pub(crate) retry_count: Option<u8>,
44    #[cfg(feature = "download")]
45    pub(crate) client: Option<reqwest::Client>,
46}
47
48impl ToolCacheBuilder {
49    /// Create a new ToolCacheBuilder
50    pub fn new() -> Self {
51        Self::default()
52    }
53
54    /// Sets the path to the tool cache directory.
55    ///
56    /// # Parameters
57    /// - `path`: The path to use for the tool cache directory.
58    pub fn tool_cache(mut self, path: impl Into<PathBuf>) -> Self {
59        self.tool_cache = Some(path.into());
60        self
61    }
62
63    /// Sets the architecture for the tool cache.
64    ///
65    /// # Parameters
66    /// - `arch`: The architecture to use (e.g., x64, arm64).
67    pub fn arch(mut self, arch: crate::ToolCacheArch) -> Self {
68        self.arch = Some(arch);
69        self
70    }
71
72    /// Sets the platform for the tool cache.
73    ///
74    /// # Parameters
75    /// - `platform`: The platform to use (e.g., Windows, Linux, macOS).
76    pub fn platform(mut self, platform: crate::platform::ToolPlatform) -> Self {
77        self.platform = Some(platform);
78        self
79    }
80
81    /// Sets the number of retry attempts for cache operations.
82    ///
83    /// # Parameters
84    /// - `count`: The number of retries to attempt.
85    pub fn retry_count(mut self, count: u8) -> Self {
86        self.retry_count = Some(count);
87        self
88    }
89
90    /// Sets the HTTP client to use for downloading tools.
91    ///
92    /// # Parameters
93    /// - `client`: The `reqwest::Client` instance to use.
94    #[cfg(feature = "download")]
95    pub fn client(mut self, client: reqwest::Client) -> Self {
96        self.client = Some(client);
97        self
98    }
99
100    /// Build the ToolCache
101    pub fn build(&self) -> ToolCache {
102        let tool_cache = self.tool_cache.clone().unwrap_or_else(get_tool_cache_path);
103        let arch = self.arch.unwrap_or(match std::env::consts::ARCH {
104            "x86_64" | "amd64" => ToolCacheArch::X64,
105            "aarch64" => ToolCacheArch::ARM64,
106            _ => ToolCacheArch::Any,
107        });
108
109        let platform = self.platform.unwrap_or_else(ToolPlatform::from_current_os);
110
111        ToolCache {
112            tool_cache,
113            arch,
114            platform,
115            retry_count: self.retry_count.unwrap_or(RETRY_COUNT),
116            #[cfg(feature = "download")]
117            client: self.client.clone().unwrap_or_default(),
118        }
119    }
120}