abserde 0.4.1

Simple platform-agnostic Rust crate for managing application settings/preferences.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
//! Simple platform-agnostic Rust crate for managing application settings/preferences.
//!
//! # Installation
//!
//! Install the crate as a dependency in your app's Cargo.toml file:
//!
//! ```toml
//! [dependencies]
//! abserde = "0.4.1"
//! ```
//!
//! # Usage
//! Import [Abserde], associated definitions, and [serde::Serialize], and [serde::Deserialize]:
//!
//! ```no_run
//! use abserde::*;
//! use serde::{Serialize, Deserialize};
//! ```
//!
//! Define a struct to store your app config.
//! You must derive your struct from [serde::Serialize] and [serde::Deserialize] traits.
//!
//! ```no_run
//! use std::collections::HashMap;
//!
//! # use serde::{Serialize, Deserialize};
//! #
//! #[derive(Serialize, Deserialize)]
//! struct MyConfig {
//! 	window_width: usize,
//! 	window_height: usize,
//! 	window_x: usize,
//! 	window_y: usize,
//! 	theme: String,
//! 	user_data: HashMap<String, String>,
//! }
//! ```
//!
//! Create an [Abserde] instance to manage how your configuration is stored on disk:
//!
//! ```no_run
//! # use serde::{Serialize, Deserialize};
//! #
//! # use abserde::*;
//! #
//! # #[derive(Serialize, Deserialize)]
//! # struct MyConfig;
//! #
//! let my_abserde = Abserde::default();
//! ```
//!
//! Using [Abserde] in this way will use your crate as the name for the app config directory.
//!
//! Alternatively, you can also pass options to [Abserde] to change the location or format of your config file:
//!
//! ```no_run
//! # use serde::{Serialize, Deserialize};
//! #
//! # use abserde::*;
//! #
//! # #[derive(Serialize, Deserialize)]
//! # struct MyConfig;
//! #
//! let my_abserde = Abserde {
//! 	app: "MyApp".to_string(),
//! 	location: Location::Auto,
//! 	format: Format::Json,
//! };
//! ```
//!
//! Load data into config from disk:
//!
//! ```no_run
//! # use serde::{Serialize, Deserialize};
//! #
//! # use abserde::*;
//! #
//! # #[derive(Serialize, Deserialize)]
//! # struct MyConfig;
//! #
//! # let my_abserde = Abserde {
//! # 	app: "MyApp".to_string(),
//! # 	location: Location::Auto,
//! # 	format: Format::Json,
//! # };
//! #
//! let my_config = MyConfig::load_config(&my_abserde)?;
//! #
//! # Ok::<(), Error>(())
//! ```
//!
//! Save config data to disk:
//!
//! ```no_run
//! # use serde::{Serialize, Deserialize};
//! #
//! # use abserde::*;
//! #
//! # #[derive(Serialize, Deserialize)]
//! # struct MyConfig;
//! #
//! # let my_abserde = Abserde {
//! # 	app: "MyApp".to_string(),
//! # 	location: Location::Auto,
//! # 	format: Format::Json,
//! # };
//! #
//! # let my_config = MyConfig::load_config(&my_abserde)?;
//! my_config.save_config(&my_abserde)?;
//! #
//! # Ok::<(), Error>(())
//! ```
//!
//! Delete config file from disk:
//!
//! ```no_run
//! # use serde::{Serialize, Deserialize};
//! #
//! # use abserde::*;
//! #
//! # #[derive(Serialize, Deserialize)]
//! # struct MyConfig;
//! #
//! # let my_abserde = Abserde {
//! # 	app: "MyApp".to_string(),
//! # 	location: Location::Auto,
//! # 	format: Format::Json,
//! # };
//! #
//! # let my_config = MyConfig::load_config(&my_abserde)?;
//! # my_config.save_config(&my_abserde)?;
//! #
//! my_abserde.delete()?;
//!
//! # Ok::<(), Error>(())
//! ```

#![deny(missing_docs)]
#![allow(clippy::tabs_in_doc_comments)]

use std::env::var;
use std::fs::{create_dir_all, remove_dir, remove_file, File};
use std::path::PathBuf;
use std::{error, io, result};

use serde::{de::DeserializeOwned, Serialize};

const MSG_NO_SYSTEM_CONFIG_DIR: &str = "no system config directory detected";

/// Alias for generic Error type.
pub type Error = Box<dyn error::Error>;

/// Alias for Result type wrapping generic Error type.
pub type Result<T> = result::Result<T, Error>;

