1pub 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 pub fn to_json(&self) -> Result<String> {
122 Ok(serde_json::to_string(&self)?)
123 }
124
125 pub fn to_json_pretty(&self) -> Result<String> {
131 Ok(serde_json::to_string_pretty(&self)?)
132 }
133
134 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}