1use crate::{Error, Result};
4use std::path::Path;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum OS {
9 Linux,
11 Macos,
13 Windows,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq)]
19pub enum Arch {
20 X86_64,
22 Arm64,
24}
25
26#[derive(Debug, Clone, PartialEq, Eq)]
28pub enum InstallerKind {
29 AppImage,
31 Deb,
33 Rpm,
35 AppTarGz,
37 AppZip,
39 Msi,
41 Nsis,
43}
44
45#[derive(Debug, Clone, PartialEq, Eq)]
47pub struct SystemInfo {
48 pub os: OS,
50 pub arch: Arch,
52}
53
54#[derive(Debug, Clone, PartialEq, Eq)]
56pub struct TargetInfo {
57 pub target: String,
59 pub system: SystemInfo,
61}
62
63impl TargetInfo {
64 pub fn from_system(system: SystemInfo) -> Self {
68 let os = match system.os {
69 OS::Linux => "linux",
70 OS::Macos => "darwin",
71 OS::Windows => "windows",
72 };
73 let arch = match system.arch {
74 Arch::X86_64 => "x86_64",
75 Arch::Arm64 => "aarch64",
76 };
77 Self {
78 target: format!("{os}-{arch}"),
79 system,
80 }
81 }
82}
83
84impl SystemInfo {
85 pub fn current() -> Result<Self> {
89 let os = if cfg!(target_os = "linux") {
90 OS::Linux
91 } else if cfg!(target_os = "macos") {
92 OS::Macos
93 } else if cfg!(target_os = "windows") {
94 OS::Windows
95 } else {
96 return Err(Error::UnsupportedOs);
97 };
98 let arch = if cfg!(target_arch = "x86_64") {
99 Arch::X86_64
100 } else if cfg!(target_arch = "aarch64") {
101 Arch::Arm64
102 } else {
103 return Err(Error::UnsupportedArch);
104 };
105 Ok(Self { os, arch })
106 }
107}
108
109impl InstallerKind {
110 pub fn from_path(path: &Path) -> Result<Self> {
115 let name = path
116 .file_name()
117 .and_then(|name| name.to_str())
118 .unwrap_or_default();
119 if name.ends_with(".AppImage") {
120 Ok(Self::AppImage)
121 } else if name.ends_with(".deb") {
122 Ok(Self::Deb)
123 } else if name.ends_with(".rpm") {
124 Ok(Self::Rpm)
125 } else if name.ends_with(".app.tar.gz") {
126 Ok(Self::AppTarGz)
127 } else if name.ends_with(".app.zip") {
128 Ok(Self::AppZip)
129 } else if name.ends_with(".msi") {
130 Ok(Self::Msi)
131 } else if name.ends_with(".exe") {
132 Ok(Self::Nsis)
133 } else {
134 Err(Error::InvalidUpdaterFormat)
135 }
136 }
137}