Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit acc2a84

Browse files
authored
Merge pull request #22 from editorconfig-checker/enumerateArchitecture
Enumerate Architecture
2 parents 58ea158 + 20ae037 commit acc2a84

5 files changed

Lines changed: 57 additions & 35 deletions

File tree

src/architecture.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::str::FromStr;
2+
3+
use crate::error::{Error, Result};
4+
5+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
6+
pub enum Architecture {
7+
Amd64,
8+
I386,
9+
}
10+
11+
impl Architecture {
12+
pub fn stringify(self) -> &'static str {
13+
use Architecture::*;
14+
match self {
15+
Amd64 => "amd64",
16+
I386 => "386",
17+
}
18+
}
19+
}
20+
21+
impl FromStr for Architecture {
22+
type Err = Error;
23+
fn from_str(s: &str) -> Result<Self, Self::Err> {
24+
let lower = s.to_lowercase();
25+
match lower.as_str() {
26+
"x86_64" => Ok(Architecture::Amd64),
27+
// TODO: test if this actually matches
28+
"x86" => Ok(Architecture::I386),
29+
_ => Err(Error::ParseArch(lower)),
30+
}
31+
}
32+
}

src/checker.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::architecture::Architecture;
12
use crate::error::{Error, Result};
23
use crate::ostype::OsType;
34
use std::{fs, io, path::Path, path::PathBuf};
@@ -9,14 +10,21 @@ mod tests {
910
use flate2::write::GzEncoder;
1011
use flate2::Compression;
1112
use std::env;
13+
use std::env::consts;
1214
use std::fs::File;
1315
use tempfile::NamedTempFile;
1416

1517
#[test]
1618
fn test_generate_filename() {
17-
assert_eq!(generate_filename(OsType::Plan9, "def"), "ec-plan9-def");
19+
assert_eq!(
20+
generate_filename(OsType::Plan9, Architecture::Amd64),
21+
"ec-plan9-amd64"
22+
);
1823

19-
assert_eq!(generate_filename(OsType::Linux, "amd64"), "ec-linux-amd64");
24+
assert_eq!(
25+
generate_filename(OsType::Linux, Architecture::I386),
26+
"ec-linux-386"
27+
);
2028
}
2129

2230
#[test]
@@ -68,9 +76,10 @@ mod tests {
6876
fn test_download() -> Result<()> {
6977
let base_url = generate_base_url("2.0.3");
7078
let base_path = get_base_path(env::current_exe().unwrap()).unwrap();
71-
let os_type = sys_info::os_type()?.parse::<OsType>()?;
79+
let architecture = consts::ARCH.parse::<Architecture>()?;
80+
let os_type = consts::OS.parse::<OsType>()?;
7281

73-
let filename = generate_filename(os_type, get_architecture().unwrap());
82+
let filename = generate_filename(os_type, architecture);
7483

7584
let result = download(&base_url, &base_path, &filename);
7685
let tar_path = format!("{}/{}.tar.gz", base_path, filename);
@@ -118,25 +127,12 @@ mod tests {
118127
}
119128
}
120129

121-
// TODO: How to use cfg to pass a value into this function to be able to test it?
122-
// TODO: Test
123-
pub fn get_architecture() -> Result<&'static str> {
124-
// TODO: This is not sufficient and needs to care for more cases
125-
if cfg!(target_pointer_width = "64") {
126-
Ok("amd64")
127-
} else if cfg!(target_pointer_width = "32") {
128-
Ok("386")
129-
} else {
130-
Err(Error::UnknownArch)
131-
}
132-
}
133-
134130
pub fn path_exists(filename: impl AsRef<std::path::Path>) -> bool {
135131
filename.as_ref().exists()
136132
}
137133

138-
pub fn generate_filename(os: OsType, arch: &str) -> String {
139-
format!("ec-{}-{}", os.stringify(), arch)
134+
pub fn generate_filename(os: OsType, arch: Architecture) -> String {
135+
format!("ec-{}-{}", os.stringify(), arch.stringify())
140136
}
141137

142138
pub fn generate_base_url(version: &str) -> String {

src/error.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
55
#[derive(Debug)]
66
pub enum Error {
77
IO(std::io::Error),
8-
UnknownArch,
9-
UnknownOS(sys_info::Error),
108
ParseOS(String),
9+
ParseArch(String),
1110
InvalidPathName(std::ffi::OsString),
1211
Network(reqwest::Error),
1312
Encoding(std::str::Utf8Error),
@@ -20,9 +19,8 @@ impl fmt::Display for Error {
2019
use Error::*;
2120
match self {
2221
IO(err) => write!(fmt, "IO({})", err),
23-
UnknownArch => write!(fmt, "Unknown Architecture"),
24-
UnknownOS(err) => write!(fmt, "Unknown Operating System ({})", err),
2522
ParseOS(err) => write!(fmt, "Cannot parse Operating System Name ({})", err),
23+
ParseArch(err) => write!(fmt, "Cannot parse System Architecture ({})", err),
2624
InvalidPathName(err) => write!(fmt, "Invalid Path Name ({:?})", err),
2725
Network(err) => write!(fmt, "Error downloading the file ({})", err),
2826
Encoding(err) => write!(fmt, "Encoding error ({})", err),
@@ -38,12 +36,6 @@ impl From<std::io::Error> for Error {
3836
}
3937
}
4038

41-
impl From<sys_info::Error> for Error {
42-
fn from(err: sys_info::Error) -> Self {
43-
Error::UnknownOS(err)
44-
}
45-
}
46-
4739
impl From<std::ffi::OsString> for Error {
4840
fn from(err: std::ffi::OsString) -> Self {
4941
Error::InvalidPathName(err)

src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
mod architecture;
12
mod checker;
23
mod error;
34
mod ostype;
45

5-
use crate::{error::Result, ostype::OsType};
6+
use crate::{architecture::Architecture, error::Result, ostype::OsType};
67
use std::{
78
env,
9+
env::consts,
810
io::{self, Write},
911
process,
1012
};
1113

1214
fn main() -> Result<()> {
1315
let version = "2.0.3";
1416

15-
let architecture = checker::get_architecture()?;
16-
let os_type = sys_info::os_type()?;
17-
let os_type = os_type.parse::<OsType>()?;
17+
let architecture = consts::ARCH.parse::<Architecture>()?;
18+
let os_type = consts::OS.parse::<OsType>()?;
1819

1920
let base_path = checker::get_base_path(env::current_exe()?)?;
2021
let filename = checker::generate_filename(os_type, architecture);

src/ostype.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod tests {
1111
assert!("HALLO".parse::<OsType>().is_err());
1212
assert_eq!("Linux".parse::<OsType>().unwrap(), OsType::Linux);
1313
assert_eq!("Darwin".parse::<OsType>().unwrap(), OsType::Darwin);
14+
assert_eq!("macos".parse::<OsType>().unwrap(), OsType::Darwin);
1415
assert_eq!("WiNdOwS".parse::<OsType>().unwrap(), OsType::Windows);
1516
}
1617
}
@@ -51,12 +52,12 @@ impl FromStr for OsType {
5152
fn from_str(s: &str) -> Result<Self, Self::Err> {
5253
let lower = s.to_lowercase();
5354
match lower.as_str() {
55+
"linux" => Ok(OsType::Linux),
56+
"macos" | "darwin" => Ok(OsType::Darwin),
5457
// TODO: test if this actually matches
5558
"dragonfly" => Ok(OsType::Dragonfly),
5659
// TODO: test if this actually matches
5760
"freebsd" => Ok(OsType::FreeBSD),
58-
"linux" => Ok(OsType::Linux),
59-
"darwin" => Ok(OsType::Darwin),
6061
// TODO: test if this actually matches
6162
"netbsd" => Ok(OsType::NetBSD),
6263
// TODO: test if this actually matches

0 commit comments

Comments
 (0)