Skip to content
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

[RFC] BREAKING CHANGES: A feature for every readout type (Closes #110) #111

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ build = "build.rs"

[dependencies]
cfg-if = "1.0.0"
libc = "0.2.112"
home = "0.5.3"
libc = { version = "0.2.112", optional = true }
home = { version = "0.5.3", optional = true }

[target.'cfg(not(target_os = "windows"))'.dependencies]
if-addrs = "0.7.0"
if-addrs = { version = "0.7.0", optional = true }

[target.'cfg(any(target_os="freebsd", target_os = "linux"))'.dependencies]
sqlite = "0.26.0"
sqlite = { version = "0.26.0", optional = true }

[target.'cfg(any(target_os="freebsd", target_os = "netbsd"))'.dependencies]
x11rb = "0.9.0"
x11rb = { version = "0.9.0", optional = true }

[target.'cfg(target_os = "linux")'.dependencies]
dirs = "4.0"
walkdir = "2.3.2"
os-release = "0.1"
dirs = { version = "4.0", optional = true }
walkdir = { version = "2.3.2", optional = true }
os-release = { version = "0.1", optional = true }

[target.'cfg(target_os = "netbsd")'.dependencies]
nix = "0.23.1"
Expand All @@ -39,7 +39,7 @@ core-graphics = "0.22.3"
mach = "0.3.2"

[target.'cfg(target_family = "unix")'.dependencies]
num_cpus = "1.13.1"
num_cpus = { version = "1.13.1", optional = true }

[target.'cfg(target_os = "windows")'.dependencies]
local-ip-address = "0.4.4"
Expand All @@ -54,10 +54,10 @@ windows = { version = "0.29.0", features = [
wmi = "0.9.2"

[target.'cfg(any(target_os = "linux", target_os = "netbsd", target_os = "android"))'.dependencies]
itertools = "0.10.3"
itertools = { version = "0.10.3", optional = true }

[target.'cfg(not(any(target_os = "netbsd", target_os = "windows")))'.dependencies]
sysctl = "0.4.3"
sysctl = { version = "0.4.3", optional = true }

[target.'cfg(any(target_os = "linux", target_os = "netbsd"))'.build-dependencies]
pkg-config = { version = "0.3.24", optional = true}
Expand All @@ -69,5 +69,16 @@ default-features = false
features = ["build","cargo","git","rustc"]

[features]
default = ["general", "kernel", "memory", "network", "package", "product", "graphical", "processor"]
general = ["libc", "os-release", "x11rb", "dirs", "sysctl"]
kernel = ["libc", "sysctl"]
memory = []
network = ["if-addrs"]
package = ["sqlite", "walkdir", "home"]
product = ["itertools"]
battery = []
processor = ["num_cpus", "libc"]
graphical = ["libc", "dirs"]

openwrt = []
hash = ["vergen"]
version = ["vergen", "pkg-config"]
4 changes: 3 additions & 1 deletion src/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl From<std::str::Utf8Error> for ReadoutError {
ReadoutError::Other(e.to_string())
}
}

impl From<std::num::ParseFloatError> for ReadoutError {
fn from(e: std::num::ParseFloatError) -> Self {
ReadoutError::Other(e.to_string())
Expand Down Expand Up @@ -237,6 +238,7 @@ impl GeneralReadout for AndroidGeneralReadout {
}
}
}

match (hardware, model, processor) {
(Some(hardware), _, _) => Ok(hardware),
(_, Some(model), _) => Ok(model),
Expand Down Expand Up @@ -436,7 +438,7 @@ impl AndroidPackageReadout {
};

let dpkg_dir = Path::new(&prefix).join("var/lib/dpkg/info");

extra::get_entries(&dpkg_dir).map(|entries| {
entries
.iter()
Expand Down
119 changes: 119 additions & 0 deletions src/enums.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use std::fmt;

/// This enum contains possible error types when doing sensor & variable readouts.
#[derive(Debug, Clone)]
pub enum ReadoutError {
/// A specific metric might not be available on all systems (e. g. battery percentage on a
/// desktop). \
/// If you encounter this error, it means that the requested value is not available.
MetricNotAvailable,

/// The default error for any readout that is not implemented by a particular platform.
NotImplemented,

/// A readout for a metric might be available, but fails due to missing dependencies or other
/// unsatisfied requirements.
Other(String),

/// Getting a readout on a specific operating system might not make sense or causes some other
/// kind of warning. This is not necessarily an error.
Warning(String),
}

impl ToString for ReadoutError {
fn to_string(&self) -> String {
match self {
ReadoutError::MetricNotAvailable => {
String::from("Metric is not available on this system.")
}
ReadoutError::NotImplemented => {
String::from("This metric is not available on this platform or is not yet implemented by libmacchina.")
}
ReadoutError::Other(s) => s.clone(),
ReadoutError::Warning(s) => s.clone(),
}
}
}

impl From<&ReadoutError> for ReadoutError {
fn from(r: &ReadoutError) -> Self {
r.to_owned()
}
}

/// Holds the possible variants for battery status.
pub enum BatteryState {
Charging,
Discharging,
}

impl From<BatteryState> for &'static str {
fn from(state: BatteryState) -> &'static str {
match state {
BatteryState::Charging => "Charging",
BatteryState::Discharging => "Discharging",
}
}
}

/// The currently running shell is a program, whose path
/// can be _relative_, or _absolute_.
#[derive(Debug)]
pub enum ShellFormat {
Relative,
Absolute,
}

#[derive(Debug)]
/// There are two distinct kinds of shells, a so called *"current"* shell, i.e. the shell the user is currently using.
/// And a default shell, i.e. that the user sets for themselves using the `chsh` tool.
pub enum ShellKind {
Current,
Default,
}

#[derive(Debug)]
/// The supported package managers whose packages can be extracted.
pub enum PackageManager {
Homebrew,
MacPorts,
Pacman,
Portage,
Dpkg,
Opkg,
Xbps,
Pkgsrc,
Apk,
Eopkg,
Rpm,
Cargo,
Flatpak,
Snap,
Android,
Pkg,
Scoop,
}

impl fmt::Display for PackageManager {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
PackageManager::Homebrew => write!(f, "homebrew"),
PackageManager::MacPorts => write!(f, "macports"),
PackageManager::Pacman => write!(f, "pacman"),
PackageManager::Portage => write!(f, "portage"),
PackageManager::Dpkg => write!(f, "dpkg"),
PackageManager::Opkg => write!(f, "opkg"),
PackageManager::Xbps => write!(f, "xbps"),
PackageManager::Pkgsrc => write!(f, "pkgsrc"),
PackageManager::Apk => write!(f, "apk"),
PackageManager::Eopkg => write!(f, "eopkg"),
PackageManager::Rpm => write!(f, "rpm"),
PackageManager::Cargo => write!(f, "cargo"),
PackageManager::Flatpak => write!(f, "flatpak"),
PackageManager::Snap => write!(f, "snap"),
PackageManager::Android => write!(f, "android"),
PackageManager::Pkg => write!(f, "pkg"),
PackageManager::Scoop => write!(f, "scoop"),
}
}
}
Loading