/// Storage format for app config.
///
/// Each format is enabled as a feature. The json feature is included by default.
/// All other format features are disabled by default.
#[derive(Debug, PartialEq, Clone, Default)]
pub enum Format {
	// Default will become the first supported in order of preference.
	#[default]

	/// JSON format using the serde_json crate.
	#[cfg(feature = "json")]
	Json,

	/// YAML format using the serde_yaml crate.
	#[cfg(feature = "yaml")]
	Yaml,

	/// Pickle (Python) format using the serde-pickle crate.
	#[cfg(feature = "pickle")]
	Pickle,

	/// INI (Windows) format using the serde_ini crate.
	#[cfg(feature = "ini")]
	Ini,

	/// TOML format using the toml crate.
	#[cfg(feature = "toml")]
	Toml,
}

impl Format {
	/// Return default file name of config file for this format.
	pub fn default_name(&self) -> String {
		format!("config.{:?}", self).to_lowercase()
	}
}

/// Represents the location of a config file.
#[derive(Debug, PartialEq, Clone, Default)]
pub enum Location {
	/// Automatically determines location of config file based on platform/OS.
	#[default]
	Auto,

	/// Provides the full path to the config file.
	Path(PathBuf),

	/// Automatically determines config directory, with file name specified manually.
	File(PathBuf),

	/// Automatically determines config file name, with directory specified manually.
	Dir(PathBuf),
}

/// Represents an Abserde app, specifying how app settings are to be managed.
#[derive(Debug, PartialEq, Clone)]
pub struct Abserde {
	/// App name under which app settings are typically to be stored.
	pub app: String,

	/// Location specification for where app settings are physically kept.
	pub location: Location,

	/// Format for app setting storage and serialisation.
	pub format: Format,
}

impl Abserde {
	fn config_path(&self) -> Result<PathBuf> {
		let system_config_dir = dirs::config_dir()
			.ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, MSG_NO_SYSTEM_CONFIG_DIR))?;

		Ok(match &self.location {
			Location::Auto => system_config_dir
				.join(&self.app)
				.join(&self.format.default_name()),
			Location::Path(path) => path.clone(),
			Location::Dir(dir) => dir.join(&self.format.default_name()),
			Location::File(file) => system_config_dir.join(&self.app).join(file),
		})
	}

	/// Delete settings file related to this app.
	pub fn delete(&self) -> Result<()> {
		let config_path = self.config_path()?;

		remove_file(&config_path)?;

		match &self.location {
			// Don't attempt to delete folder if manually specifying folder.
			Location::Dir(_) => {}
			// Attempt to delete parent folder if it is empty.
			_ => {
				let config_dir = config_path.parent().ok_or_else(|| {
					io::Error::new(io::ErrorKind::NotFound, MSG_NO_SYSTEM_CONFIG_DIR)
				})?;

				// Ignore any errors here, as they are sometimes expected.
				_ = remove_dir(config_dir);
			}
		}

		Ok(())
	}
}

impl Default for Abserde {
	fn default() -> Self {
		Self {
			app: var("CARGO_PKG_NAME").unwrap_or_else(|_| env!("CARGO_PKG_NAME").to_string()),
			location: Default::default(),
			format: Default::default(),
		}
	}
}

/// Trait that apps can implement to store app settings.
///
/// Implementing types must also implement [serde::Serialize] and [serde::Deserialize] traits.
pub trait Config {
	/// Type of implementation.
	type T;

	/// Load a config from disk into the implementing type.
	fn load_config(abserde: &Abserde) -> Result<Self::T>;

	/// Save a config from the implementing type to disk.
	fn save_config(&self, abserde: &Abserde) -> Result<()>;
}

