1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* ARCH.rs
 *   by Lut99
 *
 * Created:
 *   22 May 2022, 17:35:56
 * Last edited:
 *   31 May 2022, 17:01:04
 * Auto updated?
 *   Yes
 *
 * Description:
 *   Defines enums and parsers to work with multiple architectures.
**/

use std::error::Error;
use std::fmt::{Display, Formatter, Result as FResult};
use std::hash::Hash;
use std::str::FromStr;

use serde::{Deserialize, Serialize};


/***** ERRORS *****/
/// Defines the error that may occur when parsing architectures
#[derive(Debug)]
pub enum ArchError {
    /// Could not deserialize the given string
    UnknownArchitecture { raw: String },
}
impl Display for ArchError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
        use ArchError::*;
        match self {
            UnknownArchitecture { raw } => write!(f, "Unknown architecture '{raw}'"),
        }
    }
}
impl Error for ArchError {}





/***** AUXILLARY *****/
/// A formatter for architectures that writes it in a way that Brane understands.
#[derive(Debug)]
pub struct ArchBraneFormatter<'a> {
    /// The architecture to format.
    arch: &'a Arch,
}
impl<'a> Display for ArchBraneFormatter<'a> {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
        match self.arch {
            Arch::X86_64 => write!(f, "x86_64"),
            Arch::Aarch64 => write!(f, "aarch64"),
        }
    }
}

/// Formatter that writes the given Arch in a way that the Rust compiler ecosystem understands.
#[derive(Debug)]
pub struct ArchRustFormatter<'a> {
    /// The architecture to format.
    arch: &'a Arch,
}
impl<'a> Display for ArchRustFormatter<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
        use Arch::*;
        match self.arch {
            X86_64 => write!(f, "x86_64"),
            Aarch64 => write!(f, "aarch64"),
        }
    }
}

/// Formatter that writes the given Arch in a way that the Docker ecosystem understands.
#[derive(Debug)]
pub struct ArchDockerFormatter<'a> {
    /// The architecture to format.
    arch: &'a Arch,
}
impl<'a> Display for ArchDockerFormatter<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
        use Arch::*;
        match self.arch {
            X86_64 => write!(f, "x86_64"),
            Aarch64 => write!(f, "aarch64"),
        }
    }
}

/// Formatter that writes the given Arch in a way that the JuiceFS ecosystem understands.
#[derive(Debug)]
pub struct ArchJuiceFsFormatter<'a> {
    /// The architecture to format.
    arch: &'a Arch,
}
impl<'a> Display for ArchJuiceFsFormatter<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
        use Arch::*;
        match self.arch {
            X86_64 => write!(f, "amd64"),
            Aarch64 => write!(f, "arm64"),
        }
    }
}

/// A formatter for architectures that writes it in a way that is used to download cfssl binaries.
#[derive(Debug)]
pub struct ArchCfsslFormatter<'a> {
    /// The architecture to format.
    os: &'a Arch,
}
impl<'a> Display for ArchCfsslFormatter<'a> {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
        match self.os {
            Arch::X86_64 => write!(f, "amd64"),
            Arch::Aarch64 => write!(f, "arm64"),
        }
    }
}





/***** LIBRARY *****/
/// The Arch enum defines possible architectures that we know of and love
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum Arch {
    /// The standard x86_64 architecture
    #[serde(alias = "amd64")]
    X86_64,
    /// The arm64 / macOS M1 architecture
    #[serde(alias = "arm64")]
    Aarch64,
}

impl Arch {
    /// Constant referring to the compiled (=host) architecture
    #[cfg(target_arch = "x86_64")]
    pub const HOST: Self = Self::X86_64;
    #[cfg(target_arch = "aarch64")]
    pub const HOST: Self = Self::Aarch64;

    /// Allows one to serialize the architecture for use in the Brane ecosystem.
    ///
    /// # Returns
    /// An `ArchBraneFormatter` that implements Display in a Brane-compatible way.
    #[inline]
    pub fn brane(&self) -> ArchBraneFormatter { ArchBraneFormatter { arch: self } }

    /// Allows one to serialize the architecture for use in the Rust ecosystem.
    ///
    /// # Returns
    /// An `ArchRustFormatter` that implements Display in a Rust-compatible way.
    #[inline]
    pub fn rust(&self) -> ArchRustFormatter { ArchRustFormatter { arch: self } }

    /// Allows one to serialize the architecture for use in the Docker ecosystem.
    ///
    /// # Returns
    /// An `ArchDockerFormatter` that implements Display in a Docker-compatible way.
    #[inline]
    pub fn docker(&self) -> ArchDockerFormatter { ArchDockerFormatter { arch: self } }

    /// Allows one to serialize the architecture for use in the Juice FS ecosystem.
    ///
    /// # Returns
    /// An `ArchJuiceFsFormatter` that implements Display in a Juice FS-compatible way.
    #[inline]
    pub fn juicefs(&self) -> ArchJuiceFsFormatter { ArchJuiceFsFormatter { arch: self } }

    /// Allows one to serialize the architecture for use to download cfssl binaries.
    ///
    /// # Returns
    /// An `ArchCfsslFormatter` that implements [`Display`]` in a cfssl-compatible way.
    #[inline]
    pub fn cfssl(&self) -> ArchCfsslFormatter { ArchCfsslFormatter { os: self } }
}

impl Display for Arch {
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
        match self {
            Arch::X86_64 => write!(f, "x86-64"),
            Arch::Aarch64 => write!(f, "ARM 64-bit"),
        }
    }
}
impl FromStr for Arch {
    type Err = ArchError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "x86_64" | "amd64" => Ok(Arch::X86_64),

            "aarch64" | "arm64" => Ok(Arch::Aarch64),

            // Meta-argument for resolving the local architecture
            #[cfg(target_arch = "x86_64")]
            "$LOCAL" => Ok(Self::X86_64),
            #[cfg(target_arch = "aarch64")]
            "$LOCAL" => Ok(Self::Aarch64),
            #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
            "$LOCAL" => {
                compile_error!("Non-x86/64, non-aarch64 processor architecture not supported");
            },

            raw => Err(ArchError::UnknownArchitecture { raw: raw.to_string() }),
        }
    }
}