2121use crate :: extended_config:: ExtendedConfig ;
2222use crate :: message;
2323use crate :: model:: ModelAdapter ;
24- use crate :: proposal:: Proposal ;
2524use agama_locale_data:: { InvalidKeymapId , InvalidLocaleId , InvalidTimezoneId , KeymapId , LocaleId } ;
2625use agama_utils:: actor:: { self , Actor , Handler , MessageHandler } ;
2726use agama_utils:: api;
2827use agama_utils:: api:: event:: { self , Event } ;
29- use agama_utils:: api:: l10n:: SystemInfo ;
28+ use agama_utils:: api:: l10n:: { Proposal , SystemInfo } ;
3029use agama_utils:: api:: scope:: Scope ;
3130use agama_utils:: issue:: { self , Issue } ;
3231use 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.
7170pub 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-
8478impl 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]
152153impl 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]
178179impl 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]
219212impl 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]
226219impl 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]
239232impl 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]
250243impl 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 } ) ;
0 commit comments