sd-switch 0.6.3

A systemd unit reload/restart utility for Home Manager
Documentation
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use std::{borrow::Cow, path::Path, sync::LazyLock};

use unic_langid::LanguageIdentifier;

struct EnUs;
struct SvSe;

pub static MSG: LazyLock<Box<dyn Messages + Send + Sync>> =
    LazyLock::new(|| init_messages(sys_locale::get_locales()));

fn init_messages(
    locales: impl Iterator<Item = String>,
) -> Box<dyn Messages + Send + Sync + 'static> {
    let mut available_langs: Vec<(&str, Box<dyn Messages + Send + Sync>)> =
        vec![("en", Box::new(EnUs)), ("sv", Box::new(SvSe))];

    for preferred_lang in locales.filter_map(|t| t.parse::<LanguageIdentifier>().ok()) {
        for (lang, msgs) in available_langs.drain(..) {
            if preferred_lang.matches(
                &lang
                    .parse::<LanguageIdentifier>()
                    .expect("should be valid locale string"),
                false,
                true,
            ) {
                return msgs;
            }
        }
    }

    Box::new(EnUs)
}

/// Trait covering all translatable strings in sd-switch.
///
/// The default implementation for each trait function matches the
/// implementation for `EnUs`.
pub(crate) trait Messages {
    fn err_both_system_and_user(&self) -> Cow<'static, str> {
        EnUs.err_both_system_and_user()
    }

    fn err_not_a_directory(&self, path: &Path) -> Cow<'static, str> {
        EnUs.err_not_a_directory(path)
    }

    fn err_missing_required_option(&self, name: &str) -> Cow<'static, str> {
        EnUs.err_missing_required_option(name)
    }

    fn err_run_help(&self, program: &str) -> Cow<'static, str> {
        EnUs.err_run_help(program)
    }

    fn err_invalid_timeout(&self) -> Cow<'static, str> {
        EnUs.err_invalid_timeout()
    }

    fn err_no_systemd_conn(&self) -> Cow<'static, str> {
        EnUs.err_no_systemd_conn()
    }

    fn err_switch_failed(&self) -> Cow<'static, str> {
        EnUs.err_switch_failed()
    }

    fn err_argument_missing(&self, option: &str) -> Cow<'static, str> {
        EnUs.err_argument_missing(option)
    }

    fn err_unrecognized_option(&self, option: &str) -> Cow<'static, str> {
        EnUs.err_unrecognized_option(option)
    }

    fn err_option_missing(&self, option: &str) -> Cow<'static, str> {
        EnUs.err_option_missing(option)
    }

    fn err_option_duplicated(&self, option: &str) -> Cow<'static, str> {
        EnUs.err_option_duplicated(option)
    }

    fn err_unexpected_argument(&self, option: &str) -> Cow<'static, str> {
        EnUs.err_unexpected_argument(option)
    }

    fn err_open_system_bus(&self) -> Cow<'static, str> {
        EnUs.err_open_system_bus()
    }

    fn err_open_user_bus(&self) -> Cow<'static, str> {
        EnUs.err_open_user_bus()
    }

    fn err_open_user_bus_alt(&self, bus_path: &str) -> Cow<'static, str> {
        EnUs.err_open_user_bus_alt(bus_path)
    }

    fn err_no_bus_socket(&self, bus_path: &str) -> Cow<'static, str> {
        EnUs.err_no_bus_socket(bus_path)
    }

    fn err_bus_failure(&self) -> Cow<'static, str> {
        EnUs.err_bus_failure()
    }

    fn err_bus_failure_alt(&self, bus_path: &str) -> Cow<'static, str> {
        EnUs.err_bus_failure_alt(bus_path)
    }

    fn warn_alternate_bus(&self, bus_path: &str) -> Cow<'static, str> {
        EnUs.warn_alternate_bus(bus_path)
    }

    fn help_brief(&self, package_version: &str, program: &str) -> Cow<'static, str> {
        EnUs.help_brief(package_version, program)
    }

    fn help_system(&self) -> Cow<'static, str> {
        EnUs.help_system()
    }

    fn help_user(&self) -> Cow<'static, str> {
        EnUs.help_user()
    }

    fn help_new_units(&self) -> Cow<'static, str> {
        EnUs.help_new_units()
    }

    fn help_old_units(&self) -> Cow<'static, str> {
        EnUs.help_old_units()
    }

    fn help_dry_run(&self) -> Cow<'static, str> {
        EnUs.help_dry_run()
    }

    fn help_timeout(&self) -> Cow<'static, str> {
        EnUs.help_timeout()
    }

    fn help_force_systemctl(&self) -> Cow<'static, str> {
        EnUs.help_force_systemctl()
    }

    fn help_verbose(&self) -> Cow<'static, str> {
        EnUs.help_verbose()
    }

    fn help_help(&self) -> Cow<'static, str> {
        EnUs.help_help()
    }

    fn help_version(&self) -> Cow<'static, str> {
        EnUs.help_version()
    }

    fn enable_verbose_output(&self) -> Cow<'static, str> {
        EnUs.enable_verbose_output()
    }

    fn performing_dry_run(&self) -> Cow<'static, str> {
        EnUs.performing_dry_run()
    }
}

