-
Notifications
You must be signed in to change notification settings - Fork 43
/
client.rs
175 lines (159 loc) · 5.57 KB
/
client.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) [2024] SUSE LLC
//
// All Rights Reserved.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, contact SUSE LLC.
//
// To contact SUSE LLC about this file by physical or electronic mail, you may
// find current contact information at www.suse.com.
use super::proxies::Software1Proxy;
use crate::error::ServiceError;
use serde::Serialize;
use serde_repr::Serialize_repr;
use std::collections::HashMap;
use zbus::Connection;
/// Represents a software product
#[derive(Debug, Serialize, utoipa::ToSchema)]
pub struct Pattern {
/// Pattern name (eg., "aaa_base", "gnome")
pub name: String,
/// Pattern category (e.g., "Production")
pub category: String,
/// Pattern icon path locally on system
pub icon: String,
/// Pattern description
pub description: String,
/// Pattern summary
pub summary: String,
/// Pattern order
pub order: String,
}
/// Represents the reason why a pattern is selected.
#[derive(Clone, Copy, Debug, PartialEq, Serialize_repr, utoipa::ToSchema)]
#[repr(u8)]
pub enum SelectedBy {
/// The pattern was selected by the user.
User = 0,
/// The pattern was selected automatically.
Auto = 1,
/// The pattern has not be selected.
None = 2,
}
#[derive(Debug, thiserror::Error)]
#[error("Unknown selected by value: '{0}'")]
pub struct UnknownSelectedBy(u8);
impl TryFrom<u8> for SelectedBy {
type Error = UnknownSelectedBy;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::User),
1 => Ok(Self::Auto),
_ => Err(UnknownSelectedBy(value)),
}
}
}
/// D-Bus client for the software service
#[derive(Clone)]
pub struct SoftwareClient<'a> {
software_proxy: Software1Proxy<'a>,
}
impl<'a> SoftwareClient<'a> {
pub async fn new(connection: Connection) -> Result<SoftwareClient<'a>, ServiceError> {
Ok(Self {
software_proxy: Software1Proxy::new(&connection).await?,
})
}
/// Returns the available patterns
pub async fn patterns(&self, filtered: bool) -> Result<Vec<Pattern>, ServiceError> {
let patterns: Vec<Pattern> = self
.software_proxy
.list_patterns(filtered)
.await?
.into_iter()
.map(
|(name, (category, description, icon, summary, order))| Pattern {
name,
category,
icon,
description,
summary,
order,
},
)
.collect();
Ok(patterns)
}
/// Returns the ids of patterns selected by user
pub async fn user_selected_patterns(&self) -> Result<Vec<String>, ServiceError> {
let patterns: Vec<String> = self
.software_proxy
.selected_patterns()
.await?
.into_iter()
.filter_map(|(id, reason)| match SelectedBy::try_from(reason) {
Ok(SelectedBy::User) => Some(id),
Ok(_reason) => None,
Err(e) => {
log::warn!("Ignoring pattern {}. Error: {}", &id, e);
None
}
})
.collect();
Ok(patterns)
}
/// Returns the selected pattern and the reason each one selected.
pub async fn selected_patterns(&self) -> Result<HashMap<String, SelectedBy>, ServiceError> {
let patterns = self.software_proxy.selected_patterns().await?;
let patterns = patterns
.into_iter()
.filter_map(|(id, reason)| match SelectedBy::try_from(reason) {
Ok(reason) => Some((id, reason)),
Err(e) => {
log::warn!("Ignoring pattern {}. Error: {}", &id, e);
None
}
})
.collect();
Ok(patterns)
}
/// Selects patterns by user
pub async fn select_patterns(
&self,
patterns: HashMap<String, bool>,
) -> Result<(), ServiceError> {
let (add, remove): (Vec<_>, Vec<_>) =
patterns.into_iter().partition(|(_, install)| *install);
let add: Vec<_> = add.iter().map(|(name, _)| name.as_ref()).collect();
let remove: Vec<_> = remove.iter().map(|(name, _)| name.as_ref()).collect();
let wrong_patterns = self
.software_proxy
.set_user_patterns(add.as_slice(), remove.as_slice())
.await?;
if !wrong_patterns.is_empty() {
Err(ServiceError::UnknownPatterns(wrong_patterns))
} else {
Ok(())
}
}
/// Returns the required space for installing the selected patterns.
///
/// It returns a formatted string including the size and the unit.
pub async fn used_disk_space(&self) -> Result<String, ServiceError> {
Ok(self.software_proxy.used_disk_space().await?)
}
/// Starts the process to read the repositories data.
pub async fn probe(&self) -> Result<(), ServiceError> {
Ok(self.software_proxy.probe().await?)
}
}