installrs 0.1.0-rc6

Build self-contained software installers in plain Rust, with an optional native wizard GUI (Win32 / GTK3), component selection, progress, cancellation, and compression.
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
use anyhow::Result;
use std::sync::{Arc, Mutex};

use crate::Installer;

/// Callback type for the install page closure.
pub type InstallCallback = Box<dyn FnOnce(&mut GuiContext) -> Result<()> + Send + 'static>;

/// Callback that runs after a page becomes visible via forward navigation.
/// Back navigation does not fire this callback.
pub type OnEnterCallback = Box<dyn Fn(&mut PageContext) -> Result<()> + 'static>;

/// Callback that runs before forward navigation away from a page.
/// Returning `Ok(false)` cancels the navigation. Back navigation does not
/// fire this callback.
pub type OnBeforeLeaveCallback = Box<dyn Fn(&mut PageContext) -> Result<bool> + 'static>;

/// Pure predicate: returns `true` to hide the page. Evaluated every time
/// the wizard navigates past it, so the outcome can change mid-wizard as
/// installer state (options, component selection, etc.) evolves. Must not
/// mutate state — run side effects from `on_enter` instead.
pub type SkipIfCallback = Box<dyn Fn(&PageContext) -> bool + 'static>;

/// Callback that runs at wizard startup (before the window is shown, or
/// before the install callback fires in headless mode). Inspect
/// `installer.headless` to branch on mode.
pub type StartCallback = Box<dyn FnOnce(&mut crate::Installer) -> Result<()> + 'static>;

/// Callback that runs at wizard exit (after the window closes, or after
/// the install callback completes in headless mode). Always runs, even on
/// failure.
pub type ExitCallback = Box<dyn FnOnce(&mut crate::Installer) -> Result<()> + 'static>;

/// Configuration for the wizard-style installer GUI.
pub struct WizardConfig {
    pub title: String,
    pub pages: Vec<ConfiguredPage>,
    pub buttons: ButtonLabels,
    pub on_start: Option<StartCallback>,
    pub on_exit: Option<ExitCallback>,
}

/// A wizard page with optional navigation callbacks.
pub struct ConfiguredPage {
    pub page: WizardPage,
    pub on_enter: Option<OnEnterCallback>,
    pub on_before_leave: Option<OnBeforeLeaveCallback>,
    pub skip_if: Option<SkipIfCallback>,
}

impl ConfiguredPage {
    pub fn new(page: WizardPage) -> Self {
        Self {
            page,
            on_enter: None,
            on_before_leave: None,
            skip_if: None,
        }
    }
}

/// Labels for the wizard navigation buttons. Customize via
/// [`InstallerGui::buttons`](crate::gui::InstallerGui::buttons) to translate
/// or rename them.
pub struct ButtonLabels {
    pub back: String,
    pub next: String,
    pub install: String,
    pub uninstall: String,
    pub finish: String,
    pub cancel: String,
}

impl Default for ButtonLabels {
    fn default() -> Self {
        Self {
            back: "< Back".into(),
            next: "Next >".into(),
            install: "Install".into(),
            uninstall: "Uninstall".into(),
            finish: "Finish".into(),
            cancel: "Cancel".into(),
        }
    }
}

/// A single page in the wizard flow.
pub enum WizardPage {
    Welcome {
        title: String,
        message: String,
    },
    License {
        heading: String,
        text: String,
        accept_label: String,
    },
    Components {
        heading: String,
        label: String,
    },
    DirectoryPicker {
        heading: String,
        label: String,
        default: String,
    },
    Install {
        callback: InstallCallback,
        /// When true, the Next button preceding this page (and visible while
        /// on it) renders `buttons.uninstall` instead of `buttons.install`.
        /// Set via [`crate::gui::InstallerGui::uninstall_page`].
        is_uninstall: bool,
    },
    Finish {
        title: String,
        message: String,
    },
    /// Shown after the install page when the install callback returns an
    /// error or the user cancels mid-install. The actual error text is
    /// populated at runtime.
    Error {
        title: String,
        message: String,
    },
    /// A user-defined page that lays out a column of simple widgets (text
    /// inputs, checkboxes, dropdowns). Each widget is tied to an
    /// [`crate::OptionValue`] keyed by `key`; values are pre-filled from the
    /// installer's options store on entry and written back on forward
    /// navigation. Validation belongs in `on_before_leave`.
    Custom {
        heading: String,
        label: String,
        widgets: Vec<CustomWidget>,
    },
}

