Skip to main content

ghactions_toolcache/
arch.rs

1//! # Tool Cache CPU Architecture
2
3use std::fmt::Display;
4
5/// Tool Cache CPU Architecture enum
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum ToolCacheArch {
8    /// amd64/x86_64
9    X64,
10    /// Arm64
11    ARM64,
12    /// Any other architecture
13    Any,
14}
15
16impl From<&ToolCacheArch> for ToolCacheArch {
17    fn from(arch: &ToolCacheArch) -> Self {
18        *arch
19    }
20}
21
22impl From<String> for ToolCacheArch {
23    fn from(arch: String) -> Self {
24        match arch.to_lowercase().as_str() {
25            "x64" => ToolCacheArch::X64,
26            "arm64" => ToolCacheArch::ARM64,
27            _ => ToolCacheArch::Any,
28        }
29    }
30}
31
32impl From<&str> for ToolCacheArch {
33    fn from(arch: &str) -> Self {
34        arch.to_string().into()
35    }
36}
37
38impl From<&String> for ToolCacheArch {
39    fn from(value: &String) -> Self {
40        value.clone().into()
41    }
42}
43
44impl Display for ToolCacheArch {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        match self {
47            ToolCacheArch::X64 => write!(f, "x64"),
48            ToolCacheArch::ARM64 => write!(f, "arm64"),
49            ToolCacheArch::Any => write!(f, "**"),
50        }
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_toolcache_arch() {
60        let x64 = ToolCacheArch::X64;
61        let arm64 = ToolCacheArch::ARM64;
62        let any = ToolCacheArch::Any;
63
64        assert_eq!(x64.to_string(), "x64");
65        assert_eq!(arm64.to_string(), "arm64");
66        assert_eq!(any.to_string(), "**");
67
68        let x64_str = "x64".to_string();
69        let arm64_str = "arm64".to_string();
70        let any_str = "**".to_string();
71
72        assert_eq!(x64, x64_str.into());
73        assert_eq!(arm64, arm64_str.into());
74        assert_eq!(any, any_str.into());
75    }
76}