impl Messages for EnUs {
    fn err_both_system_and_user(&self) -> Cow<'static, str> {
        "Cannot provide both --system and --user".into()
    }

    fn err_not_a_directory(&self, path: &Path) -> Cow<'static, str> {
        format!("{} is not a directory", path.display()).into()
    }

    fn err_missing_required_option(&self, name: &str) -> Cow<'static, str> {
        format!("Required option '{name}' missing.").into()
    }

    fn err_run_help(&self, program: &str) -> Cow<'static, str> {
        format!("Run '{program} --help' for help.").into()
    }

    fn err_invalid_timeout(&self) -> Cow<'static, str> {
        "Invalid timeout value, expected positive whole number.".into()
    }

    fn err_no_systemd_conn(&self) -> Cow<'static, str> {
        "Failed to connect to systemd using D-Bus or systemctl".into()
    }

    fn err_switch_failed(&self) -> Cow<'static, str> {
        "Error switching systemd units".into()
    }

    fn err_argument_missing(&self, option: &str) -> Cow<'static, str> {
        format!("Argument to option '{option}' missing").into()
    }

    fn err_unrecognized_option(&self, option: &str) -> Cow<'static, str> {
        format!("Unrecognized option: '{option}'").into()
    }

    fn err_option_missing(&self, option: &str) -> Cow<'static, str> {
        format!("Required option '{option}' missing").into()
    }

    fn err_option_duplicated(&self, option: &str) -> Cow<'static, str> {
        format!("Option '{option}' given more than once").into()
    }

    fn err_unexpected_argument(&self, option: &str) -> Cow<'static, str> {
        format!("Option '{option}' does not take an argument").into()
    }

    fn err_open_system_bus(&self) -> Cow<'static, str> {
        "Failed to open system D-Bus connection".into()
    }

    fn err_open_user_bus(&self) -> Cow<'static, str> {
        "Failed to open user session D-Bus connection".into()
    }

    fn err_open_user_bus_alt(&self, bus_path: &str) -> Cow<'static, str> {
        format!("Failed to open session D-Bus connection at {bus_path}").into()
    }

    fn err_no_bus_socket(&self, bus_path: &str) -> Cow<'static, str> {
        format!("No D-Bus socket found at {bus_path}").into()
    }

    fn err_bus_failure(&self) -> Cow<'static, str> {
        "Cannot reach running systemd on standard D-Bus".into()
    }

    fn err_bus_failure_alt(&self, bus_path: &str) -> Cow<'static, str> {
        format!("Failed to open session D-Bus connection at {bus_path}").into()
    }

    fn warn_alternate_bus(&self, bus_path: &str) -> Cow<'static, str> {
        format!(
            "Connected to systemd on D-Bus socket {bus_path}.\n\
             This is different from what is in ${{DBUS_SESSION_BUS_ADDRESS}},\n\
             perhaps you have multiple D-Bus sessions running?"
        )
        .into()
    }

    fn help_brief(&self, package_version: &str, program: &str) -> Cow<'static, str> {
        format!(
            "{package_version}\n\
             \n\
             Given two directories of systemd unit files, this tool will stop,\n\
             start, restart, etc. services in the first directory to match the\n\
             units in the second directory.\n\
             \n\
             Usage: {program} [options]"
        )
        .into()
    }

    fn help_system(&self) -> Cow<'static, str> {
        "Connect to system bus.".into()
    }

    fn help_user(&self) -> Cow<'static, str> {
        "Connect to user bus (the default).".into()
    }

    fn help_new_units(&self) -> Cow<'static, str> {
        "Path to target directory of systemd unit files.".into()
    }

    fn help_old_units(&self) -> Cow<'static, str> {
        "Path to reference directory of systemd unit files.".into()
    }

    fn help_dry_run(&self) -> Cow<'static, str> {
        "Print, but do not perform, the switch actions.".into()
    }

    fn help_timeout(&self) -> Cow<'static, str> {
        "The number of milliseconds to wait for units to start, \
         stop, reload, etc. Default is 120000 (2 minutes)."
            .into()
    }

    fn help_force_systemctl(&self) -> Cow<'static, str> {
        "Force use of systemctl, even if D-Bus is available.".into()
    }

    fn help_verbose(&self) -> Cow<'static, str> {
        "Log all actions.".into()
    }

    fn help_help(&self) -> Cow<'static, str> {
        "Print command help.".into()
    }

    fn help_version(&self) -> Cow<'static, str> {
        "program version.".into()
    }

    fn enable_verbose_output(&self) -> Cow<'static, str> {
        "Enabling verbose output".into()
    }

    fn performing_dry_run(&self) -> Cow<'static, str> {
        "Performing dry-run".into()
    }
}

