Skip to content

Commit 9006aee

Browse files
committed
Move proposal to api
1 parent f508fca commit 9006aee

File tree

11 files changed

+48
-70
lines changed

11 files changed

+48
-70
lines changed

rust/agama-l10n/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,8 @@ pub use service::Service;
4444
mod model;
4545
pub use model::{Model, ModelAdapter};
4646

47-
mod proposal;
48-
pub use proposal::Proposal;
49-
50-
pub mod helpers;
51-
pub mod message;
52-
5347
mod dbus;
5448
mod extended_config;
49+
pub mod helpers;
50+
pub mod message;
5551
mod monitor;

rust/agama-l10n/src/message.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
// To contact SUSE LLC about this file by physical or electronic mail, you may
1919
// find current contact information at www.suse.com.
2020

21-
use crate::proposal::Proposal;
2221
use agama_locale_data::{KeymapId, LocaleId};
2322
use agama_utils::actor::Message;
2423
use agama_utils::api;
25-
use agama_utils::api::l10n::SystemInfo;
24+
use agama_utils::api::l10n::{Proposal, SystemInfo};
2625
use serde::Deserialize;
2726

2827
#[derive(Clone)]

rust/agama-l10n/src/service.rs

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121
use crate::extended_config::ExtendedConfig;
2222
use crate::message;
2323
use crate::model::ModelAdapter;
24-
use crate::proposal::Proposal;
2524
use agama_locale_data::{InvalidKeymapId, InvalidLocaleId, InvalidTimezoneId, KeymapId, LocaleId};
2625
use agama_utils::actor::{self, Actor, Handler, MessageHandler};
2726
use agama_utils::api;
2827
use agama_utils::api::event::{self, Event};
29-
use agama_utils::api::l10n::SystemInfo;
28+
use agama_utils::api::l10n::{Proposal, SystemInfo};
3029
use agama_utils::api::scope::Scope;
3130
use agama_utils::issue::{self, Issue};
3231
use async_trait::async_trait;
@@ -69,18 +68,13 @@ pub enum Error {
6968
/// * Holds the user configuration.
7069
/// * Applies the user configuration at the end of the installation.
7170
pub struct Service {
72-
state: State,
71+
system: SystemInfo,
72+
config: ExtendedConfig,
7373
model: Box<dyn ModelAdapter + Send + 'static>,
7474
issues: Handler<issue::Service>,
7575
events: event::Sender,
7676
}
7777

78-
struct State {
79-
system: SystemInfo,
80-
config: ExtendedConfig,
81-
proposal: Option<Proposal>,
82-
}
83-
8478
impl Service {
8579
pub fn new<T: ModelAdapter + Send + 'static>(
8680
model: T,
@@ -89,26 +83,33 @@ impl Service {
8983
) -> Service {
9084
let system = model.read_system_info();
9185
let config = ExtendedConfig::new_from(&system);
92-
let proposal = (&config).into();
93-
let state = State {
94-
system,
95-
config,
96-
proposal: Some(proposal),
97-
};
9886

9987
Self {
100-
state,
88+
system,
89+
config,
10190
model: Box::new(model),
10291
issues,
10392
events,
10493
}
10594
}
10695

96+
fn get_proposal(&self) -> Option<Proposal> {
97+
if !self.find_issues().is_empty() {
98+
return None;
99+
}
100+
101+
Some(Proposal {
102+
keymap: self.config.keymap.clone(),
103+
locale: self.config.locale.clone(),
104+
timezone: self.config.timezone.clone(),
105+
})
106+
}
107+
107108
/// Returns configuration issues.
108109
///
109110
/// It returns an issue for each unknown element (locale, keymap and timezone).
110111
fn find_issues(&self) -> Vec<Issue> {
111-
let config = &self.state.config;
112+
let config = &self.config;
112113
let mut issues = vec![];
113114
if !self.model.locales_db().exists(&config.locale) {
114115
issues.push(Issue {
@@ -151,7 +152,7 @@ impl Actor for Service {
151152
#[async_trait]
152153
impl MessageHandler<message::GetSystem> for Service {
153154
async fn handle(&mut self, _message: message::GetSystem) -> Result<SystemInfo, Error> {
154-
Ok(self.state.system.clone())
155+
Ok(self.system.clone())
155156
}
156157
}
157158

@@ -177,11 +178,10 @@ impl MessageHandler<message::SetSystem<message::SystemConfig>> for Service {
177178
#[async_trait]
178179
impl MessageHandler<message::GetConfig> for Service {
179180
async fn handle(&mut self, _message: message::GetConfig) -> Result<api::l10n::Config, Error> {
180-
let config = self.state.config.clone();
181181
Ok(api::l10n::Config {
182-
locale: Some(config.locale.to_string()),
183-
keymap: Some(config.keymap.to_string()),
184-
timezone: Some(config.timezone.to_string()),
182+
locale: Some(self.config.locale.to_string()),
183+
keymap: Some(self.config.keymap.to_string()),
184+
timezone: Some(self.config.timezone.to_string()),
185185
})
186186
}
187187
}
@@ -192,21 +192,14 @@ impl MessageHandler<message::SetConfig<api::l10n::Config>> for Service {
192192
&mut self,
193193
message: message::SetConfig<api::l10n::Config>,
194194
) -> Result<(), Error> {
195-
let config = ExtendedConfig::new_from(&self.state.system);
195+
let config = ExtendedConfig::new_from(&self.system);
196196
let merged = config.merge(&message.config)?;
197-
if merged == self.state.config {
197+
if merged == self.config {
198198
return Ok(());
199199
}
200200

201-
self.state.config = merged;
201+
self.config = merged;
202202
let issues = self.find_issues();
203-
204-
self.state.proposal = if issues.is_empty() {
205-
Some((&self.state.config).into())
206-
} else {
207-
None
208-
};
209-
210203
self.issues
211204
.cast(issue::message::Update::new(Scope::L10n, issues))?;
212205
self.events
@@ -218,14 +211,14 @@ impl MessageHandler<message::SetConfig<api::l10n::Config>> for Service {
218211
#[async_trait]
219212
impl MessageHandler<message::GetProposal> for Service {
220213
async fn handle(&mut self, _message: message::GetProposal) -> Result<Option<Proposal>, Error> {
221-
Ok(self.state.proposal.clone())
214+
Ok(self.get_proposal())
222215
}
223216
}
224217

225218
#[async_trait]
226219
impl MessageHandler<message::Install> for Service {
227220
async fn handle(&mut self, _message: message::Install) -> Result<(), Error> {
228-
let Some(proposal) = &self.state.proposal else {
221+
let Some(proposal) = self.get_proposal() else {
229222
return Err(Error::MissingProposal);
230223
};
231224

@@ -238,7 +231,7 @@ impl MessageHandler<message::Install> for Service {
238231
#[async_trait]
239232
impl MessageHandler<message::UpdateLocale> for Service {
240233
async fn handle(&mut self, message: message::UpdateLocale) -> Result<(), Error> {
241-
self.state.system.locale = message.locale;
234+
self.system.locale = message.locale;
242235
_ = self
243236
.events
244237
.send(Event::SystemChanged { scope: Scope::L10n });
@@ -249,7 +242,7 @@ impl MessageHandler<message::UpdateLocale> for Service {
249242
#[async_trait]
250243
impl MessageHandler<message::UpdateKeymap> for Service {
251244
async fn handle(&mut self, message: message::UpdateKeymap) -> Result<(), Error> {
252-
self.state.system.keymap = message.keymap;
245+
self.system.keymap = message.keymap;
253246
_ = self
254247
.events
255248
.send(Event::SystemChanged { scope: Scope::L10n });

rust/agama-manager/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,5 @@ pub mod service;
2525
pub use service::Service;
2626

2727
pub mod message;
28-
mod proposal;
2928

3029
pub use agama_l10n as l10n;

rust/agama-manager/src/message.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
// find current contact information at www.suse.com.
2020

2121
use crate::l10n;
22-
use crate::proposal::Proposal;
2322
use agama_lib::install_settings::InstallSettings;
2423
use agama_utils::actor::Message;
25-
use agama_utils::api::{Scope, Status, SystemInfo};
24+
use agama_utils::api::{Proposal, Scope, Status, SystemInfo};
2625
use agama_utils::issue::Issue;
2726
use serde::Deserialize;
2827
use std::collections::HashMap;

rust/agama-manager/src/service.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020

2121
use crate::l10n;
2222
use crate::message::{self, Action};
23-
use crate::proposal::Proposal;
2423
use agama_lib::install_settings::InstallSettings;
2524
use agama_utils::actor::{self, Actor, Handler, MessageHandler};
2625
use agama_utils::api::status::State;
27-
use agama_utils::api::{event, Event, Scope, Status, SystemInfo};
26+
use agama_utils::api::{event, Event, Proposal, Scope, Status, SystemInfo};
2827
use agama_utils::issue;
2928
use agama_utils::progress;
3029
use async_trait::async_trait;

rust/agama-server/src/web/docs/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,15 @@ impl ApiDocBuilder for ConfigApiDocBuilder {
162162
.schema_from::<crate::server::types::IssuesMap>()
163163
.schema_from::<crate::software::web::SoftwareProposal>()
164164
.schema_from::<agama_manager::message::Action>()
165-
.schema_from::<agama_utils::api::Progress>()
166165
.schema_from::<agama_utils::api::Status>()
166+
.schema_from::<agama_utils::api::Progress>()
167167
.schema_from::<agama_utils::api::status::State>()
168168
.schema_from::<agama_utils::api::Scope>()
169169
.schema_from::<agama_utils::api::SystemInfo>()
170170
.schema_from::<agama_utils::api::l10n::SystemInfo>()
171171
.schema_from::<agama_utils::api::l10n::Config>()
172+
.schema_from::<agama_utils::api::Proposal>()
173+
.schema_from::<agama_utils::api::l10n::Proposal>()
172174
.build()
173175
}
174176
}

rust/agama-utils/src/api.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,7 @@ pub use status::Status;
3636
pub mod system_info;
3737
pub use system_info::SystemInfo;
3838

39+
pub mod proposal;
40+
pub use proposal::Proposal;
41+
3942
pub mod l10n;

rust/agama-utils/src/api/l10n.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
//! This module contains all Agama public types that might be available over
2222
//! the HTTP and WebSocket API.
2323
24-
pub mod config;
24+
mod config;
2525
pub use config::Config;
2626

27-
pub mod system_info;
27+
mod system_info;
2828
pub use system_info::{Keymap, LocaleEntry, SystemInfo, TimezoneEntry};
29+
30+
mod proposal;
31+
pub use proposal::Proposal;
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
// To contact SUSE LLC about this file by physical or electronic mail, you may
1919
// find current contact information at www.suse.com.
2020

21-
use crate::extended_config::ExtendedConfig;
2221
use agama_locale_data::{KeymapId, LocaleId, TimezoneId};
2322
use serde::{Deserialize, Serialize};
2423
use serde_with::{serde_as, DisplayFromStr};
2524

2625
/// Describes what Agama proposes for the target system.
2726
#[serde_as]
28-
#[derive(Clone, Debug, Deserialize, Serialize)]
27+
#[derive(Clone, Debug, Deserialize, Serialize, utoipa::ToSchema)]
2928
pub struct Proposal {
3029
/// Keymap (e.g., "us", "cz(qwerty)", etc.).
3130
#[serde_as(as = "DisplayFromStr")]
@@ -37,17 +36,3 @@ pub struct Proposal {
3736
#[serde_as(as = "DisplayFromStr")]
3837
pub timezone: TimezoneId,
3938
}
40-
41-
/// Turns the configuration into a proposal.
42-
///
43-
/// It is possible because, in the l10n module, the configuration and the
44-
/// proposal are mostly the same.
45-
impl From<&ExtendedConfig> for Proposal {
46-
fn from(config: &ExtendedConfig) -> Self {
47-
Proposal {
48-
keymap: config.keymap.clone(),
49-
locale: config.locale.clone(),
50-
timezone: config.timezone.clone(),
51-
}
52-
}
53-
}

0 commit comments

Comments
 (0)