1use crate::dirs::{config_home, data_home};
2use std::path::{Path, PathBuf};
3use std::fs;
4use std::io;
5
6pub struct Cfg;
10impl Cfg {
11 pub fn path(appname: &str, file: &str) -> PathBuf {
15 local_cfg_dir(appname).join(file)
16 }
17
18 pub fn read(appname: &str, file: &str) -> io::Result<String> {
23 fs::read_to_string(Self::path(appname, file))
24 }
25
26 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 pub fn global_path(appname: &str, file: &str) -> PathBuf {
38 global_cfg_dir(appname).join(file)
39 }
40
41 pub fn global_read(appname: &str, file: &str) -> io::Result<String> {
46 fs::read_to_string(Self::global_path(appname, file))
47 }
48
49 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
58pub fn local_cfg_dir(appname: &str) -> PathBuf {
63 config_home().join(appname)
64}
65
66pub 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
81pub struct Appdata;
85impl Appdata {
86 pub fn path(appname: &str, file: &str) -> PathBuf {
90 local_data_dir(appname).join(file)
91 }
92
93 pub fn read_str(appname: &str, file: &str) -> io::Result<String> {
97 fs::read_to_string(Self::path(appname, file))
98 }
99
100 pub fn read(appname: &str, file: &str) -> io::Result<Vec<u8>> {
104 fs::read(Self::path(appname, file))
105 }
106
107 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
116pub fn local_data_dir(appname: &str) -> PathBuf {
121 data_home().join(appname)
122}