apputils/
config.rs

1use crate::dirs::{config_home, data_home};
2use std::path::{Path, PathBuf};
3use std::fs;
4use std::io;
5
6/// Config file helper
7/// 
8/// A simple helper for managing config files.
9pub struct Cfg;
10impl Cfg {
11	/// Config file path
12	/// 
13	/// Returns the formatted path to a config file of an app.
14	pub fn path(appname: &str, file: &str) -> PathBuf {
15		local_cfg_dir(appname).join(file)
16	}
17
18	/// Read config file
19	/// 
20	/// Reads an app's config file into a [String].
21	/// This can then be used to parse it via Serde.
22	pub fn read(appname: &str, file: &str) -> io::Result<String> {
23		fs::read_to_string(Self::path(appname, file))
24	}
25
26	/// Save config file
27	/// 
28	/// Saves to an app's config file. Note that this will override the file!
29	pub fn save(appname: &str, file: &str, data: impl AsRef<[u8]>) -> io::Result<()> {
30		fs::create_dir_all(local_cfg_dir(appname))?;
31		fs::write(Self::path(appname, file), data)
32	}
33
34	/// Global config file path
35	/// 
36	/// Returns the formatted path to an app's global config file.
37	pub fn global_path(appname: &str, file: &str) -> PathBuf {
38		global_cfg_dir(appname).join(file)
39	}
40
41	/// Read global config
42	/// 
43	/// Reads the global config file of an app into a [String].
44	/// Will most likely be parsed, e.g. via Serde.
45	pub fn global_read(appname: &str, file: &str) -> io::Result<String> {
46		fs::read_to_string(Self::global_path(appname, file))
47	}
48
49	/// Save global
50	/// 
51	/// Saves to an app's global config file. Note that this will override the file!
52	pub fn global_save(appname: &str, file: &str, data: impl AsRef<[u8]>) -> io::Result<()> {
53		fs::create_dir_all(global_cfg_dir(appname))?;
54		fs::write(Self::global_path(appname, file), data)
55	}
56}
57
58/// Local config folder
59/// 
60/// Returns the path to your app's local config folder.  
61/// `appname`: The short name of your app in lower case.
62pub fn local_cfg_dir(appname: &str) -> PathBuf {
63	config_home().join(appname)
64}
65
66/// Global config folder
67/// 
68/// Returns the path to your app's global config folder.  
69/// `appname`: The short name of your app in lower case.
70/// 
71/// Due to there not being a proper `/etc` equivalent on Windows,
72/// this defaults to the [local](local_cfg_dir) config folder on Windows.
73pub fn global_cfg_dir(appname: &str) -> PathBuf {
74	if cfg!(target_family = "unix") {
75		Path::new("/etc").join(appname)
76	} else {
77		local_cfg_dir(appname)
78	}
79}
80
81/// Appdata helper
82/// 
83/// A simple 
84pub struct Appdata;
85impl Appdata {
86	/// Appdata file path
87	/// 
88	/// Returns the formatted path to a data file of an app.
89	pub fn path(appname: &str, file: &str) -> PathBuf {
90		local_data_dir(appname).join(file)
91	}
92
93	/// Unicode Read
94	/// 
95	/// Reads a text file from the app's data folder.
96	pub fn read_str(appname: &str, file: &str) -> io::Result<String> {
97		fs::read_to_string(Self::path(appname, file))
98	}
99	
100	/// Binary Read
101	/// 
102	/// Reads a binary file from the app's data folder.
103	pub fn read(appname: &str, file: &str) -> io::Result<Vec<u8>> {
104		fs::read(Self::path(appname, file))
105	}
106
107	/// Save data file
108	/// 
109	/// Saves the data file of an app. Note that this overrides the target file!
110	pub fn save(appname: &str, file: &str, data: impl AsRef<[u8]>) -> io::Result<()> {
111		fs::create_dir_all(local_data_dir(appname))?;
112		fs::write(Self::path(appname, file), data)
113	}
114}
115
116/// Application data folder
117/// 
118/// Returns the path to your app's data folder.  
119/// `appname`: The short name of your app in lower case.
120pub fn local_data_dir(appname: &str) -> PathBuf {
121	data_home().join(appname)
122}