os_info/info.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
// spell-checker:ignore itertools, iproduct, bitnesses
use std::fmt::{self, Display, Formatter};
use super::{Bitness, Type, Version};
/// Holds information about operating system (type, version, etc.).
///
/// The best way to get string representation of the operation system information is to use its
/// `Display` implementation.
///
/// # Examples
///
/// ```
/// use os_info;
///
/// let info = os_info::get();
/// println!("OS information: {}", info);
/// ```
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Info {
/// Operating system type. See `Type` for details.
pub(crate) os_type: Type,
/// Operating system version. See `Version` for details.
pub(crate) version: Version,
/// Operating system edition.
pub(crate) edition: Option<String>,
/// Operating system edition.
pub(crate) codename: Option<String>,
/// 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,
}
impl Info {
/// Constructs a new `Info` instance with unknown type, version and bitness.
///
/// # Examples
///
/// ```
/// use os_info::{Info, Type, Version, Bitness};
///
/// let info = Info::unknown();
/// assert_eq!(Type::Unknown, info.os_type());
/// assert_eq!(&Version::Unknown, info.version());
/// assert_eq!(None, info.edition());
/// assert_eq!(None, info.codename());
/// assert_eq!(Bitness::Unknown, info.bitness());
/// ```
pub fn unknown() -> Self {
Self {
os_type: Type::Unknown,
version: Version::Unknown,
edition: None,
codename: None,
bitness: Bitness::Unknown,
}
}
/// Constructs a new `Info` instance with the specified operating system type.
///
/// # Examples
///
/// ```
/// use os_info::{Info, Type, Version, Bitness};
///
/// let os_type = Type::Linux;
/// let info = Info::with_type(os_type);
/// assert_eq!(os_type, info.os_type());
/// assert_eq!(&Version::Unknown, info.version());
/// assert_eq!(None, info.edition());
/// assert_eq!(None, info.codename());
/// assert_eq!(Bitness::Unknown, info.bitness());
/// ```
pub fn with_type(os_type: Type) -> Self {
Self {
os_type,
..Default::default()
}
}
/// Returns operating system type. See `Type` for details.
///
/// # Examples
///
/// ```
/// use os_info::{Info, Type};
///
/// let info = Info::unknown();
/// assert_eq!(Type::Unknown, info.os_type());
/// ```
pub fn os_type(&self) -> Type {
self.os_type
}
/// Returns operating system version. See `Version` for details.
///
/// # Examples
///
/// ```
/// use os_info::{Info, Version};
///
/// let info = Info::unknown();
/// assert_eq!(&Version::Unknown, info.version());
/// ```
pub fn version(&self) -> &Version {
&self.version
}
/// Returns optional operation system edition.
///
/// # Examples
///
/// ```
/// use os_info::Info;
///
/// let info = Info::unknown();
/// assert_eq!(None, info.edition());
pub fn edition(&self) -> Option<&str> {
self.edition.as_ref().map(String::as_ref)
}
/// Returns optional operation system 'codename'.
///
/// # Examples
///
/// ```
/// use os_info::Info;
///
/// let info = Info::unknown();
/// assert_eq!(None, info.codename());
pub fn codename(&self) -> Option<&str> {
self.codename.as_ref().map(String::as_ref)
}
/// Returns operating system bitness. See `Bitness` for details.
///
/// # Examples
///
/// ```
/// use os_info::{Info, Bitness};
///
/// let info = Info::unknown();
/// assert_eq!(Bitness::Unknown, info.bitness());
/// ```
pub fn bitness(&self) -> Bitness {
self.bitness
}
}
impl Default for Info {
fn default() -> Self {
Self::unknown()
}
}
impl Display for Info {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.os_type)?;
if self.version != Version::Unknown {
write!(f, " {}", self.version)?;
}
if let Some(ref edition) = self.edition {
write!(f, " ({})", edition)?;
}
if let Some(ref codename) = self.codename {
write!(f, " ({})", codename)?;
}
write!(f, " [{}]", self.bitness)
}
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn unknown() {
let info = Info::unknown();
assert_eq!(Type::Unknown, info.os_type());
assert_eq!(&Version::Unknown, info.version());
assert_eq!(None, info.edition());
assert_eq!(None, info.codename());
assert_eq!(Bitness::Unknown, info.bitness());
}
#[test]
fn with_type() {
let types = [
Type::Redox,
Type::Alpine,
Type::Amazon,
Type::Android,
Type::Arch,
Type::CentOS,
Type::Debian,
Type::Emscripten,
Type::EndeavourOS,
Type::Fedora,
Type::Gentoo,
Type::Linux,
Type::Macos,
Type::Manjaro,
Type::Mariner,
Type::NixOS,
Type::openSUSE,
Type::OracleLinux,
Type::Pop,
Type::Redhat,
Type::RedHatEnterprise,
Type::Redox,
Type::Solus,
Type::SUSE,
Type::Ubuntu,
Type::Mint,
Type::Unknown,
Type::Windows,
];
for t in &types {
let info = Info::with_type(*t);
assert_eq!(t, &info.os_type());
}
}
#[test]
fn default() {
assert_eq!(Info::default(), Info::unknown());
}
#[test]
fn display() {
let data = [
// All unknown.
(Info::unknown(), "Unknown [unknown bitness]"),
// Type.
(
Info {
os_type: Type::Redox,
..Default::default()
},
"Redox [unknown bitness]",
),
// Type and version.
(
Info {
os_type: Type::Linux,
version: Version::Semantic(2, 3, 4),
..Default::default()
},
"Linux 2.3.4 [unknown bitness]",
),
(
Info {
os_type: Type::Arch,
version: Version::Rolling(None),
..Default::default()
},
"Arch Linux Rolling Release [unknown bitness]",
),
(
Info {
os_type: Type::Manjaro,
version: Version::Rolling(Some("2020.05.24".to_owned())),
..Default::default()
},
"Manjaro Rolling Release (2020.05.24) [unknown bitness]",
),
(
Info {
os_type: Type::Windows,
version: Version::Custom("Special Version".to_owned()),
..Default::default()
},
"Windows Special Version [unknown bitness]",
),
// Bitness.
(
Info {
bitness: Bitness::X32,
..Default::default()
},
"Unknown [32-bit]",
),
(
Info {
bitness: Bitness::X64,
..Default::default()
},
"Unknown [64-bit]",
),
// All info.
(
Info {
os_type: Type::Macos,
version: Version::Semantic(10, 2, 0),
edition: Some("edition".to_owned()),
codename: Some("codename".to_owned()),
bitness: Bitness::X64,
},
"Mac OS 10.2.0 (edition) (codename) [64-bit]",
),
];
for (info, expected) in &data {
assert_eq!(expected, &info.to_string());
}
}
}