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
60pub const FX_LIB_VERSION: &str = env!("CARGO_PKG_VERSION");
61
62#[derive(Debug, Serialize)]
63pub struct Ferrix {
64 pub cpu: cpu::Processors,
65 pub ram: ram::RAM,
66 pub swaps: ram::Swaps,
67 pub dmi: dmi::DMITable,
68 pub drm: drm::Video,
69 pub sys: sys::Sys,
70 pub init: init::SystemdServices,
71}
72
73impl Ferrix {
74 pub async fn new() -> Result<Self> {
75 let conn = zbus::Connection::system().await?;
76 Ok(Self {
77 cpu: cpu::Processors::new()?,
78 ram: ram::RAM::new()?,
79 swaps: ram::Swaps::new()?,
80 dmi: dmi::DMITable::new()?,
81 drm: drm::Video::new()?,
82 sys: sys::Sys::new()?,
83 init: init::SystemdServices::new_from_connection(&conn).await?,
84 })
85 }
86
87 fn _update(&mut self) -> Result<()> {
88 self.cpu = cpu::Processors::new()?;
89 self.ram = ram::RAM::new()?;
90 self.swaps = ram::Swaps::new()?;
91 self.sys.update()?;
92
93 Ok(())
94 }
95
96 pub async fn update(&mut self, conn: &zbus::Connection) -> Result<()> {
97 self._update()?;
98 self.init = init::SystemdServices::new_from_connection(&conn).await?;
99 Ok(())
100 }
101
102 pub async fn update1(&mut self) -> Result<()> {
103 self._update()?;
104 let conn = zbus::Connection::system().await?;
105 self.init = init::SystemdServices::new_from_connection(&conn).await?;
106 Ok(())
107 }
108
109 pub fn to_json(&self) -> Result<String> {
115 Ok(serde_json::to_string(&self)?)
116 }
117
118 pub fn to_json_pretty(&self) -> Result<String> {
124 Ok(serde_json::to_string_pretty(&self)?)
125 }
126
127 pub fn to_xml(&self) -> Result<String> {
129 let xml = XMLData::from(self);
130 let data = XMLFerrixData::from(&xml);
131 data.to_xml()
132 }
133}
134
135impl ToPlainText for Ferrix {
136 fn to_plain(&self) -> String {
137 let mut s = format!("");
138 s += &self.cpu.to_plain();
139 s += &self.init.to_plain();
140
141 s
142 }
143}
144
145#[derive(Serialize)]
146struct XMLFerrixData<'a> {
147 data: &'a XMLData<'a>,
148}
149
150#[derive(Serialize)]
151struct XMLData<'a> {
152 cpu: &'a cpu::Processors,
153 ram: &'a ram::RAM,
154 dmi: dmi::DMITableXml<'a>,
155 sys: &'a sys::Sys,
156 init: &'a init::SystemdServices,
157}
158
159impl<'a> From<&'a Ferrix> for XMLData<'a> {
160 fn from(value: &'a Ferrix) -> Self {
161 Self {
162 cpu: &value.cpu,
163 ram: &value.ram,
164 dmi: dmi::DMITableXml::from(&value.dmi),
165 sys: &value.sys,
166 init: &value.init,
167 }
168 }
169}
170
171impl<'a> XMLFerrixData<'a> {
172 fn to_xml(&self) -> Result<String> {
173 Ok(xml_serde::to_string(&self)?)
174 }
175}
176
177impl<'a> From<&'a XMLData<'a>> for XMLFerrixData<'a> {
178 fn from(value: &'a XMLData) -> Self {
179 Self { data: value }
180 }
181}