Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f3adb8c

Browse files
committedAug 9, 2023
Store config on all changes
1 parent 389f561 commit f3adb8c

File tree

1 file changed

+7
-36
lines changed

1 file changed

+7
-36
lines changed
 

‎src/config.rs

+7-36
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,12 @@ const PAGE_KEY: &str = "page";
2424
struct ItemStore {
2525
path: PathBuf,
2626
items: HashMap<String, String>,
27-
modified: bool,
2827
}
2928

3029
impl ItemStore {
3130
fn persist(&mut self) {
32-
if self.modified {
33-
let file = ok!(File::create(&self.path));
34-
ron::ser::to_writer_pretty(file, &self.items, Default::default()).wrest();
35-
self.modified = false;
36-
}
37-
}
38-
}
39-
40-
impl Drop for ItemStore {
41-
fn drop(&mut self) {
42-
self.persist();
31+
let file = ok!(File::create(&self.path));
32+
ron::ser::to_writer_pretty(file, &self.items, Default::default()).wrest();
4333
}
4434
}
4535

@@ -52,12 +42,7 @@ impl Config {
5242
pub fn new() -> Option<Self> {
5343
let path = Self::path()?;
5444
let items = Self::load(&path);
55-
let store = ItemStore {
56-
path,
57-
items,
58-
modified: false,
59-
};
60-
45+
let store = ItemStore { path, items };
6146
Some(Self {
6247
store: Arc::new(RwLock::new(store)),
6348
})
@@ -85,20 +70,19 @@ impl Config {
8570
pub fn set_page(&mut self, page: Page) {
8671
let text = ok!(ron::to_string(&page));
8772
self.set(PAGE_KEY, text);
88-
self.persist();
8973
}
9074

9175
pub fn get_log_path(&self) -> Option<PathBuf> {
9276
if let Some(folder) = self.get(LOG_PATH_KEY) {
9377
return Some(PathBuf::from(folder));
9478
}
79+
9580
Self::get_default_log_path()
9681
}
9782

9883
pub fn set_log_path(&mut self, folder: &Path) {
9984
if let Some(folder) = folder.to_str() {
10085
self.set(LOG_PATH_KEY, folder.to_owned());
101-
self.persist();
10286
} else {
10387
println!("Unable to convert path to string: {folder:?}");
10488
}
@@ -108,13 +92,13 @@ impl Config {
10892
if let Some(folder) = self.get(SAVE_PATH_KEY) {
10993
return Some(PathBuf::from(folder));
11094
}
95+
11196
Self::get_default_save_path()
11297
}
11398

11499
pub fn set_save_path(&mut self, folder: &Path) {
115100
if let Some(folder) = folder.to_str() {
116101
self.set(SAVE_PATH_KEY, folder.to_owned());
117-
self.persist();
118102
} else {
119103
println!("Unable to convert path to string: {folder:?}");
120104
}
@@ -130,7 +114,6 @@ impl Config {
130114
}
131115

132116
self.set(STATS_AVATAR_KEY, avatar);
133-
self.persist();
134117
}
135118

136119
pub fn get_exp_avatar(&self) -> Option<String> {
@@ -143,7 +126,6 @@ impl Config {
143126
}
144127

145128
self.set(EXP_AVATAR_KEY, avatar);
146-
self.persist();
147129
}
148130

149131
pub fn get_notes(&self, avatar: &str) -> Option<String> {
@@ -168,7 +150,6 @@ impl Config {
168150
}
169151

170152
self.set(&key, notes);
171-
self.persist();
172153
}
173154

174155
pub fn get_plants(&self) -> Option<Vec<Plant>> {
@@ -181,13 +162,11 @@ impl Config {
181162
// Remove the entry if plants is empty.
182163
if plants.is_empty() {
183164
self.remove(PLANTS_KEY);
184-
self.persist();
185165
return;
186166
}
187167

188168
let text = ok!(ron::to_string(plants));
189169
self.set(PLANTS_KEY, text);
190-
self.persist();
191170
}
192171

193172
pub fn get_crop_descriptions(&self) -> Option<BTreeSet<String>> {
@@ -200,13 +179,11 @@ impl Config {
200179
// Remove the entry if the set is empty.
201180
if descriptions.is_empty() {
202181
self.remove(DESCRIPTIONS_KEY);
203-
self.persist();
204182
return;
205183
}
206184

207185
let text = ok!(ron::to_string(descriptions));
208186
self.set(DESCRIPTIONS_KEY, text);
209-
self.persist();
210187
}
211188

212189
pub fn get_avatar_skills(&self, avatar: &str) -> Option<HashMap<u32, (i32, i32)>> {
@@ -236,13 +213,11 @@ impl Config {
236213
let key = format!("{avatar} {AVATAR_SKILLS}");
237214
if skills.is_empty() {
238215
self.remove(&key);
239-
self.persist();
240216
return;
241217
}
242218

243219
let text = ok!(ron::to_string(&skills));
244220
self.set(&key, text);
245-
self.persist();
246221
}
247222

248223
fn path() -> Option<PathBuf> {
@@ -262,20 +237,16 @@ impl Config {
262237
fn set(&mut self, key: &str, item: String) {
263238
let mut lock = self.store.write().wrest();
264239
lock.items.insert(key.to_owned(), item);
265-
lock.modified = true;
240+
lock.persist();
266241
}
267242

268243
fn remove(&mut self, key: &str) {
269244
let mut lock = self.store.write().wrest();
270245
if lock.items.remove(key).is_some() {
271-
lock.modified = true;
246+
lock.persist();
272247
}
273248
}
274249

275-
fn persist(&mut self) {
276-
self.store.write().wrest().persist();
277-
}
278-
279250
fn get_sota_config_path() -> Option<PathBuf> {
280251
let path = dirs::config_dir()?;
281252
Some(path.join("Portalarium").join("Shroud of the Avatar"))

0 commit comments

Comments
 (0)