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