sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use crate::{Error, Uid};

use super::User;

/// A collection of users.
#[doc = include_str!("../../docs/snippets/user.md")]
pub struct Users {
    users: Vec<User>,
}

impl_collection_field!(Users, User, users);

impl From<Users> for Vec<User> {
    fn from(users: Users) -> Self {
        users.users
    }
}

impl Users {
    /// Creates an empty [`Users`].
    pub fn new() -> Result<Self, Error> {
        Ok(Self {
            users: crate::sys::new_users()?,
        })
    }

    impl_new_with_refreshed_list!(Users);

    /// Empties the user list, then recomputes it completely.
    pub fn refresh(&mut self) {
        crate::sys::get_users(&mut self.users);
    }

    /// Returns the [`User`] with the given `user_id`.
    pub fn get_user_by_id(&self, user_id: &Uid) -> Option<&User> {
        self.users.iter().find(|user| user.id() == user_id)
    }
}