Formatting, add Default trait to Machine struct

This commit is contained in:
Lucien Cartier-Tilet 2021-12-17 17:20:05 +01:00
parent bb44f85e42
commit ca6515fad4
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA

View File

@ -15,18 +15,22 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use serde::Serialize;
use sysinfo::{System, SystemExt, DiskExt};
use sysinfo::{DiskExt, System, SystemExt};
#[derive(Debug, Serialize)]
pub struct Memory {
pub name: String,
pub size: u64,
pub available: u64
pub available: u64,
}
impl Memory {
pub fn new(name: String, size: u64, available: u64) -> Self {
Self { name, size, available }
Self {
name,
size,
available,
}
}
pub fn used(&self) -> u64 {
@ -64,8 +68,18 @@ impl Machine {
)
})
.collect::<Vec<Memory>>();
let ram = Memory::new("RAM".into(), sys.total_memory() * 1000, sys.available_memory() * 1000);
let swap = Memory::new("Swap".into(), sys.total_swap() * 1000, sys.free_swap() * 1000);
// Size of RAM and Swap is returned in KB, unlike the drives
// size which is in byte
let ram = Memory::new(
"RAM".into(),
sys.total_memory() * 1000,
sys.available_memory() * 1000,
);
let swap = Memory::new(
"Swap".into(),
sys.total_swap() * 1000,
sys.free_swap() * 1000,
);
Self {
os: sys.name().unwrap(),
kernel: sys.kernel_version().unwrap(),
@ -73,7 +87,13 @@ impl Machine {
uptime: sys.uptime(),
ram,
swap,
disks
disks,
}
}
}
impl Default for Machine {
fn default() -> Self {
Self::new()
}
}