diff options
| author | troido <troido@protonmail.com> | 2020-02-18 01:11:49 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-18 01:11:49 +0100 |
| commit | c71ecb48fa4368035a852e2d06869a21382a6876 (patch) | |
| tree | 8d5a598fedf4e42bd6d8d1b0d476301dede2f20c /src/persistence.rs | |
| parent | c921686355c86afb5bf47e4b6c696057ede01b1f (diff) | |
Players are now saved/loaded too
Diffstat (limited to 'src/persistence.rs')
| -rw-r--r-- | src/persistence.rs | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/src/persistence.rs b/src/persistence.rs index 1808652..7c15ec0 100644 --- a/src/persistence.rs +++ b/src/persistence.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use std::fs; +use std::env; use serde_json; use serde_json::Value; use crate::{ @@ -29,9 +30,27 @@ pub struct FileStorage { } impl FileStorage { - pub fn new(path: &str) -> Self { + pub fn new(path: PathBuf) -> Self { Self { - directory: PathBuf::from(path) + directory: path + } + } + + pub fn savedir() -> Option<PathBuf> { + if let Some(pathname) = env::var_os("ASCIIFARM_SAVE_DIR") { + Some(PathBuf::from(pathname)) + } else if let Some(pathname) = env::var_os("XDG_DATA_HOME") { + let mut path = PathBuf::from(pathname); + path.push("asciifarm"); + path.push("saves"); + Some(path) + } else if let Some(pathname) = env::var_os("HOME") { + let mut path = PathBuf::from(pathname); + path.push(".asciifarm"); + path.push("saves"); + Some(path) + } else { + None } } } |
