Skip to main content

subspace_shell/theme/
new_install.rs

1//! A product's theme for a new install, recorded as the saved theme at the
2//! desktop app's first start. Once recorded it is the user's choice like any
3//! other, so a later change of the product's default never moves it, and an
4//! install with no recorded theme keeps showing the product's default.
5
6use std::io;
7use std::path::Path;
8
9use subspace_core::state::State;
10use subspace_product::{ProductContext, ValidatedProduct};
11
12use super::THEME_PREF;
13use crate::product::ProductFiles;
14
15/// Records `theme` as the saved theme when `context`'s product has neither a
16/// config file nor a state file: a new install. Returns whether it recorded.
17/// An existing install, including one whose files are unreadable or
18/// malformed, is left alone. Nothing is written, and an error returned, when
19/// a new install's `theme` is not a variant of the product's theme catalogue
20/// or when the files' folder cannot be read, so a new install cannot be told
21/// from an existing one. Only the desktop app calls this, before it starts;
22/// command-line and headless runs must not.
23// qual:api (apps call it from their entrypoint, before the desktop starts)
24pub fn record_new_install_theme(context: &ProductContext, theme: &str) -> Result<bool, String> {
25    let files = ProductFiles::for_context(context);
26    let new = is_new_install(&files).map_err(|error| file_error(&files, &error))?;
27    new.then(|| record(&context.product, &files, theme))
28        .transpose()
29        .map(|recorded| recorded.is_some())
30}
31
32/// Saves `theme`, a variant of `product`'s catalogue, into a new state file.
33fn record(product: &ValidatedProduct, files: &ProductFiles, theme: &str) -> Result<(), String> {
34    super::loader::require_catalogue_variant(product, "new install theme", theme)?;
35    save_theme(&files.state, theme).map_err(|error| file_error(files, &error))
36}
37
38fn save_theme(file: &Path, theme: &str) -> io::Result<()> {
39    let mut state = State::default();
40    state.set(THEME_PREF, theme);
41    state.save(file)
42}
43
44fn file_error(files: &ProductFiles, error: &io::Error) -> String {
45    format!(
46        "could not record the new install's theme in {}: {error}",
47        files.state.display()
48    )
49}
50
51/// True when neither the config file nor the state file exists. Both are
52/// checked without following links, so a dangling link counts as a file.
53fn is_new_install(files: &ProductFiles) -> io::Result<bool> {
54    Ok(absent(&files.config)? && absent(&files.state)?)
55}
56
57/// Whether nothing is at `path`. An error other than "not found", such as an
58/// unreadable folder, is returned rather than guessed at.
59fn absent(path: &Path) -> io::Result<bool> {
60    match std::fs::symlink_metadata(path) {
61        Ok(_) => Ok(false),
62        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(true),
63        Err(error) => Err(error),
64    }
65}