Skip to main content

hyprshell_config_lib/io/
convert.rs

1use crate::io::*;
2
3impl TryFrom<Config> for crate::Config {
4    type Error = anyhow::Error;
5
6    fn try_from(value: Config) -> Result<Self, Self::Error> {
7        Ok(Self {
8            windows: value.windows.map(Windows::try_into).transpose()?,
9        })
10    }
11}
12
13impl From<crate::Config> for Config {
14    fn from(value: crate::Config) -> Self {
15        Self {
16            version: crate::CURRENT_CONFIG_VERSION,
17            windows: value.windows.map(crate::Windows::into),
18        }
19    }
20}
21
22impl TryFrom<Windows> for crate::Windows {
23    type Error = anyhow::Error;
24    fn try_from(value: Windows) -> Result<Self, Self::Error> {
25        Ok(Self {
26            general: crate::WindowsGeneral {
27                scale: value.scale,
28                items_per_row: value.items_per_row,
29            },
30            switch: value.switch.map(Switch::try_into).transpose()?,
31            switch_2: value.switch_2.map(Switch::try_into).transpose()?,
32            overview: value.overview.map(Overview::try_into).transpose()?,
33        })
34    }
35}
36
37impl From<crate::Windows> for Windows {
38    fn from(value: crate::Windows) -> Self {
39        Self {
40            scale: value.general.scale,
41            items_per_row: value.general.items_per_row,
42            overview: value.overview.map(crate::Overview::into),
43            switch: value.switch.map(crate::Switch::into),
44            switch_2: value.switch_2.map(crate::Switch::into),
45        }
46    }
47}
48
49impl TryFrom<Overview> for crate::Overview {
50    type Error = anyhow::Error;
51    fn try_from(value: Overview) -> Result<Self, Self::Error> {
52        Ok(Self {
53            key: value.key,
54            modifier: value.modifier,
55            top_offset: value.top_offset,
56            filter_by_same_class: value.filter_by.contains(&FilterBy::SameClass),
57            filter_by_current_workspace: value.filter_by.contains(&FilterBy::CurrentWorkspace),
58            filter_by_current_monitor: value.filter_by.contains(&FilterBy::CurrentMonitor),
59            launcher: value.launcher.try_into()?,
60            exclude_workspaces: value.exclude_workspaces,
61        })
62    }
63}
64
65impl From<crate::Overview> for Overview {
66    fn from(value: crate::Overview) -> Self {
67        let mut filter = vec![];
68        if value.filter_by_current_monitor {
69            filter.push(FilterBy::CurrentMonitor);
70        }
71        if value.filter_by_current_workspace {
72            filter.push(FilterBy::CurrentWorkspace);
73        }
74        if value.filter_by_same_class {
75            filter.push(FilterBy::SameClass);
76        }
77        Self {
78            launcher: value.launcher.into(),
79            key: value.key,
80            modifier: value.modifier,
81            filter_by: filter,
82            exclude_workspaces: value.exclude_workspaces,
83            top_offset: value.top_offset,
84        }
85    }
86}
87
88impl TryFrom<Switch> for crate::Switch {
89    type Error = anyhow::Error;
90    fn try_from(value: Switch) -> Result<Self, Self::Error> {
91        // TODO check key +  kill key
92        Ok(Self {
93            modifier: value.modifier,
94            key: value.key,
95            filter_by_same_class: value.filter_by.contains(&FilterBy::SameClass),
96            filter_by_current_workspace: value.filter_by.contains(&FilterBy::CurrentWorkspace),
97            filter_by_current_monitor: value.filter_by.contains(&FilterBy::CurrentMonitor),
98            switch_workspaces: value.switch_workspaces,
99            exclude_workspaces: value.exclude_workspaces,
100            kill_key: value.kill_key,
101        })
102    }
103}
104
105impl From<crate::Switch> for Switch {
106    fn from(value: crate::Switch) -> Self {
107        let mut filter = vec![];
108        if value.filter_by_current_monitor {
109            filter.push(FilterBy::CurrentMonitor);
110        }
111        if value.filter_by_current_workspace {
112            filter.push(FilterBy::CurrentWorkspace);
113        }
114        if value.filter_by_same_class {
115            filter.push(FilterBy::SameClass);
116        }
117        Self {
118            modifier: value.modifier,
119            key: value.key,
120            filter_by: filter,
121            switch_workspaces: value.switch_workspaces,
122            exclude_workspaces: value.exclude_workspaces,
123            kill_key: value.kill_key,
124        }
125    }
126}
127
128impl TryFrom<Launcher> for crate::Launcher {
129    type Error = anyhow::Error;
130    fn try_from(value: Launcher) -> Result<Self, Self::Error> {
131        Ok(Self {
132            default_terminal: value.default_terminal,
133            launch_modifier: value.launch_modifier,
134            width: value.width,
135            show_when_empty: value.show_when_empty,
136            max_items: value.max_items,
137            plugins: value.plugins.try_into()?,
138        })
139    }
140}
141
142impl From<crate::Launcher> for Launcher {
143    fn from(value: crate::Launcher) -> Self {
144        Self {
145            default_terminal: value.default_terminal,
146            launch_modifier: value.launch_modifier,
147            width: value.width,
148            max_items: value.max_items,
149            show_when_empty: value.show_when_empty,
150            plugins: value.plugins.into(),
151        }
152    }
153}
154
155impl TryFrom<Plugins> for crate::Plugins {
156    type Error = anyhow::Error;
157
158    fn try_from(value: Plugins) -> Result<Self, Self::Error> {
159        Ok(Self {
160            applications: value
161                .applications
162                .map(ApplicationsPluginConfig::try_into)
163                .transpose()?,
164            terminal: if value.terminal.is_some() {
165                Some(())
166            } else {
167                None
168            },
169            shell: if value.shell.is_some() {
170                Some(())
171            } else {
172                None
173            },
174            websearch: value.websearch.map(WebSearchConfig::try_into).transpose()?,
175            calc: if value.calc.is_some() { Some(()) } else { None },
176            path: if value.path.is_some() { Some(()) } else { None },
177            actions: value
178                .actions
179                .map(ActionsPluginConfig::try_into)
180                .transpose()?,
181        })
182    }
183}
184
185impl From<crate::Plugins> for Plugins {
186    fn from(value: crate::Plugins) -> Self {
187        Self {
188            applications: value.applications.map(Into::into),
189            terminal: if value.terminal.is_some() {
190                Some(EmptyConfig {})
191            } else {
192                None
193            },
194            shell: if value.shell.is_some() {
195                Some(EmptyConfig {})
196            } else {
197                None
198            },
199            websearch: value.websearch.map(Into::into),
200            calc: if value.calc.is_some() {
201                Some(EmptyConfig {})
202            } else {
203                None
204            },
205            path: if value.path.is_some() {
206                Some(EmptyConfig {})
207            } else {
208                None
209            },
210            actions: value.actions.map(Into::into),
211        }
212    }
213}
214
215impl TryFrom<ActionsPluginConfig> for crate::ActionsPluginConfig {
216    type Error = anyhow::Error;
217
218    fn try_from(value: ActionsPluginConfig) -> Result<Self, Self::Error> {
219        Ok(Self {
220            actions: value
221                .actions
222                .into_iter()
223                .map(ActionsPluginAction::try_into)
224                .collect::<Result<_, _>>()?,
225        })
226    }
227}
228
229impl From<crate::ActionsPluginConfig> for ActionsPluginConfig {
230    fn from(value: crate::ActionsPluginConfig) -> Self {
231        Self {
232            actions: value.actions.into_iter().map(Into::into).collect(),
233        }
234    }
235}
236
237impl TryFrom<ApplicationsPluginConfig> for crate::ApplicationsPluginConfig {
238    type Error = anyhow::Error;
239
240    fn try_from(value: ApplicationsPluginConfig) -> Result<Self, Self::Error> {
241        Ok(Self {
242            run_cache_weeks: value.run_cache_weeks,
243            show_execs: value.show_execs,
244            show_actions_submenu: value.show_actions_submenu,
245        })
246    }
247}
248
249impl From<crate::ApplicationsPluginConfig> for ApplicationsPluginConfig {
250    fn from(value: crate::ApplicationsPluginConfig) -> Self {
251        Self {
252            run_cache_weeks: value.run_cache_weeks,
253            show_execs: value.show_execs,
254            show_actions_submenu: value.show_actions_submenu,
255        }
256    }
257}
258
259impl TryFrom<WebSearchConfig> for crate::WebSearchConfig {
260    type Error = anyhow::Error;
261
262    fn try_from(value: WebSearchConfig) -> Result<Self, Self::Error> {
263        Ok(Self {
264            engines: value
265                .engines
266                .into_iter()
267                .map(SearchEngine::try_into)
268                .collect::<Result<_, _>>()?,
269        })
270    }
271}
272
273impl From<crate::WebSearchConfig> for WebSearchConfig {
274    fn from(value: crate::WebSearchConfig) -> Self {
275        Self {
276            engines: value.engines.into_iter().map(Into::into).collect(),
277        }
278    }
279}
280
281impl TryFrom<SearchEngine> for crate::SearchEngine {
282    type Error = anyhow::Error;
283
284    fn try_from(value: SearchEngine) -> Result<Self, Self::Error> {
285        Ok(Self {
286            url: value.url,
287            name: value.name,
288            key: value.key,
289        })
290    }
291}
292
293impl From<crate::SearchEngine> for SearchEngine {
294    fn from(value: crate::SearchEngine) -> Self {
295        Self {
296            url: value.url,
297            name: value.name,
298            key: value.key,
299        }
300    }
301}
302
303impl TryFrom<ActionsPluginAction> for crate::ActionsPluginAction {
304    type Error = anyhow::Error;
305
306    fn try_from(value: ActionsPluginAction) -> Result<Self, Self::Error> {
307        match value {
308            ActionsPluginAction::LockScreen => Ok(Self::LockScreen),
309            ActionsPluginAction::Hibernate => Ok(Self::Hibernate),
310            ActionsPluginAction::Logout => Ok(Self::Logout),
311            ActionsPluginAction::Reboot => Ok(Self::Reboot),
312            ActionsPluginAction::Shutdown => Ok(Self::Shutdown),
313            ActionsPluginAction::Suspend => Ok(Self::Suspend),
314            ActionsPluginAction::Custom(v) => Ok(Self::Custom(v.try_into()?)),
315        }
316    }
317}
318
319impl From<crate::ActionsPluginAction> for ActionsPluginAction {
320    fn from(value: crate::ActionsPluginAction) -> Self {
321        match value {
322            crate::ActionsPluginAction::LockScreen => Self::LockScreen,
323            crate::ActionsPluginAction::Hibernate => Self::Hibernate,
324            crate::ActionsPluginAction::Logout => Self::Logout,
325            crate::ActionsPluginAction::Reboot => Self::Reboot,
326            crate::ActionsPluginAction::Shutdown => Self::Shutdown,
327            crate::ActionsPluginAction::Suspend => Self::Suspend,
328            crate::ActionsPluginAction::Custom(v) => Self::Custom(v.into()),
329        }
330    }
331}
332
333impl TryFrom<ActionsPluginActionCustom> for crate::ActionsPluginActionCustom {
334    type Error = anyhow::Error;
335
336    fn try_from(value: ActionsPluginActionCustom) -> Result<Self, Self::Error> {
337        // TODO check icon
338        Ok(Self {
339            names: value.names,
340            details: value.details,
341            command: value.command,
342            icon: value.icon,
343        })
344    }
345}
346
347impl From<crate::ActionsPluginActionCustom> for ActionsPluginActionCustom {
348    fn from(value: crate::ActionsPluginActionCustom) -> Self {
349        Self {
350            names: value.names,
351            details: value.details,
352            command: value.command,
353            icon: value.icon,
354        }
355    }
356}