sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use crate::support::impl_get_set::impl_get_set;

use super::Pid;

/// Specifies when related information is updated.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum UpdateKind {
    /// Never updates the related information.
    #[default]
    Never,
    /// Always updates the related information.
    Always,
    /// Updates the related information only if it was not set at least once.
    OnlyIfNotSet,
}

impl UpdateKind {
    /// If `self` is `OnlyIfNotSet`, calls `f` and returns its value.
    #[allow(dead_code)] // Required for unsupported targets.
    pub(crate) fn needs_update(self, f: impl Fn() -> bool) -> bool {
        match self {
            Self::Never => false,
            Self::Always => true,
            Self::OnlyIfNotSet => f(),
        }
    }
}

/// Specifies whether all processes or only some of them are updated.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ProcessesToUpdate<'a> {
    /// Refreshes all processes.
    All,
    /// Refreshes only the processes with the listed [`Pid`].
    Some(&'a [Pid]),
}

/// Selects which process fields are refreshed.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ProcessRefreshKind {
    cpu: bool,
    disk_usage: bool,
    memory: bool,
    user: UpdateKind,
    cwd: UpdateKind,
    root: UpdateKind,
    environ: UpdateKind,
    cmd: UpdateKind,
    exe: UpdateKind,
    tasks: bool,
    gpu_usage: bool,
    gpu_memory: bool,
}

/// Creates a `ProcessRefreshKind` with every refresh disabled.
impl Default for ProcessRefreshKind {
    fn default() -> Self {
        Self {
            cpu: false,
            disk_usage: false,
            memory: false,
            user: UpdateKind::default(),
            cwd: UpdateKind::default(),
            root: UpdateKind::default(),
            environ: UpdateKind::default(),
            cmd: UpdateKind::default(),
            exe: UpdateKind::default(),
            tasks: false,
            gpu_usage: false,
            gpu_memory: false,
        }
    }
}

impl ProcessRefreshKind {
    /// Creates a `ProcessRefreshKind` with every refresh disabled.
    pub fn nothing() -> Self {
        Self::default()
    }

    /// Creates a `ProcessRefreshKind` with every refresh enabled except tasks.
    pub fn everything() -> Self {
        Self {
            cpu: true,
            disk_usage: true,
            memory: true,
            user: UpdateKind::OnlyIfNotSet,
            cwd: UpdateKind::OnlyIfNotSet,
            root: UpdateKind::OnlyIfNotSet,
            environ: UpdateKind::OnlyIfNotSet,
            cmd: UpdateKind::OnlyIfNotSet,
            exe: UpdateKind::OnlyIfNotSet,
            tasks: false,
            gpu_usage: true,
            gpu_memory: true,
        }
    }

    impl_get_set!(
        ProcessRefreshKind,
        cpu,
        with_cpu,
        without_cpu,
        "\
It will retrieve both CPU usage and CPU accumulated time,"
    );
    impl_get_set!(
        ProcessRefreshKind,
        disk_usage,
        with_disk_usage,
        without_disk_usage
    );
    impl_get_set!(
        ProcessRefreshKind,
        user,
        with_user,
        without_user,
        UpdateKind,
        "\
It will retrieve the following information:

 * user ID
 * user effective ID (if available on the platform)
 * user group ID (if available on the platform)
 * user effective ID (if available on the platform)"
    );
    impl_get_set!(ProcessRefreshKind, memory, with_memory, without_memory);
    impl_get_set!(ProcessRefreshKind, cwd, with_cwd, without_cwd, UpdateKind);
    impl_get_set!(
        ProcessRefreshKind,
        root,
        with_root,
        without_root,
        UpdateKind
    );
    impl_get_set!(
        ProcessRefreshKind,
        environ,
        with_environ,
        without_environ,
        UpdateKind
    );
    impl_get_set!(ProcessRefreshKind, cmd, with_cmd, without_cmd, UpdateKind);
    impl_get_set!(ProcessRefreshKind, exe, with_exe, without_exe, UpdateKind);
    impl_get_set!(ProcessRefreshKind, tasks, with_tasks, without_tasks);
    impl_get_set!(
        ProcessRefreshKind,
        gpu_usage,
        with_gpu_usage,
        without_gpu_usage
    );
    impl_get_set!(
        ProcessRefreshKind,
        gpu_memory,
        with_gpu_memory,
        without_gpu_memory
    );
}

/// Selects which CPU fields are refreshed.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct CpuRefreshKind {
    usage: bool,
    frequency: bool,
}

impl CpuRefreshKind {
    /// Creates a `CpuRefreshKind` with every refresh set to `false`.
    pub fn nothing() -> Self {
        Self::default()
    }

    /// Creates a `CpuRefreshKind` with every refresh set to `true`.
    pub fn everything() -> Self {
        Self {
            usage: true,
            frequency: true,
        }
    }

    impl_get_set!(CpuRefreshKind, usage, with_usage, without_usage);
    impl_get_set!(CpuRefreshKind, frequency, with_frequency, without_frequency);
}

/// Specifies which memory to refresh.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct MemoryRefreshKind {
    ram: bool,
    swap: bool,
}

impl MemoryRefreshKind {
    /// Creates a `MemoryRefreshKind` with every refresh set to `false`.
    pub fn nothing() -> Self {
        Self::default()
    }

    /// Creates a `MemoryRefreshKind` with every refresh set to `true`.
    pub fn everything() -> Self {
        Self {
            ram: true,
            swap: true,
        }
    }

    impl_get_set!(MemoryRefreshKind, ram, with_ram, without_ram);
    impl_get_set!(MemoryRefreshKind, swap, with_swap, without_swap);
}

/// Specifies what to refresh on the [`System`][crate::System] type.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[doc = include_str!("../../docs/snippets/refresh.md")]
pub struct RefreshKind {
    processes: Option<ProcessRefreshKind>,
    memory: Option<MemoryRefreshKind>,
    cpu: Option<CpuRefreshKind>,
}

impl RefreshKind {
    /// Creates a `RefreshKind` with every refresh set to `false` or `None`.
    pub fn nothing() -> Self {
        Self::default()
    }

    /// Creates a `RefreshKind` with every refresh set to `true` or `Some(...)`.
    pub fn everything() -> Self {
        Self {
            processes: Some(ProcessRefreshKind::everything()),
            memory: Some(MemoryRefreshKind::everything()),
            cpu: Some(CpuRefreshKind::everything()),
        }
    }

    impl_get_set!(
        RefreshKind,
        processes,
        with_processes,
        without_processes,
        ProcessRefreshKind
    );
    impl_get_set!(
        RefreshKind,
        memory,
        with_memory,
        without_memory,
        MemoryRefreshKind
    );
    impl_get_set!(RefreshKind, cpu, with_cpu, without_cpu, CpuRefreshKind);
}