Skip to main content

etcd_bin_vendored/
lib.rs

1#![doc = include_str!("../README.md")]
2
3use std::{error, fmt, path::Path};
4
5/// Error returned when `etcd` is not supported on the requested OS and architecture.
6pub struct ArchitectureNotSupported {
7    inner: ArchitectureNotSupportedInner,
8}
9
10enum ArchitectureNotSupportedInner {
11    Unknown { os: &'static str, arch: &'static str },
12}
13
14impl fmt::Display for ArchitectureNotSupported {
15    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16        match self.inner {
17            ArchitectureNotSupportedInner::Unknown { os, arch } => write!(f, "etcd not supported for {os}-{arch}"),
18        }
19    }
20}
21
22impl fmt::Debug for ArchitectureNotSupported {
23    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24        match self.inner {
25            ArchitectureNotSupportedInner::Unknown { os, arch } => f
26                .debug_struct("ArchitectureNotSupported::Unknown")
27                .field("os", &os)
28                .field("arch", &arch)
29                .finish(),
30        }
31    }
32}
33
34impl error::Error for ArchitectureNotSupported {}
35
36macro_rules! match_platform {
37    ($(($os:literal, $arch:literal, $module:ident)),* $(,)?) => {
38        match (::std::env::consts::OS, ::std::env::consts::ARCH) {
39            $(
40                #[cfg(all(target_os = $os, target_arch = $arch))]
41                ($os, $arch) => $module::etcd_bin_path().map_err(|_| unreachable!()),
42            )*
43            (os, arch) => Err(ArchitectureNotSupported {
44                inner: ArchitectureNotSupportedInner::Unknown { os, arch }
45            })
46        }
47    }
48}
49
50/// Return a path to the `etcd` binary for this platform.
51///
52/// This function returns the path to the `etcd` program when it is supported on the current platform. In the case of an
53/// unsupported platform, an `Err(ArchitectureNotSupported)` is returned with a description of the error.
54pub fn etcd_bin_path() -> Result<&'static Path, ArchitectureNotSupported> {
55    match_platform! {
56        ("linux",    "x86_64",    etcd_bin_vendored_linux_amd64),
57        ("linux",    "aarch64",   etcd_bin_vendored_linux_arm64),
58        ("linux",    "powerpc64", etcd_bin_vendored_linux_ppc64le),
59        ("linux",    "s390x",     etcd_bin_vendored_linux_s390x),
60        ("macos",    "x86_64",    etcd_bin_vendored_darwin_amd64),
61        ("macos",    "aarch64",   etcd_bin_vendored_darwin_arm64),
62        ("windows",  "x86_64",    etcd_bin_vendored_windows_amd64),
63        ("windows",  "aarch64",   etcd_bin_vendored_windows_amd64),
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::etcd_bin_path;
70
71    #[test]
72    fn path_exists() {
73        etcd_bin_path().expect("this should work on the platform");
74    }
75}