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