-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement OS Family feature #313
base: master
Are you sure you want to change the base?
Changes from 10 commits
9dc4d43
c64ded9
80d9645
a331c81
8a3e250
5f943cc
8d36e39
c8b0378
2965c42
abfe975
519e05d
94e450b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::fmt::{self, Display, Formatter}; | ||
|
||
/// A general category for operating system to place them into 'families' | ||
/// Example of use case is when program logic needs to perform an operation | ||
/// on linux, but does not care which distro it is. | ||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
#[allow(non_camel_case_types, clippy::upper_case_acronyms)] | ||
#[non_exhaustive] | ||
pub enum Family { | ||
/// Berkely Standard Distributions | ||
/// https://en.wikipedia.org/wiki/Berkeley_Software_Distribution | ||
BSD, | ||
/// Linux Operating systems of all type | ||
/// https://en.wikipedia.org/wiki/Linux | ||
Linux, | ||
/// Apple's MacOS | ||
/// https://en.wikipedia.org/wiki/Macintosh_operating_systems | ||
MacOS, | ||
/// NT based operatings system | ||
/// https://en.wikipedia.org/wiki/Windows_NT | ||
WindowsNT, | ||
/// SunOS and OSs derived from SunOS such as Illumos | ||
/// https://en.wikipedia.org/wiki/SunOS | ||
SunOS, | ||
/// Operating systems whose family is unknown | ||
Unknown, | ||
} | ||
|
||
impl Default for Family { | ||
fn default() -> Self { | ||
Family::Unknown | ||
} | ||
} | ||
|
||
impl Display for Family { | ||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||
match *self { | ||
Family::BSD => write!(f, "BSD"), | ||
Family::Linux => write!(f, "Linux"), | ||
Family::MacOS => write!(f, "MacOS"), | ||
Family::SunOS => write!(f, "SunOS"), | ||
Family::WindowsNT => write!(f, "Windows NT"), | ||
_ => write!(f, "{:?}", self), | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
use std::fmt::{self, Display, Formatter}; | ||
|
||
use super::{Bitness, Type, Version}; | ||
use super::{Bitness, Family, Type, Version}; | ||
|
||
/// Holds information about operating system (type, version, etc.). | ||
/// | ||
|
@@ -31,6 +31,8 @@ pub struct Info { | |
/// Operating system architecture in terms of how many bits compose the basic values it can deal | ||
/// with. See `Bitness` for details. | ||
pub(crate) bitness: Bitness, | ||
/// Get the family of operating system. See "Family" for details. | ||
pub(crate) family: Family, | ||
} | ||
|
||
impl Info { | ||
|
@@ -55,6 +57,7 @@ impl Info { | |
edition: None, | ||
codename: None, | ||
bitness: Bitness::Unknown, | ||
family: Family::Unknown, | ||
} | ||
} | ||
|
||
|
@@ -147,6 +150,20 @@ impl Info { | |
pub fn bitness(&self) -> Bitness { | ||
self.bitness | ||
} | ||
|
||
/// Returns the operating system family. See 'Family' for details. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use os_info::{Info, Family}; | ||
/// | ||
/// let info = Info::unknown(); | ||
/// assert_eq!(Family::Unknown, info.family()); | ||
/// ``` | ||
pub fn family(&self) -> Family { | ||
self.family | ||
} | ||
Comment on lines
+164
to
+166
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose it makes sense to use match here instead of storing family as a separate field. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the recommendation here is something like, in a short hand version match Type {
Type::Debian => Family::Linux,
Type::Gentoo => Family::Linux,
Type::FreeBSD => Family::BSD,
Type::Windows => Family::DOS,
// and so forth
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I had exactly this in mind. Simply put this match into the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For BSDs, Windows, and MacOS, that is fine since those categories do not expand often. But for the Linux, this would require everytime a new distro is added as a Type, then this match would have to expand and add another arm. The current method should scale with any number of distros without having to update this function or add a new arm to a match. That is at least my current thought process. This would be the low maintenance way to approach this. |
||
} | ||
|
||
impl Default for Info { | ||
|
@@ -299,6 +316,7 @@ mod tests { | |
edition: Some("edition".to_owned()), | ||
codename: Some("codename".to_owned()), | ||
bitness: Bitness::X64, | ||
family: Family::MacOS, | ||
}, | ||
"Mac OS 10.2.0 (edition) (codename) [64-bit]", | ||
), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ use std::{fs::File, io::Read}; | |
|
||
use log::{error, trace}; | ||
|
||
use crate::{Bitness, Info, Type, Version}; | ||
use crate::{Bitness, Family, Info, Type, Version}; | ||
|
||
const UNAME_FILE: &str = "sys:uname"; | ||
|
||
|
@@ -18,6 +18,7 @@ pub fn current_platform() -> Info { | |
os_type: Type::Redox, | ||
version, | ||
bitness: Bitness::Unknown, | ||
family: Family::WindowsNT, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also seems weird to me. 🙃 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this one, @kraileth suggested changing since Windows is no longer truly "DOS," and I guess that has been true since the 90s. So calling it in the "DOS" family is misleading. He recommended changing it to either something NT since NT is the kernel architecture or just having the family also be Windows. I don't really know enough about Windows and its architecture to really know either way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a thought here: the |
||
..Default::default() | ||
}; | ||
trace!("Returning {:?}", info); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it is useful to show the family here? I'm a little afraid for the output to become too verbose. Additionally it can be a little weird in some cases. For example, currently it will be "OS Windows, Family: Unknown".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can remove that out. Probably not so useful on output, but I also assumed the output here was probably mostly for eyes on validation since os_info's biggest use case is probably as a library to gather the data needed and the caller would just grab the info and the fields it needs.
For Windows, the family should appear as DOS.
Mhmm, that is odd. I got that output from my work machine which is Windows 11, but shows the version as Windows 10.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to this information it is correct, but I agree that it can be confusing. Ideally, it should be something like "Windows 11 (10.0.22000)".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, yea that is confusing. You would think you see "10" as a major number for versioning, it specifically would reference Windows 10 since they changed from their random naming scheme of XP, Vista, Millennium to 7, 8, 10, 11.