mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 20:25:06 +00:00
Merge branch 'fw/merge-i' into area/fwb2
This commit is contained in:
@@ -134,6 +134,24 @@ pub fn resolve(app_id: &str, portable: &Portable, location: &Location, probe: &P
|
||||
/// folder (`Photocraft` on macOS and Windows) keeps them. The `<APP>_CONFIG_DIR` variable still
|
||||
/// comes from `app_id`.
|
||||
pub fn resolve_named(app_id: &str, folder: &str, portable: &Portable, location: &Location, probe: &Probe) -> Resolved {
|
||||
resolve_checked(app_id, folder, portable, location, probe, ensure_writable)
|
||||
}
|
||||
|
||||
/// [`resolve_named`] without touching the disk: a portable folder is taken as found, without creating it or
|
||||
/// checking that it is writable. For [`SettingsStore::read`](crate::SettingsStore::read), which writes nothing.
|
||||
pub(crate) fn resolve_unprobed(app_id: &str, folder: &str, portable: &Portable, location: &Location, probe: &Probe) -> Resolved {
|
||||
resolve_checked(app_id, folder, portable, location, probe, |_| Ok(()))
|
||||
}
|
||||
|
||||
/// The folder rules, with `writable` deciding whether a portable folder is used.
|
||||
fn resolve_checked(
|
||||
app_id: &str,
|
||||
folder: &str,
|
||||
portable: &Portable,
|
||||
location: &Location,
|
||||
probe: &Probe,
|
||||
writable: impl Fn(&Path) -> std::io::Result<()>,
|
||||
) -> Resolved {
|
||||
match location {
|
||||
Location::Dir(dir) => return Resolved::new(Some(dir.clone()), Rule::Override),
|
||||
Location::Memory => return Resolved::new(None, Rule::Memory),
|
||||
@@ -148,7 +166,7 @@ pub fn resolve_named(app_id: &str, folder: &str, portable: &Portable, location:
|
||||
}
|
||||
let mut warning = None;
|
||||
if let Some((marker, dir)) = probe.exe_dir.as_deref().and_then(|d| portable_dir(d, portable)) {
|
||||
match ensure_writable(&dir) {
|
||||
match writable(&dir) {
|
||||
Ok(()) => return Resolved::new(Some(dir), Rule::Portable),
|
||||
Err(e) => {
|
||||
let instead = match &probe.platform_config_dir {
|
||||
|
||||
@@ -29,6 +29,15 @@ pub const FIRST_RETRY_DELAY: Duration = Duration::from_secs(1);
|
||||
/// Longest wait between retries.
|
||||
pub const MAX_RETRY_DELAY: Duration = Duration::from_secs(30);
|
||||
|
||||
/// What a load does with a `config.toml` that is not TOML.
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum Damaged {
|
||||
/// Move it aside, so the next save starts a good file (an opened store).
|
||||
MoveAside,
|
||||
/// Leave it where it is (a read).
|
||||
Leave,
|
||||
}
|
||||
|
||||
/// How to open a store.
|
||||
pub struct OpenOptions<T> {
|
||||
/// Where the settings live.
|
||||
@@ -136,35 +145,7 @@ impl<T: Settings> SettingsStore<T> {
|
||||
pub fn open_with(options: OpenOptions<T>) -> (Self, LoadReport) {
|
||||
let portable = Portable { markers: T::PORTABLE_MARKERS, dir: T::PORTABLE_DIR };
|
||||
let resolved = paths::resolve_named(T::APP_ID, T::FOLDER_NAME, &portable, &options.location, &options.probe);
|
||||
let path = resolved.dir.as_ref().map(|d| d.join(CONFIG_FILE));
|
||||
let mut report = LoadReport {
|
||||
dir: resolved.dir.clone(),
|
||||
rule: resolved.rule.to_string(),
|
||||
warning: resolved.warning.clone(),
|
||||
source: Source::Defaults,
|
||||
broken_moved_to: None,
|
||||
section_errors: Vec::new(),
|
||||
migrated_from: None,
|
||||
read_only: None,
|
||||
};
|
||||
let mut store = Self {
|
||||
dir: resolved.dir,
|
||||
path,
|
||||
doc: document::new_document(T::SCHEMA_VERSION),
|
||||
base: default_document::<T>(),
|
||||
files: options.files,
|
||||
value: T::default(),
|
||||
read_only: None,
|
||||
dirty: false,
|
||||
written: false,
|
||||
last_change: None,
|
||||
debounce: options.debounce,
|
||||
failures: 0,
|
||||
retry_at: None,
|
||||
seen_text: None,
|
||||
moved_aside: Vec::new(),
|
||||
merged_change: false,
|
||||
};
|
||||
let (mut store, mut report) = Self::unloaded(resolved, options.files, options.debounce);
|
||||
|
||||
let (Some(dir), Some(path)) = (store.dir.clone(), store.path.clone()) else {
|
||||
return (store, report);
|
||||
@@ -179,19 +160,56 @@ impl<T: Settings> SettingsStore<T> {
|
||||
store.set_read_only(&mut report, ReadOnlyReason::DirNotWritable(e));
|
||||
return (store, report);
|
||||
}
|
||||
store.load(&path, &options.legacy, Damaged::MoveAside, &mut report);
|
||||
(store, report)
|
||||
}
|
||||
|
||||
match store.files.read(&path) {
|
||||
Ok(Some(text)) => store.load_text(&path, &text, &mut report),
|
||||
Ok(None) => {
|
||||
if let Some((value, errors, from)) = migrate::import_legacy(&options.legacy) {
|
||||
store.value = value;
|
||||
store.dirty = true;
|
||||
report.source = Source::Legacy(from);
|
||||
report.section_errors = errors;
|
||||
}
|
||||
}
|
||||
Err(e) => store.set_read_only(&mut report, ReadOnlyReason::Unreadable(e)),
|
||||
/// The settings [`open_with`](Self::open_with) would load from `options`, read without opening a store: for a
|
||||
/// second process, or a read before the application's store opens. It takes the same load path, and touches
|
||||
/// nothing on disk: no folder is created, none is checked for writing (a portable folder is taken as found), a
|
||||
/// damaged `config.toml` reads as defaults and stays where it is, and with no `config.toml` the legacy files are
|
||||
/// imported in memory only.
|
||||
pub fn read(options: &OpenOptions<T>) -> (T, LoadReport) {
|
||||
let portable = Portable { markers: T::PORTABLE_MARKERS, dir: T::PORTABLE_DIR };
|
||||
let resolved = paths::resolve_unprobed(T::APP_ID, T::FOLDER_NAME, &portable, &options.location, &options.probe);
|
||||
let (mut store, mut report) = Self::unloaded(resolved, options.files.clone(), options.debounce);
|
||||
if let Some(path) = store.path.clone() {
|
||||
store.load(&path, &options.legacy, Damaged::Leave, &mut report);
|
||||
}
|
||||
(std::mem::take(&mut store.value), report)
|
||||
}
|
||||
|
||||
/// A store at `resolved` holding the defaults, before anything is read.
|
||||
fn unloaded(resolved: paths::Resolved, files: Arc<dyn SettingsFiles>, debounce: Duration) -> (Self, LoadReport) {
|
||||
let path = resolved.dir.as_ref().map(|d| d.join(CONFIG_FILE));
|
||||
let report = LoadReport {
|
||||
dir: resolved.dir.clone(),
|
||||
rule: resolved.rule.to_string(),
|
||||
warning: resolved.warning.clone(),
|
||||
source: Source::Defaults,
|
||||
broken_moved_to: None,
|
||||
section_errors: Vec::new(),
|
||||
migrated_from: None,
|
||||
read_only: None,
|
||||
};
|
||||
let store = Self {
|
||||
dir: resolved.dir,
|
||||
path,
|
||||
doc: document::new_document(T::SCHEMA_VERSION),
|
||||
base: default_document::<T>(),
|
||||
files,
|
||||
value: T::default(),
|
||||
read_only: None,
|
||||
dirty: false,
|
||||
written: false,
|
||||
last_change: None,
|
||||
debounce,
|
||||
failures: 0,
|
||||
retry_at: None,
|
||||
seen_text: None,
|
||||
moved_aside: Vec::new(),
|
||||
merged_change: false,
|
||||
};
|
||||
(store, report)
|
||||
}
|
||||
|
||||
@@ -343,13 +361,32 @@ impl<T: Settings> SettingsStore<T> {
|
||||
}
|
||||
}
|
||||
|
||||
fn load_text(&mut self, path: &Path, text: &str, report: &mut LoadReport) {
|
||||
/// Loads `config.toml` at `path`, or with none the first of the `legacy` files that imports. An imported or
|
||||
/// upgraded value marks the store dirty, so an opened store saves it.
|
||||
fn load(&mut self, path: &Path, legacy: &[LegacyFile<T>], damaged: Damaged, report: &mut LoadReport) {
|
||||
match self.files.read(path) {
|
||||
Ok(Some(text)) => self.load_text(path, &text, damaged, report),
|
||||
Ok(None) => {
|
||||
if let Some((value, errors, from)) = migrate::import_legacy(legacy) {
|
||||
self.value = value;
|
||||
self.dirty = true;
|
||||
report.source = Source::Legacy(from);
|
||||
report.section_errors = errors;
|
||||
}
|
||||
}
|
||||
Err(e) => self.set_read_only(report, ReadOnlyReason::Unreadable(e)),
|
||||
}
|
||||
}
|
||||
|
||||
fn load_text(&mut self, path: &Path, text: &str, damaged: Damaged, report: &mut LoadReport) {
|
||||
let mut doc = match document::parse(text) {
|
||||
Ok(doc) => doc,
|
||||
Err(_) => {
|
||||
match self.files.move_aside(path) {
|
||||
Ok(moved) => report.broken_moved_to = Some(moved),
|
||||
Err(e) => self.set_read_only(report, ReadOnlyReason::CannotMoveAside(e)),
|
||||
if damaged == Damaged::MoveAside {
|
||||
match self.files.move_aside(path) {
|
||||
Ok(moved) => report.broken_moved_to = Some(moved),
|
||||
Err(e) => self.set_read_only(report, ReadOnlyReason::CannotMoveAside(e)),
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -140,3 +140,49 @@ fn a_map_entry_the_app_removes_is_removed_from_the_file() {
|
||||
let (last, _) = open_keyed(tmp.path());
|
||||
assert!(last.get().shortcuts.is_empty(), "{}", read_config(tmp.path()));
|
||||
}
|
||||
|
||||
/// Options for a `Prefs` store in `dir`, importing `legacy.txt` (its text is the language) when there is no
|
||||
/// `config.toml`.
|
||||
fn read_options(dir: &Path) -> OpenOptions<Prefs> {
|
||||
fn parse(bytes: &[u8]) -> Result<(Prefs, Vec<craft_settings::SectionError>), String> {
|
||||
let mut prefs = Prefs::default();
|
||||
prefs.general.language = String::from_utf8(bytes.to_vec()).map_err(|e| e.to_string())?;
|
||||
Ok((prefs, Vec::new()))
|
||||
}
|
||||
let legacy = craft_settings::LegacyFile { path: dir.join("legacy.txt"), parse };
|
||||
OpenOptions::new(Location::Dir(dir.to_path_buf())).probe(empty_probe()).legacy(vec![legacy])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_read_sees_what_a_store_wrote_and_touches_nothing_on_disk() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
|
||||
// A folder that does not exist reads as defaults and is not created.
|
||||
let missing = tmp.path().join("missing");
|
||||
let (prefs, _) = SettingsStore::<Prefs>::read(&read_options(&missing));
|
||||
assert_eq!(prefs, Prefs::default());
|
||||
assert!(!missing.exists(), "a read creates no folder");
|
||||
|
||||
// With no config.toml the legacy file is imported in memory only.
|
||||
std::fs::write(tmp.path().join("legacy.txt"), "nl").unwrap();
|
||||
let (prefs, _) = SettingsStore::<Prefs>::read(&read_options(tmp.path()));
|
||||
assert_eq!(prefs.general.language, "nl");
|
||||
assert!(!config_path(tmp.path()).exists(), "a read writes no config.toml");
|
||||
|
||||
// What an opened store saved is what a read sees, ahead of the legacy file.
|
||||
let (mut store, _) = SettingsStore::open_with(read_options(tmp.path()));
|
||||
store.update(|p| p.general.language = "de".into());
|
||||
store.flush().unwrap();
|
||||
let (prefs, _) = SettingsStore::<Prefs>::read(&read_options(tmp.path()));
|
||||
assert_eq!(prefs.general.language, "de");
|
||||
|
||||
// A damaged config.toml reads as defaults and stays where it is, unchanged.
|
||||
let broken = "schema_version = 2\n[general\nlanguage = \"fr\"\n";
|
||||
write_config(tmp.path(), broken);
|
||||
let (prefs, report) = SettingsStore::<Prefs>::read(&read_options(tmp.path()));
|
||||
assert_eq!(prefs.general.language, Prefs::default().general.language);
|
||||
assert!(report.broken_moved_to.is_none());
|
||||
assert_eq!(read_config(tmp.path()), broken, "a read never moves or rewrites the file");
|
||||
let moved = std::fs::read_dir(tmp.path()).unwrap().flatten().any(|e| e.file_name().to_string_lossy().starts_with("config.broken-"));
|
||||
assert!(!moved, "nothing is moved aside");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user