about_system/
lib.rs

1// TODO: no_std
2
3extern crate cpuid;
4#[macro_use]
5extern crate quick_error;
6extern crate semver;
7
8#[cfg(unix)]
9mod unix;
10
11#[cfg(unix)]
12use unix::{PlatformError, total_memory};
13#[cfg(unix)]
14pub use unix::os;
15
16#[cfg(windows)]
17mod windows;
18
19#[cfg(windows)]
20use windows::{PlatformError, total_memory};
21#[cfg(windows)]
22pub use windows::os;
23
24use semver::{SemVerError, Version};
25use std::{io, result};
26use std::error::Error as ErrorTrait;
27
28/// Information about the current operating system
29pub struct OSVersion {
30    /// Full name of the operating system
31    pub name: String,
32    // TODO: os_type
33    /// OS version info
34    pub version: Version,
35
36    #[cfg(unix)]
37    /// Distribution name
38    pub distribution: String,
39
40    #[cfg(windows)]
41    /// Service pack version
42    pub service_pack_version: Version,
43}
44
45/// Basic hardware information
46pub struct Platform {
47    // TODO: NUMA?
48    /// CPU name
49    pub cpu: String,
50    /// CPU vendor
51    pub cpu_vendor: String,
52
53    /// Total installed memory
54    pub memory: u64,
55}
56
57quick_error! {
58    #[derive(Debug)]
59    pub enum Error {
60        PlatformError(err: PlatformError) {
61            from()
62            description(err.description())
63            display("{}", err)
64        }
65
66        IO(err: io::Error) {
67            from()
68            description(err.description())
69            display(s) -> ("{}: {}", s.description(), err)
70        }
71
72        SemVer(err: SemVerError) {
73            from()
74            description(err.description())
75            display(s) -> ("{}: {}", s.description(), err)
76        }
77
78        String(err: String) {
79            from()
80            description(err)
81            display("{}", err)
82        }
83    }
84}
85
86pub type Result<T> = result::Result<T, Error>;
87
88/// Get basic information about the hardware we're running on.
89///
90/// # Requirements
91///
92/// For *nix: a functional `/proc` filesystem with system information. For
93/// Windows: XP or Server 2003.
94// TODO: don't show requirements for anothe system in the doc
95pub fn platform() -> Result<Platform> {
96    let cpu = cpuid::identify()?;
97
98    Ok(Platform {
99        cpu: cpu.brand,
100        cpu_vendor: cpu.vendor,
101
102        memory: total_memory(),
103    })
104}