Skip to main content

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 battery;
47pub mod cpu;
48pub mod cpu_freq;
49pub mod dmi;
50pub mod drm;
51pub mod init;
52pub mod net;
53pub mod parts;
54pub mod ram;
55pub mod soft;
56pub mod sys;
57pub mod vmstat;
58pub mod vulnerabilities;
59
60pub mod traits;
61pub mod utils;
62
63use crate::traits::ToPlainText;
64use anyhow::Result;
65use serde::Serialize;
66
67pub const FX_LIB_VERSION: &str = env!("CARGO_PKG_VERSION");
68
69#[derive(Debug, Serialize)]
70pub struct Ferrix {
71    pub cpu: cpu::Processors,
72    pub ram: ram::RAM,
73    pub swaps: ram::Swaps,
74    pub dmi: dmi::DMITable,
75    pub drm: drm::Video,
76    pub sys: sys::Sys,
77    pub init: init::SystemdServices,
78}
79
80impl Ferrix {
81    pub async fn new() -> Result<Self> {
82        let conn = zbus::Connection::system().await?;
83        Ok(Self {
84            cpu: cpu::Processors::new()?,
85            ram: ram::RAM::new()?,
86            swaps: ram::Swaps::new()?,
87            dmi: dmi::DMITable::new()?,
88            drm: drm::Video::new()?,
89            sys: sys::Sys::new()?,
90            init: init::SystemdServices::new_from_connection(&conn).await?,
91        })
92    }
93
94    fn _update(&mut self) -> Result<()> {
95        self.cpu = cpu::Processors::new()?;
96        self.ram = ram::RAM::new()?;
97        self.swaps = ram::Swaps::new()?;
98        self.sys.update()?;
99
100        Ok(())
101    }
102
103    pub async fn update(&mut self, conn: &zbus::Connection) -> Result<()> {
104        self._update()?;
105        self.init = init::SystemdServices::new_from_connection(&conn).await?;
106        Ok(())
107    }
108
109    pub async fn update1(&mut self) -> Result<()> {
110        self._update()?;
111        let conn = zbus::Connection::system().await?;
112        self.init = init::SystemdServices::new_from_connection(&conn).await?;
113        Ok(())
114    }
115
116    /// Performs serialization of structure data in JSON.
117    ///
118    /// The returned value will be a SINGLE LINE of JSON data
119    /// intended for reading by third-party software or for
120    /// transmission over the network.
121    pub fn to_json(&self) -> Result<String> {
122        Ok(serde_json::to_string(&self)?)
123    }
124
125    /// Performs serialization in "pretty" JSON
126    ///
127    /// JSON will contain unnecessary newline transitions and spaces
128    /// to visually separate the blocks. It is well suited for human
129    /// reading and analysis.
130    pub fn to_json_pretty(&self) -> Result<String> {
131        Ok(serde_json::to_string_pretty(&self)?)
132    }
133
134    /// Performs data serialization in XML format
135    pub fn to_xml(&self) -> Result<String> {
136        let xml = XMLData::from(self);
137        let data = XMLFerrixData::from(&xml);
138        data.to_xml()
139    }
140}
141
142impl ToPlainText for Ferrix {
143    fn to_plain(&self) -> String {
144        let mut s = format!("");
145        s += &self.cpu.to_plain();
146        s += &self.init.to_plain();
147
148        s
149    }
150}
151
152#[derive(Serialize)]
153struct XMLFerrixData<'a> {
154    data: &'a XMLData<'a>,
155}
156
157#[derive(Serialize)]
158struct XMLData<'a> {
159    cpu: &'a cpu::Processors,
160    ram: &'a ram::RAM,
161    dmi: dmi::DMITableXml<'a>,
162    sys: &'a sys::Sys,
163    init: &'a init::SystemdServices,
164}
165
166impl<'a> From<&'a Ferrix> for XMLData<'a> {
167    fn from(value: &'a Ferrix) -> Self {
168        Self {
169            cpu: &value.cpu,
170            ram: &value.ram,
171            dmi: dmi::DMITableXml::from(&value.dmi),
172            sys: &value.sys,
173            init: &value.init,
174        }
175    }
176}
177
178impl<'a> XMLFerrixData<'a> {
179    fn to_xml(&self) -> Result<String> {
180        Ok(xml_serde::to_string(&self)?)
181    }
182}
183
184impl<'a> From<&'a XMLData<'a>> for XMLFerrixData<'a> {
185    fn from(value: &'a XMLData) -> Self {
186        Self { data: value }
187    }
188}