ferrix_app/
export.rs

1/* export.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//! Export manager
22
23use std::fmt::Display;
24
25use ferrix_lib::{
26    battery::BatInfo,
27    cpu::Processors,
28    drm::Video,
29    init::SystemdServices,
30    ram::RAM,
31    sys::{Groups, OsRelease, Users},
32    traits::ToJson,
33};
34use serde::Serialize;
35
36use crate::{DataLoadingState, KernelData};
37
38#[derive(Debug, Clone, Eq, PartialEq)]
39pub enum ExportStatus {
40    LoadingData,
41    ErrorLoadingData(String),
42    SerializingStructure,
43    ErrorSerializing(String),
44    WritingData,
45    ErrorWritingData(String),
46}
47
48#[derive(Debug, Clone, Eq, PartialEq)]
49pub enum ExportFormat {
50    CompressedJson,
51    HumanJson,
52}
53
54impl ExportFormat {
55    pub const ALL: &[Self] = &[Self::CompressedJson, Self::HumanJson];
56}
57
58impl Display for ExportFormat {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        write!(
61            f,
62            "{}",
63            match self {
64                Self::CompressedJson => "Compressed JSON",
65                Self::HumanJson => "Human-readable JSON",
66            }
67        )
68    }
69}
70
71#[derive(Debug, Clone, Eq, PartialEq)]
72pub enum ExportMode {
73    AllData,
74    Selected,
75}
76
77impl ExportMode {
78    pub const ALL: &[Self] = &[Self::AllData, Self::Selected];
79}
80
81impl Display for ExportMode {
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        write!(
84            f,
85            "{}",
86            match self {
87                Self::AllData => "All collected data",
88                Self::Selected => "Selected data",
89            }
90        )
91    }
92}
93
94#[derive(Debug, Clone, Serialize)]
95#[serde(untagged)]
96pub enum ExportMember<'a, T> {
97    Data { data: Option<&'a T> },
98    Error { error_text: String },
99}
100
101impl<'a, T> From<&'a DataLoadingState<T>> for ExportMember<'a, T> {
102    fn from(value: &'a DataLoadingState<T>) -> Self {
103        match value {
104            DataLoadingState::Loaded(data) => Self::Data { data: Some(data) },
105            DataLoadingState::Loading => Self::Data { data: None },
106            DataLoadingState::Error(why) => Self::Error {
107                error_text: why.clone(),
108            },
109        }
110    }
111}
112
113fn get_data<'a, T>(data: &'a DataLoadingState<T>) -> Option<ExportMember<'a, T>> {
114    if let DataLoadingState::Loading = data {
115        return None;
116    }
117    Some(ExportMember::from(data))
118}
119
120#[derive(Debug, Clone, Serialize)]
121pub struct ExportData<'a> {
122    pub cpu: Option<ExportMember<'a, Processors>>,
123    pub ram: Option<ExportMember<'a, RAM>>,
124    pub battery: Option<ExportMember<'a, BatInfo>>,
125    pub drm: Option<ExportMember<'a, Video>>,
126    pub os_release: Option<ExportMember<'a, OsRelease>>,
127    pub kernel: Option<ExportMember<'a, KernelData>>,
128    pub users: Option<ExportMember<'a, Users>>,
129    pub groups: Option<ExportMember<'a, Groups>>,
130    pub systemd: Option<ExportMember<'a, SystemdServices>>,
131    pub misc: Option<ExportMember<'a, crate::System>>,
132}
133
134impl<'a> From<&'a mut crate::Ferrix> for ExportData<'a> {
135    fn from(value: &'a mut crate::Ferrix) -> Self {
136        Self {
137            cpu: get_data(&value.proc_data),
138            ram: get_data(&value.ram_data),
139            battery: get_data(&value.bat_data),
140            drm: get_data(&value.drm_data),
141            os_release: get_data(&value.osrel_data),
142            kernel: get_data(&value.info_kernel),
143            users: get_data(&value.users_list),
144            groups: get_data(&value.groups_list),
145            systemd: get_data(&value.sysd_services_list),
146            misc: get_data(&value.system),
147        }
148    }
149}
150
151impl<'a> ToJson for ExportData<'a> {}