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

Commit f673cb1

Browse files
committed
Extract OsType into own file
1 parent 3656738 commit f673cb1

3 files changed

Lines changed: 76 additions & 66 deletions

File tree

src/checker.rs

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::error::{Error, Result};
2-
use std::{fs, io, path::Path, path::PathBuf, str::FromStr};
2+
use crate::ostype::OsType;
3+
use std::{fs, io, path::Path, path::PathBuf};
34

45
#[cfg(test)]
56
mod tests {
@@ -115,70 +116,6 @@ mod tests {
115116
// check that the extracted file contains the same as the initial file
116117
assert_eq!(initinal_content, unpacked_content);
117118
}
118-
119-
#[test]
120-
fn test_parse_os_type() {
121-
assert!("HALLO".parse::<OsType>().is_err());
122-
assert_eq!("Linux".parse::<OsType>().unwrap(), OsType::Linux);
123-
assert_eq!("Darwin".parse::<OsType>().unwrap(), OsType::Darwin);
124-
assert_eq!("WiNdOwS".parse::<OsType>().unwrap(), OsType::Windows);
125-
}
126-
}
127-
128-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
129-
pub enum OsType {
130-
Darwin,
131-
Dragonfly,
132-
FreeBSD,
133-
Linux,
134-
NetBSD,
135-
OpenBSD,
136-
// currently there is no Plan9 target for Rust
137-
Plan9,
138-
Solaris,
139-
Windows,
140-
}
141-
142-
impl OsType {
143-
pub fn stringify(self) -> &'static str {
144-
use OsType::*;
145-
match self {
146-
Darwin => "darwin",
147-
Dragonfly => "dragonfly",
148-
FreeBSD => "freebsd",
149-
Linux => "linux",
150-
NetBSD => "netbsd",
151-
OpenBSD => "openbsd",
152-
Plan9 => "plan9",
153-
Solaris => "solaris",
154-
Windows => "windows",
155-
}
156-
}
157-
}
158-
159-
impl FromStr for OsType {
160-
type Err = Error;
161-
fn from_str(s: &str) -> Result<Self, Self::Err> {
162-
let lower = s.to_lowercase();
163-
match lower.as_str() {
164-
// TODO: test if this actually matches
165-
"dragonfly" => Ok(OsType::Dragonfly),
166-
// TODO: test if this actually matches
167-
"freebsd" => Ok(OsType::FreeBSD),
168-
"linux" => Ok(OsType::Linux),
169-
"darwin" => Ok(OsType::Darwin),
170-
// TODO: test if this actually matches
171-
"netbsd" => Ok(OsType::NetBSD),
172-
// TODO: test if this actually matches
173-
"openbsd" => Ok(OsType::OpenBSD),
174-
// TODO: test if this actually matches
175-
"plan9" => Ok(OsType::Plan9),
176-
// TODO: test if this actually matches
177-
"solaris" => Ok(OsType::Solaris),
178-
"windows" => Ok(OsType::Windows),
179-
_ => Err(Error::ParseOS(lower)),
180-
}
181-
}
182119
}
183120

184121
// TODO: How to use cfg to pass a value into this function to be able to test it?

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
mod checker;
22
mod error;
3+
mod ostype;
34

4-
use crate::{checker::OsType, error::Result};
5+
use crate::{error::Result, ostype::OsType};
56
use std::{
67
env,
78
io::{self, Write},

src/ostype.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use std::str::FromStr;
2+
3+
use crate::error::{Error, Result};
4+
5+
#[cfg(test)]
6+
mod tests {
7+
use super::*;
8+
9+
#[test]
10+
fn test_parse_os_type() {
11+
assert!("HALLO".parse::<OsType>().is_err());
12+
assert_eq!("Linux".parse::<OsType>().unwrap(), OsType::Linux);
13+
assert_eq!("Darwin".parse::<OsType>().unwrap(), OsType::Darwin);
14+
assert_eq!("WiNdOwS".parse::<OsType>().unwrap(), OsType::Windows);
15+
}
16+
}
17+
18+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
19+
pub enum OsType {
20+
Darwin,
21+
Dragonfly,
22+
FreeBSD,
23+
Linux,
24+
NetBSD,
25+
OpenBSD,
26+
// currently there is no Plan9 target for Rust
27+
Plan9,
28+
Solaris,
29+
Windows,
30+
}
31+
32+
impl OsType {
33+
pub fn stringify(self) -> &'static str {
34+
use OsType::*;
35+
match self {
36+
Darwin => "darwin",
37+
Dragonfly => "dragonfly",
38+
FreeBSD => "freebsd",
39+
Linux => "linux",
40+
NetBSD => "netbsd",
41+
OpenBSD => "openbsd",
42+
Plan9 => "plan9",
43+
Solaris => "solaris",
44+
Windows => "windows",
45+
}
46+
}
47+
}
48+
49+
impl FromStr for OsType {
50+
type Err = Error;
51+
fn from_str(s: &str) -> Result<Self, Self::Err> {
52+
let lower = s.to_lowercase();
53+
match lower.as_str() {
54+
// TODO: test if this actually matches
55+
"dragonfly" => Ok(OsType::Dragonfly),
56+
// TODO: test if this actually matches
57+
"freebsd" => Ok(OsType::FreeBSD),
58+
"linux" => Ok(OsType::Linux),
59+
"darwin" => Ok(OsType::Darwin),
60+
// TODO: test if this actually matches
61+
"netbsd" => Ok(OsType::NetBSD),
62+
// TODO: test if this actually matches
63+
"openbsd" => Ok(OsType::OpenBSD),
64+
// TODO: test if this actually matches
65+
"plan9" => Ok(OsType::Plan9),
66+
// TODO: test if this actually matches
67+
"solaris" => Ok(OsType::Solaris),
68+
"windows" => Ok(OsType::Windows),
69+
_ => Err(Error::ParseOS(lower)),
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)