impl Messages for SvSe {
    fn err_both_system_and_user(&self) -> Cow<'static, str> {
        "Kan inte ange både --system och --user".into()
    }

    fn err_not_a_directory(&self, path: &Path) -> Cow<'static, str> {
        format!("{} är inte en katalog", path.display()).into()
    }

    fn err_missing_required_option(&self, name: &str) -> Cow<'static, str> {
        format!("Obligatoriska flaggan '{name}' saknas.").into()
    }

    fn err_run_help(&self, program: &str) -> Cow<'static, str> {
        format!("Kör '{program} --help' för hjälp.").into()
    }

    fn err_invalid_timeout(&self) -> Cow<'static, str> {
        "Ogiltigt timeout-värde, förväntade ett positivt heltal".into()
    }

    fn err_no_systemd_conn(&self) -> Cow<'static, str> {
        "Lyckades inte koppla till systemd genom D-Bus eller systemctl".into()
    }

    fn err_switch_failed(&self) -> Cow<'static, str> {
        "Fel vid byte av systemd-enheter".into()
    }

    fn err_argument_missing(&self, option: &str) -> Cow<'static, str> {
        format!("Argument till flaggan '{option}' saknas").into()
    }

    fn err_unrecognized_option(&self, option: &str) -> Cow<'static, str> {
        format!("Okänd flagga: '{option}'").into()
    }

    fn err_option_missing(&self, option: &str) -> Cow<'static, str> {
        format!("Obligatorisk flagga '{option}' saknas").into()
    }

    fn err_option_duplicated(&self, option: &str) -> Cow<'static, str> {
        format!("Flagga '{option}' gavs fler än en gång").into()
    }

    fn err_unexpected_argument(&self, option: &str) -> Cow<'static, str> {
        format!("Flagga '{option}' tar inget argument").into()
    }

    fn err_open_system_bus(&self) -> Cow<'static, str> {
        "Lyckades inte öppna en koppling till systemets D-Bus".into()
    }

    fn err_open_user_bus(&self) -> Cow<'static, str> {
        "Lyckades inte öppna en koppling till användarsessionens D-Bus".into()
    }

    fn err_open_user_bus_alt(&self, bus_path: &str) -> Cow<'static, str> {
        format!("Lyckades inte öppna en koppling till D-Bus-socketen {bus_path}").into()
    }

    fn err_no_bus_socket(&self, bus_path: &str) -> Cow<'static, str> {
        format!("Kunde inte hitta en D-Bus-socket på {bus_path}").into()
    }

    fn err_bus_failure(&self) -> Cow<'static, str> {
        "Kan inte nå en körande systemd på vanliga D-Bus".into()
    }

    fn err_bus_failure_alt(&self, bus_path: &str) -> Cow<'static, str> {
        format!("Kan inte nå en körande systemd på D-Bus-socketen {bus_path}").into()
    }

    fn warn_alternate_bus(&self, bus_path: &str) -> Cow<'static, str> {
        format!(
            "Kopplad till systemd på D-Bus-socketen {bus_path}.\n\
             Detta är annorlunda från värdet i ${{DBUS_SESSION_BUS_ADDRESS}},\n\
             kanske du kör flera D-Bus-sessioner samtidigt?"
        )
        .into()
    }

    fn help_brief(&self, package_version: &str, program: &str) -> Cow<'static, str> {
        format!(
            "{package_version}\n\
             \n\
             Givet två kataloger med systemd-enheter så stoppar, startar,\n\
             omstartar, osv. detta verktyg enheter de två katalogerna så att\n\
             systemet matchar andra katalogen.\n\
             \n\
             Användning: {program} [flaggor]"
        )
        .into()
    }

    fn help_system(&self) -> Cow<'static, str> {
        "Koppla till systembussen.".into()
    }

    fn help_user(&self) -> Cow<'static, str> {
        "Koppla till användarbussen (förvalt).".into()
    }

    fn help_new_units(&self) -> Cow<'static, str> {
        "Sökväg till målkatalogen med systemd-enheter.".into()
    }

    fn help_old_units(&self) -> Cow<'static, str> {
        "Sökväg till referenskatalog med systemd-enheter.".into()
    }

    fn help_dry_run(&self) -> Cow<'static, str> {
        "Skriv ut, men utför inte, aktiveringen av enheter.".into()
    }

    fn help_timeout(&self) -> Cow<'static, str> {
        "Antalet millisekunder att vänta på enheter att starta, stoppa, ladda om, etc. \
         Förvalt värde är 120000 (2 minuter)."
            .into()
    }

    fn help_force_systemctl(&self) -> Cow<'static, str> {
        "Tvinga användning av systemctl, även om D-Bus är tillgänglig.".into()
    }

    fn help_verbose(&self) -> Cow<'static, str> {
        "Logga alla händelser.".into()
    }

    fn help_help(&self) -> Cow<'static, str> {
        "Skriv ut kommandohjälpen.".into()
    }

    fn help_version(&self) -> Cow<'static, str> {
        "Skriv ut programversionen.".into()
    }

    fn enable_verbose_output(&self) -> Cow<'static, str> {
        "Aktiverar detaljerad utskrift".into()
    }

    fn performing_dry_run(&self) -> Cow<'static, str> {
        "Utför simulerad körning".into()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn can_handle_no_locales() {
        let langs = vec![].into_iter();

        assert_eq!(init_messages(langs).help_help(), EnUs.help_help());
    }

    #[test]
    fn can_initialize_known_locale() {
        let langs = vec!["sv-SE".to_string()].into_iter();

        assert_eq!(init_messages(langs).help_help(), SvSe.help_help());
    }

    #[test]
    fn ignores_unparseable_locale() {
        let langs = vec!["C".to_string()].into_iter();

        assert_eq!(init_messages(langs).help_help(), EnUs.help_help());
    }
}