impl<T> Config for T
where
	T: Serialize,
	T: DeserializeOwned,
{
	type T = T;

	fn load_config(abserde: &Abserde) -> Result<Self::T> {
		let config_path = abserde.config_path()?;

		Ok(match abserde.format {
			#[cfg(feature = "json")]
			Format::Json => {
				let file = File::open(config_path)?;

				serde_json::from_reader(io::BufReader::new(file))?
			}
			#[cfg(feature = "yaml")]
			Format::Yaml => {
				let file = File::open(config_path)?;

				serde_yaml::from_reader(io::BufReader::new(file))?
			}
			#[cfg(feature = "pickle")]
			Format::Pickle => {
				let file = File::open(config_path)?;

				serde_pickle::from_reader(io::BufReader::new(file), serde_pickle::DeOptions::new())?
			}
			#[cfg(feature = "ini")]
			Format::Ini => {
				let file = File::open(config_path)?;

				serde_ini::from_read(io::BufReader::new(file))?
			}
			#[cfg(feature = "toml")]
			Format::Toml => {
				use io::Read;

				let mut file = File::open(config_path)?;
				let mut buf = String::new();

				file.read_to_string(&mut buf)?;

				toml::from_str(&buf)?
			}
		})
	}

	fn save_config(&self, abserde: &Abserde) -> Result<()> {
		let config_path = abserde.config_path()?;
		let config_dir = config_path
			.parent()
			.ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, MSG_NO_SYSTEM_CONFIG_DIR))?;

		create_dir_all(config_dir)?;

		match abserde.format {
			#[cfg(feature = "json")]
			Format::Json => {
				serde_json::to_writer(File::create(&config_path)?, self)?;
			}
			#[cfg(feature = "yaml")]
			Format::Yaml => {
				serde_yaml::to_writer(File::create(&config_path)?, self)?;
			}
			#[cfg(feature = "pickle")]
			Format::Pickle => {
				serde_pickle::to_writer(
					&mut File::create(&config_path)?,
					self,
					serde_pickle::SerOptions::new(),
				)?;
			}
			#[cfg(feature = "ini")]
			Format::Ini => {
				serde_ini::to_writer(File::create(&config_path)?, self)?;
			}
			#[cfg(feature = "toml")]
			Format::Toml => {
				use io::Write;

				write!(File::create(&config_path)?, "{}", toml::to_string(self)?)?;
			}
		}

		Ok(())
	}
}

#[cfg(test)]
mod tests {
	use std::collections::HashMap;
	use std::fmt::Debug;

	use fake::{Dummy, Fake, Faker};
	use serde::{de::DeserializeOwned, Deserialize, Serialize};
	use serial_test::serial;
	use tempfile::{NamedTempFile, TempDir};

	use crate::{Abserde, Config, Format, Location};

	const APP_NAME: &str = "rust_prefs_test";

	// Test config type for serialisation formats that only accept basic types.
	#[derive(Serialize, Deserialize, Debug, Default, Dummy, PartialEq)]
	struct TestConfigSimple {
		string_val: String,
		i8_val: i8,
		i16_val: i16,
		i32_val: i32,
		u8_val: u8,
		u16_val: u16,
		u32_val: u32,
		f32_val: f32,
	}

	// More complex config type for serialisation formats that support advanced types.
	#[derive(Serialize, Deserialize, Debug, Default, Dummy, PartialEq)]
	struct TestConfigComplex {
		string_val: String,
		i8_val: i8,
		i16_val: i16,
		i32_val: i32,
		i64_val: i64,
		i128_val: i128,
		u8_val: u8,
		u16_val: u16,
		u32_val: u32,
		u64_val: u64,
		u128_val: u128,
		f32_val: f32,
		f64_val: f64,
		vec_1_val: Vec<i64>,
		vec_2_val: Vec<(String, i8, i8, i32, i64, String, String, String)>,
		vec_3_val: Vec<(f32, f32, f32, f64, f64)>,
		hash_map_1_val: HashMap<String, String>,
		hash_map_2_val: HashMap<i16, i64>,
		hash_map_3_val: HashMap<i16, Vec<(String, i32, u8, HashMap<String, i32>)>>,
		hash_map_4_val: HashMap<String, (f64, f32, i8)>,
	}

	// Generic dispatch method.
	fn test_save_load_delete<T>(abserde: &Abserde)
	where
		T: Serialize,
		T: DeserializeOwned,
		T: Dummy<Faker>,
		T: PartialEq,
		T: Debug,
	{
		let test_config_saved: T = Faker.fake();

		test_config_saved.save_config(&abserde).unwrap();

		let test_config_loaded = T::load_config(&abserde).unwrap();

		assert_eq!(test_config_saved, test_config_loaded);

		abserde.delete().unwrap();
	}

	#[test]
	#[serial]
	fn test_auto() {
		test_save_load_delete::<TestConfigSimple>(&Abserde::default());
	}

