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 super::loader::ProductThemes;
14use crate::product::ProductFiles;
15
16/// Records `theme` as the saved theme when `context`'s product has neither a
17/// config file nor a state file: a new install. Returns whether it recorded.
18/// `product_themes` are the app's own theme documents
19/// (`ProductBuild.product_themes`, `&[]` for none), which `theme` may be a
20/// variant of. An existing install, including one whose files are unreadable
21/// or malformed, is left alone. Nothing is written, and an error returned,
22/// when a new install's product themes fail the checks startup makes, when
23/// `theme` is not a variant of the product's theme catalogue, or when the
24/// files' folder cannot be read, so a new install cannot be told from an
25/// existing one. Only the desktop app calls this, before it starts;
26/// command-line and headless runs must not.
27// qual:api (apps call it from their entrypoint, before the desktop starts)
28pub fn record_new_install_theme(
29    context: &ProductContext,
30    theme: &str,
31    product_themes: ProductThemes,
32) -> Result<bool, String> {
33    let files = ProductFiles::for_context(context);
34    let new = is_new_install(&files).map_err(|error| file_error(&files, &error))?;
35    new.then(|| record(&context.product, &files, theme, product_themes))
36        .transpose()
37        .map(|recorded| recorded.is_some())
38}
39
40/// Saves `theme`, a variant of `product`'s catalogue, into a new state file,
41/// once `documents` pass startup's checks.
42fn record(
43    product: &ValidatedProduct,
44    files: &ProductFiles,
45    theme: &str,
46    documents: ProductThemes,
47) -> Result<(), String> {
48    super::loader::validate_product_themes(product, documents)?;
49    super::loader::require_catalogue_variant(product, "new install theme", theme, documents)?;
50    save_theme(&files.state, theme).map_err(|error| file_error(files, &error))
51}
52
53fn save_theme(file: &Path, theme: &str) -> io::Result<()> {
54    let mut state = State::default();
55    state.set(THEME_PREF, theme);
56    state.save(file)
57}
58
59fn file_error(files: &ProductFiles, error: &io::Error) -> String {
60    format!(
61        "could not record the new install's theme in {}: {error}",
62        files.state.display()
63    )
64}
65
66/// True when neither the config file nor the state file exists. Both are
67/// checked without following links, so a dangling link counts as a file.
68fn is_new_install(files: &ProductFiles) -> io::Result<bool> {
69    Ok(absent(&files.config)? && absent(&files.state)?)
70}
71
72/// Whether nothing is at `path`. An error other than "not found", such as an
73/// unreadable folder, is returned rather than guessed at.
74fn absent(path: &Path) -> io::Result<bool> {
75    match std::fs::symlink_metadata(path) {
76        Ok(_) => Ok(false),
77        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(true),
78        Err(error) => Err(error),
79    }
80}