ferrix_lib/
lib.rs

1/* lib.rs
2 *
3 * Copyright 2025 Michail Krasnov <mskrasnov07@ya.ru>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21//! ferrix-lib is a library for obtaining information about the
22//! hardware and software of a PC running Linux OS.
23//!
24//! ## Examples
25//! Get all information about hardware and software (NOTE: needed
26//! `root` permissions!):
27//! ```no-test
28//! use ferrix_lib::Ferrix;
29//!
30//! let data = Ferrix::new()?; // get all data
31//!
32//! let json_str = data.to_json()?; // get machine-readable JSON from this data
33//! let pjson_str = data.to_json_pretty()?; // get human-readable JSON
34//! let xml_str = data.to_xml()?; // get XML
35//! ```
36//!
37//! Get information about CPU:
38//! ```no-test
39//! use ferrix_lib::cpu::Processors;
40//! let proc = Processors::new()?;
41//!
42//! let json_str = data.to_json()?;
43//! let pjson_str = data.to_json_pretty()?;
44//! ```
45
46pub mod cpu;
47pub mod dmi;
48pub mod drm;
49pub mod init;
50pub mod ram;
51pub mod sys;
52
53pub mod traits;
54pub mod utils;
55
56use crate::traits::ToPlainText;
57use anyhow::Result;
58use serde::Serialize;
59
60pub const FX_LIB_VERSION: &str = env!("CARGO_PKG_VERSION");
61
62#[derive(Debug, Serialize)]
63pub struct Ferrix {
64    pub cpu: cpu::Processors,
65    pub ram: ram::RAM,
66    pub swaps: ram::Swaps,
67    pub dmi: dmi::DMITable,
68    pub drm: drm::Video,
69    pub sys: sys::Sys,
70    pub init: init::SystemdServices,
71}
72
73impl Ferrix {
74    pub async fn new() -> Result<Self> {
75        let conn = zbus::Connection::system().await?;
76        Ok(Self {
77            cpu: cpu::Processors::new()?,
78            ram: ram::RAM::new()?,
79            swaps: ram::Swaps::new()?,
80            dmi: dmi::DMITable::new()?,
81            drm: drm::Video::new()?,
82            sys: sys::Sys::new()?,
83            init: init::SystemdServices::new_from_connection(&conn).await?,
84        })
85    }
86
87    fn _update(&mut self) -> Result<()> {
88        self.cpu = cpu::Processors::new()?;
89        self.ram = ram::RAM::new()?;
90        self.swaps = ram::Swaps::new()?;
91        self.sys.update()?;
92
93        Ok(())
94    }
95
96    pub async fn update(&mut self, conn: &zbus::Connection) -> Result<()> {
97        self._update()?;
98        self.init = init::SystemdServices::new_from_connection(&conn).await?;
99        Ok(())
100    }
101
102    pub async fn update1(&mut self) -> Result<()> {
103        self._update()?;
104        let conn = zbus::Connection::system().await?;
105        self.init = init::SystemdServices::new_from_connection(&conn).await?;
106        Ok(())
107    }
108
109    /// Performs serialization of structure data in JSON.
110    ///
111    /// The returned value will be a SINGLE LINE of JSON data
112    /// intended for reading by third-party software or for
113    /// transmission over the network.
114    pub fn to_json(&self) -> Result<String> {
115        Ok(serde_json::to_string(&self)?)
116    }
117
118    /// Performs serialization in "pretty" JSON
119    ///
120    /// JSON will contain unnecessary newline transitions and spaces
121    /// to visually separate the blocks. It is well suited for human
122    /// reading and analysis.
123    pub fn to_json_pretty(&self) -> Result<String> {
124        Ok(serde_json::to_string_pretty(&self)?)
125    }
126
127    /// Performs data serialization in XML format
128    pub fn to_xml(&self) -> Result<String> {
129        let xml = XMLData::from(self);
130        let data = XMLFerrixData::from(&xml);
131        data.to_xml()
132    }
133}
134
135impl ToPlainText for Ferrix {
136    fn to_plain(&self) -> String {
137        let mut s = format!("");
138        s += &self.cpu.to_plain();
139        s += &self.init.to_plain();
140
141        s
142    }
143}
144
145#[derive(Serialize)]
146struct XMLFerrixData<'a> {
147    data: &'a XMLData<'a>,
148}
149
150#[derive(Serialize)]
151struct XMLData<'a> {
152    cpu: &'a cpu::Processors,
153    ram: &'a ram::RAM,
154    dmi: dmi::DMITableXml<'a>,
155    sys: &'a sys::Sys,
156    init: &'a init::SystemdServices,
157}
158
159impl<'a> From<&'a Ferrix> for XMLData<'a> {
160    fn from(value: &'a Ferrix) -> Self {
161        Self {
162            cpu: &value.cpu,
163            ram: &value.ram,
164            dmi: dmi::DMITableXml::from(&value.dmi),
165            sys: &value.sys,
166            init: &value.init,
167        }
168    }
169}
170
171impl<'a> XMLFerrixData<'a> {
172    fn to_xml(&self) -> Result<String> {
173        Ok(xml_serde::to_string(&self)?)
174    }
175}
176
177impl<'a> From<&'a XMLData<'a>> for XMLFerrixData<'a> {
178    fn from(value: &'a XMLData) -> Self {
179        Self { data: value }
180    }
181}