1pub mod battery;
47pub mod cpu;
48pub mod cpu_freq;
49pub mod desktop;
50pub mod dmi;
51pub mod drm;
52pub mod firmware;
53pub mod init;
54pub mod net;
55pub mod parts;
56pub mod ram;
57pub mod resources;
58pub mod soft;
59pub mod sys;
60pub mod vmstat;
61pub mod vulnerabilities;
62
63pub mod traits;
64pub mod utils;
65
66use crate::traits::ToPlainText;
67use anyhow::Result;
68use serde::Serialize;
69
70pub const FX_LIB_VERSION: &str = env!("CARGO_PKG_VERSION");
71
72#[derive(Debug, Serialize)]
73pub struct Ferrix {
74 pub cpu: cpu::Processors,
75 pub ram: ram::RAM,
76 pub swaps: ram::Swaps,
77 pub dmi: dmi::DMITable,
78 pub drm: drm::Video,
79 pub sys: sys::Sys,
80 pub init: init::SystemdServices,
81}
82
83impl Ferrix {
84 pub async fn new() -> Result<Self> {
85 let conn = zbus::Connection::system().await?;
86 Ok(Self {
87 cpu: cpu::Processors::new()?,
88 ram: ram::RAM::new()?,
89 swaps: ram::Swaps::new()?,
90 dmi: dmi::DMITable::new()?,
91 drm: drm::Video::new()?,
92 sys: sys::Sys::new()?,
93 init: init::SystemdServices::new_from_connection(&conn).await?,
94 })
95 }
96
97 fn _update(&mut self) -> Result<()> {
98 self.cpu = cpu::Processors::new()?;
99 self.ram = ram::RAM::new()?;
100 self.swaps = ram::Swaps::new()?;
101 self.sys.update()?;
102
103 Ok(())
104 }
105
106 pub async fn update(&mut self, conn: &zbus::Connection) -> Result<()> {
107 self._update()?;
108 self.init = init::SystemdServices::new_from_connection(&conn).await?;
109 Ok(())
110 }
111
112 pub async fn update1(&mut self) -> Result<()> {
113 self._update()?;
114 let conn = zbus::Connection::system().await?;
115 self.init = init::SystemdServices::new_from_connection(&conn).await?;
116 Ok(())
117 }
118
119 pub fn to_json(&self) -> Result<String> {
125 Ok(serde_json::to_string(&self)?)
126 }
127
128 pub fn to_json_pretty(&self) -> Result<String> {
134 Ok(serde_json::to_string_pretty(&self)?)
135 }
136
137 pub fn to_xml(&self) -> Result<String> {
139 let xml = XMLData::from(self);
140 let data = XMLFerrixData::from(&xml);
141 data.to_xml()
142 }
143}
144
145impl ToPlainText for Ferrix {
146 fn to_plain(&self) -> String {
147 let mut s = format!("");
148 s += &self.cpu.to_plain();
149 s += &self.init.to_plain();
150
151 s
152 }
153}
154
155#[derive(Serialize)]
156struct XMLFerrixData<'a> {
157 data: &'a XMLData<'a>,
158}
159
160#[derive(Serialize)]
161struct XMLData<'a> {
162 cpu: &'a cpu::Processors,
163 ram: &'a ram::RAM,
164 dmi: dmi::DMITableXml<'a>,
165 sys: &'a sys::Sys,
166 init: &'a init::SystemdServices,
167}
168
169impl<'a> From<&'a Ferrix> for XMLData<'a> {
170 fn from(value: &'a Ferrix) -> Self {
171 Self {
172 cpu: &value.cpu,
173 ram: &value.ram,
174 dmi: dmi::DMITableXml::from(&value.dmi),
175 sys: &value.sys,
176 init: &value.init,
177 }
178 }
179}
180
181impl<'a> XMLFerrixData<'a> {
182 fn to_xml(&self) -> Result<String> {
183 Ok(xml_serde::to_string(&self)?)
184 }
185}
186
187impl<'a> From<&'a XMLData<'a>> for XMLFerrixData<'a> {
188 fn from(value: &'a XMLData) -> Self {
189 Self { data: value }
190 }
191}