subspace_shell/theme/
new_install.rs1use 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
15pub 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
32fn 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
51fn is_new_install(files: &ProductFiles) -> io::Result<bool> {
54 Ok(absent(&files.config)? && absent(&files.state)?)
55}
56
57fn 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}