1pub 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 pub fn to_json(&self) -> Result<String> {
113 Ok(serde_json::to_string(&self)?)
114 }
115
116 pub fn to_json_pretty(&self) -> Result<String> {
122 Ok(serde_json::to_string_pretty(&self)?)
123 }
124
125 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}