/// A single row on a [`WizardPage::Custom`]. Built via
/// [`CustomPageBuilder`] and rendered as a labeled input control in a
/// vertical column.
#[derive(Clone, Debug)]
pub enum CustomWidget {
    /// Single-line text entry (optionally masked as a password).
    Text {
        key: String,
        label: String,
        default: String,
        password: bool,
    },
    /// Multi-line text area (`rows` lines tall).
    Multiline {
        key: String,
        label: String,
        default: String,
        rows: u32,
    },
    /// Integer entry. Stored as `OptionValue::Int`.
    Number {
        key: String,
        label: String,
        default: i64,
    },
    /// Single boolean toggle (the label sits next to the checkbox itself).
    Checkbox {
        key: String,
        label: String,
        default: bool,
    },
    /// One-of-N dropdown. `choices` is `(value, display_label)` — the
    /// stored option value is the `value` field of the selected pair.
    Dropdown {
        key: String,
        label: String,
        choices: Vec<(String, String)>,
        default: String,
    },
    /// Vertical stack of radio buttons; same (value, display) shape as
    /// [`CustomWidget::Dropdown`]. Exactly one is selected at a time.
    Radio {
        key: String,
        label: String,
        choices: Vec<(String, String)>,
        default: String,
    },
    /// Text entry + Browse button that opens a native file-open dialog.
    /// `filters` is `(display_label, glob_pattern)` — e.g.
    /// `[("Config", "*.toml;*.yaml"), ("All files", "*.*")]`.
    FilePicker {
        key: String,
        label: String,
        default: String,
        filters: Vec<(String, String)>,
    },
    /// Text entry + Browse button that opens a native folder-picker dialog.
    DirPicker {
        key: String,
        label: String,
        default: String,
    },
}

/// Builds the widget list for a [`WizardPage::Custom`]. Each method
/// appends one widget to the column; the returned `&mut Self` lets you
/// chain. Widget keys are the same string used by
/// [`crate::Installer::get_option`] / [`crate::Installer::option_value`]
/// — so CLI `--<key>=<value>` overrides (when the matching option is
/// registered via [`crate::Installer::option`] before
/// `process_commandline`) pre-fill the field.
pub struct CustomPageBuilder {
    pub(crate) widgets: Vec<CustomWidget>,
}

impl CustomPageBuilder {
    pub(crate) fn new() -> Self {
        Self {
            widgets: Vec::new(),
        }
    }

    /// Add a single-line text entry.
    pub fn text(&mut self, key: &str, label: &str, default: &str) -> &mut Self {
        self.widgets.push(CustomWidget::Text {
            key: key.into(),
            label: label.into(),
            default: default.into(),
            password: false,
        });
        self
    }

    /// Add a masked single-line password entry.
    pub fn password(&mut self, key: &str, label: &str) -> &mut Self {
        self.widgets.push(CustomWidget::Text {
            key: key.into(),
            label: label.into(),
            default: String::new(),
            password: true,
        });
        self
    }

    /// Add a checkbox with the given label.
    pub fn checkbox(&mut self, key: &str, label: &str, default: bool) -> &mut Self {
        self.widgets.push(CustomWidget::Checkbox {
            key: key.into(),
            label: label.into(),
            default,
        });
        self
    }

    /// Add a dropdown. `choices` is `(value, display_label)`; `default` is
    /// the `value` of the initially-selected entry (use the first entry's
    /// value if you don't care).
    pub fn dropdown(
        &mut self,
        key: &str,
        label: &str,
        choices: &[(&str, &str)],
        default: &str,
    ) -> &mut Self {
        self.widgets.push(CustomWidget::Dropdown {
            key: key.into(),
            label: label.into(),
            choices: choices
                .iter()
                .map(|(v, d)| ((*v).into(), (*d).into()))
                .collect(),
            default: default.into(),
        });
        self
    }

    /// Add a radio-button group. Same shape as [`Self::dropdown`].
    pub fn radio(
        &mut self,
        key: &str,
        label: &str,
        choices: &[(&str, &str)],
        default: &str,
    ) -> &mut Self {
        self.widgets.push(CustomWidget::Radio {
            key: key.into(),
            label: label.into(),
            choices: choices
                .iter()
                .map(|(v, d)| ((*v).into(), (*d).into()))
                .collect(),
            default: default.into(),
        });
        self
    }

    /// Add an integer entry. Stored as `OptionValue::Int`.
    pub fn number(&mut self, key: &str, label: &str, default: i64) -> &mut Self {
        self.widgets.push(CustomWidget::Number {
            key: key.into(),
            label: label.into(),
            default,
        });
        self
    }

    /// Add a multi-line text area `rows` lines tall.
    pub fn multiline(&mut self, key: &str, label: &str, default: &str, rows: u32) -> &mut Self {
        self.widgets.push(CustomWidget::Multiline {
            key: key.into(),
            label: label.into(),
            default: default.into(),
            rows,
        });
        self
    }

    /// Add a file-picker (text entry + Browse button → native file-open
    /// dialog). `filters` is `(display, glob)` — e.g.
    /// `&[("Config", "*.toml;*.yaml"), ("All files", "*.*")]`.
    pub fn file_picker(
        &mut self,
        key: &str,
        label: &str,
        default: &str,
        filters: &[(&str, &str)],
    ) -> &mut Self {
        self.widgets.push(CustomWidget::FilePicker {
            key: key.into(),
            label: label.into(),
            default: default.into(),
            filters: filters
                .iter()
                .map(|(d, p)| ((*d).into(), (*p).into()))
                .collect(),
        });
        self
    }

