Skip to content

Commit

Permalink
Adding information about architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
rbas committed Feb 13, 2023
1 parent 7011ce4 commit 1d4b260
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -51,6 +51,7 @@ println!("OS information: {info}");
println!("Type: {}", info.os_type());
println!("Version: {}", info.version());
println!("Bitness: {}", info.bitness());
println!("Architecture: {}", info.architecture());
```

### Command line tool (`os_info_cli`)
Expand Down
1 change: 1 addition & 0 deletions os_info/examples/print_version.rs
Expand Up @@ -10,4 +10,5 @@ fn main() {
println!("Edition: {:?}", info.edition());
println!("Codename: {:?}", info.codename());
println!("Bitness: {}", info.bitness());
println!("Architecture: {:?}", info.architecture());
}
32 changes: 32 additions & 0 deletions os_info/src/architecture.rs
@@ -0,0 +1,32 @@
use std::process::Command;

use log::error;

pub fn architecture() -> Option<String> {
Command::new("uname")
.arg("-m")
.output()
.map_err(|e| {
error!("Cannot invoke 'uname` to get architecture type: {:?}", e);
})
.ok()
.and_then(|out| {
if out.status.success() {
Some(String::from_utf8_lossy(&out.stdout).trim_end().to_owned())
} else {
log::error!("'uname' invocation error: {:?}", out);
None
}
})
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn uname_nonempty() {
let val = architecture().expect("uname failed");
assert!(!val.is_empty());
}
}
20 changes: 20 additions & 0 deletions os_info/src/info.rs
Expand Up @@ -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,
// Processor architecture.
pub(crate) architecture: Option<String>,
}

impl Info {
Expand All @@ -47,6 +49,7 @@ impl Info {
/// assert_eq!(None, info.edition());
/// assert_eq!(None, info.codename());
/// assert_eq!(Bitness::Unknown, info.bitness());
/// assert_eq!(None, info.architecture());
/// ```
pub fn unknown() -> Self {
Self {
Expand All @@ -55,6 +58,7 @@ impl Info {
edition: None,
codename: None,
bitness: Bitness::Unknown,
architecture: None,
}
}

Expand All @@ -72,6 +76,7 @@ impl Info {
/// assert_eq!(None, info.edition());
/// assert_eq!(None, info.codename());
/// assert_eq!(Bitness::Unknown, info.bitness());
/// assert_eq!(None, info.architecture());
/// ```
pub fn with_type(os_type: Type) -> Self {
Self {
Expand Down Expand Up @@ -147,6 +152,19 @@ impl Info {
pub fn bitness(&self) -> Bitness {
self.bitness
}

/// Returns operating system architecture.
///
/// # Examples
///
/// ```
/// use os_info::Info;
///
/// let info = Info::unknown();
/// assert_eq!(None, info.architecture());
pub fn architecture(&self) -> Option<&str> {
self.architecture.as_ref().map(String::as_ref)
}
}

impl Default for Info {
Expand Down Expand Up @@ -184,6 +202,7 @@ mod tests {
assert_eq!(None, info.edition());
assert_eq!(None, info.codename());
assert_eq!(Bitness::Unknown, info.bitness());
assert_eq!(None, info.architecture());
}

#[test]
Expand Down Expand Up @@ -301,6 +320,7 @@ mod tests {
edition: Some("edition".to_owned()),
codename: Some("codename".to_owned()),
bitness: Bitness::X64,
architecture: Some("architecture".to_owned()),
},
"Mac OS 10.2.0 (edition) (codename) [64-bit]",
),
Expand Down
2 changes: 2 additions & 0 deletions os_info/src/lib.rs
Expand Up @@ -70,6 +70,7 @@ mod imp;
#[path = "unknown/mod.rs"]
mod imp;

mod architecture;
mod bitness;
mod info;
#[cfg(not(windows))]
Expand Down Expand Up @@ -105,6 +106,7 @@ pub use crate::{bitness::Bitness, info::Info, os_type::Type, version::Version};
/// println!("Edition: {:?}", info.edition());
/// println!("Codename: {:?}", info.codename());
/// println!("Bitness: {}", info.bitness());
/// println!("Architecture: {:?}", info.architecture());
/// ```
pub fn get() -> Info {
imp::current_platform()
Expand Down
3 changes: 2 additions & 1 deletion os_info/src/linux/mod.rs
Expand Up @@ -3,7 +3,7 @@ mod lsb_release;

use log::trace;

use crate::{bitness, Info, Type};
use crate::{architecture, bitness, Info, Type};

pub fn current_platform() -> Info {
trace!("linux::current_platform is called");
Expand All @@ -12,6 +12,7 @@ pub fn current_platform() -> Info {
.or_else(file_release::get)
.unwrap_or_else(|| Info::with_type(Type::Linux));
info.bitness = bitness::get();
info.architecture = architecture::architecture();

trace!("Returning {:?}", info);
info
Expand Down
3 changes: 2 additions & 1 deletion os_info/src/macos/mod.rs
Expand Up @@ -2,7 +2,7 @@ use std::process::Command;

use log::{trace, warn};

use crate::{bitness, matcher::Matcher, Info, Type, Version};
use crate::{architecture::architecture, bitness, matcher::Matcher, Info, Type, Version};

pub fn current_platform() -> Info {
trace!("macos::current_platform is called");
Expand All @@ -11,6 +11,7 @@ pub fn current_platform() -> Info {
os_type: Type::Macos,
version: version(),
bitness: bitness::get(),
architecture: architecture(),
..Default::default()
};
trace!("Returning {:?}", info);
Expand Down
3 changes: 2 additions & 1 deletion os_info/src/netbsd/mod.rs
Expand Up @@ -2,7 +2,7 @@ use std::process::Command;

use log::{error, trace};

use crate::{bitness, uname::uname, Info, Type, Version};
use crate::{architecture::architecture, bitness, uname::uname, Info, Type, Version};

pub fn current_platform() -> Info {
trace!("netbsd::current_platform is called");
Expand All @@ -15,6 +15,7 @@ pub fn current_platform() -> Info {
os_type: Type::NetBSD,
version,
bitness: bitness::get(),
architecture: architecture::architecture(),
..Default::default()
};

Expand Down
3 changes: 2 additions & 1 deletion os_info/src/openbsd/mod.rs
Expand Up @@ -2,7 +2,7 @@ use std::process::Command;

use log::{error, trace};

use crate::{bitness, uname::uname, Info, Type, Version};
use crate::{architecture::architecture, bitness, uname::uname, Info, Type, Version};

pub fn current_platform() -> Info {
trace!("openbsd::current_platform is called");
Expand All @@ -15,6 +15,7 @@ pub fn current_platform() -> Info {
os_type: Type::OpenBSD,
version,
bitness: bitness::get(),
architecture: architecture::architecture(),
..Default::default()
};

Expand Down

0 comments on commit 1d4b260

Please sign in to comment.