	#[cfg(feature = "json")]
	#[test]
	#[serial]
	fn test_json_auto() {
		test_save_load_delete::<TestConfigComplex>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::Auto,
			format: Format::Json,
		});
	}

	#[cfg(feature = "json")]
	#[test]
	fn test_json_path() {
		let tmp_file = NamedTempFile::new().unwrap();

		test_save_load_delete::<TestConfigComplex>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::Path(tmp_file.path().into()),
			format: Format::Json,
		});
	}

	#[cfg(feature = "json")]
	#[test]
	#[serial]
	fn test_json_file() {
		test_save_load_delete::<TestConfigComplex>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::File("custom_file.json".into()),
			format: Format::Json,
		});
	}

	#[cfg(feature = "json")]
	#[test]
	fn test_json_dir() {
		let tmp_dir = TempDir::new().unwrap();

		test_save_load_delete::<TestConfigComplex>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::Dir(tmp_dir.path().into()),
			format: Format::Json,
		});
	}

	#[cfg(feature = "yaml")]
	#[test]
	#[serial]
	fn test_yaml_auto() {
		test_save_load_delete::<TestConfigComplex>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::Auto,
			format: Format::Yaml,
		});
	}

	#[cfg(feature = "yaml")]
	#[test]
	fn test_yaml_path() {
		let tmp_file = NamedTempFile::new().unwrap();

		test_save_load_delete::<TestConfigComplex>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::Path(tmp_file.path().into()),
			format: Format::Yaml,
		});
	}

	#[cfg(feature = "yaml")]
	#[test]
	#[serial]
	fn test_yaml_file() {
		test_save_load_delete::<TestConfigComplex>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::File("custom_file.yaml".into()),
			format: Format::Yaml,
		});
	}

	#[cfg(feature = "yaml")]
	#[test]
	fn test_yaml_dir() {
		let tmp_dir = TempDir::new().unwrap();

		test_save_load_delete::<TestConfigComplex>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::Dir(tmp_dir.path().into()),
			format: Format::Yaml,
		});
	}

	#[cfg(feature = "pickle")]
	#[test]
	#[serial]
	fn test_pickle_auto() {
		test_save_load_delete::<TestConfigSimple>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::Auto,
			format: Format::Pickle,
		});
	}

	#[cfg(feature = "pickle")]
	#[test]
	fn test_pickle_path() {
		let tmp_file = NamedTempFile::new().unwrap();

		test_save_load_delete::<TestConfigSimple>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::Path(tmp_file.path().into()),
			format: Format::Pickle,
		});
	}

	#[cfg(feature = "pickle")]
	#[test]
	#[serial]
	fn test_pickle_file() {
		test_save_load_delete::<TestConfigSimple>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::File("custom_file.pickle".into()),
			format: Format::Pickle,
		});
	}

	#[cfg(feature = "pickle")]
	#[test]
	fn test_pickle_dir() {
		let tmp_dir = TempDir::new().unwrap();

		test_save_load_delete::<TestConfigSimple>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::Dir(tmp_dir.path().into()),
			format: Format::Pickle,
		});
	}

	#[cfg(feature = "ini")]
	#[test]
	#[serial]
	fn test_ini_auto() {
		test_save_load_delete::<TestConfigSimple>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::Auto,
			format: Format::Ini,
		});
	}

	#[cfg(feature = "ini")]
	#[test]
	fn test_ini_path() {
		let tmp_file = NamedTempFile::new().unwrap();

		test_save_load_delete::<TestConfigSimple>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::Path(tmp_file.path().into()),
			format: Format::Ini,
		});
	}

	#[cfg(feature = "ini")]
	#[test]
	#[serial]
	fn test_ini_file() {
		test_save_load_delete::<TestConfigSimple>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::File("custom_file.ini".into()),
			format: Format::Ini,
		});
	}

	#[cfg(feature = "ini")]
	#[test]
	fn test_ini_dir() {
		let tmp_dir = TempDir::new().unwrap();

		test_save_load_delete::<TestConfigSimple>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::Dir(tmp_dir.path().into()),
			format: Format::Ini,
		});
	}

	#[cfg(feature = "toml")]
	#[test]
	#[serial]
	fn test_toml_auto() {
		test_save_load_delete::<TestConfigSimple>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::Auto,
			format: Format::Toml,
		});
	}

	#[cfg(feature = "toml")]
	#[test]
	fn test_toml_path() {
		let tmp_file = NamedTempFile::new().unwrap();

		test_save_load_delete::<TestConfigSimple>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::Path(tmp_file.path().into()),
			format: Format::Toml,
		});
	}

	#[cfg(feature = "toml")]
	#[test]
	#[serial]
	fn test_toml_file() {
		test_save_load_delete::<TestConfigSimple>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::File("custom_file.toml".into()),
			format: Format::Toml,
		});
	}

	#[cfg(feature = "toml")]
	#[test]
	fn test_toml_dir() {
		let tmp_dir = TempDir::new().unwrap();

		test_save_load_delete::<TestConfigSimple>(&Abserde {
			app: APP_NAME.to_string(),
			location: Location::Dir(tmp_dir.path().into()),
			format: Format::Toml,
		});
	}
}