    /// Add a directory-picker (text entry + Browse button → native
    /// folder-picker dialog).
    pub fn dir_picker(&mut self, key: &str, label: &str, default: &str) -> &mut Self {
        self.widgets.push(CustomWidget::DirPicker {
            key: key.into(),
            label: label.into(),
            default: default.into(),
        });
        self
    }
}

/// Messages sent from the background install thread to the GUI thread.
pub enum GuiMessage {
    SetStatus(String),
    SetProgress(f64),
    Log(String),
    Finished(Result<()>),
}

/// A [`crate::ProgressSink`] implementation that forwards events over the
/// wizard's GUI message channel.
struct ChannelSink {
    tx: std::sync::mpsc::Sender<GuiMessage>,
}

impl crate::ProgressSink for ChannelSink {
    fn set_status(&self, status: &str) {
        let _ = self.tx.send(GuiMessage::SetStatus(status.to_string()));
    }
    fn set_progress(&self, fraction: f64) {
        let _ = self.tx.send(GuiMessage::SetProgress(fraction));
    }
    fn log(&self, message: &str) {
        let _ = self.tx.send(GuiMessage::Log(message.to_string()));
    }
}

/// Context passed to the install closure, providing thread-safe GUI updates
/// and access to the `Installer`.
pub struct GuiContext {
    tx: std::sync::mpsc::Sender<GuiMessage>,
    installer: Arc<Mutex<Installer>>,
    install_dir: Arc<Mutex<String>>,
    cancelled: Arc<std::sync::atomic::AtomicBool>,
}

impl GuiContext {
    pub fn new(
        tx: std::sync::mpsc::Sender<GuiMessage>,
        installer: Arc<Mutex<Installer>>,
        install_dir: Arc<Mutex<String>>,
        cancelled: Arc<std::sync::atomic::AtomicBool>,
    ) -> Self {
        Self {
            tx,
            installer,
            install_dir,
            cancelled,
        }
    }

    /// Update the status label on the install page.
    pub fn set_status(&self, status: &str) {
        let _ = self.tx.send(GuiMessage::SetStatus(status.to_string()));
    }

    /// Update the progress bar (0.0 to 1.0).
    pub fn set_progress(&self, progress: f64) {
        let _ = self.tx.send(GuiMessage::SetProgress(progress));
    }

    /// Append a log line to the install page log area.
    pub fn log(&self, message: &str) {
        let _ = self.tx.send(GuiMessage::Log(message.to_string()));
    }

    /// Get the currently selected install directory.
    pub fn install_dir(&self) -> String {
        self.install_dir.lock().unwrap().clone()
    }

    /// Get a mutable reference to the `Installer` for calling
    /// [`Installer::file`], [`Installer::dir`], etc.
    ///
    /// The returned guard holds a mutex lock — avoid holding it across GUI calls.
    pub fn installer(&self) -> std::sync::MutexGuard<'_, Installer> {
        self.installer.lock().unwrap()
    }

    /// Build a [`crate::ProgressSink`] that forwards to this GUI context.
    ///
    /// Attach it via `ctx.installer().set_progress_sink(ctx.progress_sink())`
    /// (or rely on the wizard, which does this automatically before invoking
    /// the install-page callback).
    pub fn progress_sink(&self) -> Box<dyn crate::ProgressSink> {
        Box::new(ChannelSink {
            tx: self.tx.clone(),
        })
    }

    /// Check if the user has requested cancellation.
    pub fn is_cancelled(&self) -> bool {
        self.cancelled.load(std::sync::atomic::Ordering::Relaxed)
    }
}

/// Context passed to page `on_enter` / `on_before_leave` callbacks. Unlike
/// [`GuiContext`], these callbacks run synchronously on the GUI thread.
pub struct PageContext {
    installer: Arc<Mutex<Installer>>,
    install_dir: Arc<Mutex<String>>,
    cancelled: Arc<std::sync::atomic::AtomicBool>,
}

impl PageContext {
    pub fn new(
        installer: Arc<Mutex<Installer>>,
        install_dir: Arc<Mutex<String>>,
        cancelled: Arc<std::sync::atomic::AtomicBool>,
    ) -> Self {
        Self {
            installer,
            install_dir,
            cancelled,
        }
    }

    /// Get a mutable reference to the `Installer`.
    pub fn installer(&self) -> std::sync::MutexGuard<'_, Installer> {
        self.installer.lock().unwrap()
    }

    /// Get the currently selected install directory.
    pub fn install_dir(&self) -> String {
        self.install_dir.lock().unwrap().clone()
    }

    /// Override the install directory.
    pub fn set_install_dir(&self, dir: &str) {
        *self.install_dir.lock().unwrap() = dir.to_string();
    }

    /// Check if the user has requested cancellation.
    pub fn is_cancelled(&self) -> bool {
        self.cancelled.load(std::sync::atomic::Ordering::Relaxed)
    }
}