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