-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
respect config and add persistence to it
- Loading branch information
Showing
9 changed files
with
282 additions
and
95 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, Default)] | ||
pub struct Config { | ||
pub api_key: String, | ||
pub api_secret_key: String, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum LoadError { | ||
File, | ||
Format, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum SaveError { | ||
File, | ||
Write, | ||
Format, | ||
} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
impl Config { | ||
fn path() -> std::path::PathBuf { | ||
let mut path = if let Some(project_dirs) = | ||
directories_next::ProjectDirs::from("rs", "x86y", "Dynasty") | ||
{ | ||
project_dirs.data_dir().into() | ||
} else { | ||
std::env::current_dir().unwrap_or_default() | ||
}; | ||
path.push("config.json"); | ||
path | ||
} | ||
|
||
pub async fn load() -> Result<Config, LoadError> { | ||
use tokio::fs::File; | ||
use tokio::io::AsyncReadExt; | ||
|
||
let mut contents = String::new(); | ||
let mut file = File::open(Self::path()) | ||
.await | ||
.map_err(|_| LoadError::File)?; | ||
file.read_to_string(&mut contents) | ||
.await | ||
.map_err(|_| LoadError::File)?; | ||
serde_json::from_str(&contents).map_err(|_| LoadError::Format) | ||
} | ||
|
||
pub async fn save(self) -> Result<(), SaveError> { | ||
use tokio::fs::File; | ||
use tokio::io::AsyncWriteExt; | ||
|
||
let json = serde_json::to_string_pretty(&self).map_err(|_| SaveError::Format)?; | ||
let path = Self::path(); | ||
if let Some(dir) = path.parent() { | ||
tokio::fs::create_dir_all(dir) | ||
.await | ||
.map_err(|_| SaveError::File)?; | ||
} | ||
{ | ||
let mut file = File::create(path).await.map_err(|_| SaveError::File)?; | ||
file.write_all(json.as_bytes()) | ||
.await | ||
.map_err(|_| SaveError::Write)?; | ||
} | ||
tokio::time::sleep(std::time::Duration::from_secs(2)).await; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
impl Config { | ||
fn storage() -> Option<web_sys::Storage> { | ||
let window = web_sys::window()?; | ||
|
||
window.local_storage().ok()? | ||
} | ||
|
||
async fn load() -> Result<Config, LoadError> { | ||
let storage = Self::storage().ok_or(LoadError::File)?; | ||
|
||
let contents = storage | ||
.get_item("state") | ||
.map_err(|_| LoadError::File)? | ||
.ok_or(LoadError::File)?; | ||
|
||
serde_json::from_str(&contents).map_err(|_| LoadError::Format) | ||
} | ||
|
||
async fn save(self) -> Result<(), SaveError> { | ||
let storage = Self::storage().ok_or(SaveError::File)?; | ||
|
||
let json = serde_json::to_string_pretty(&self).map_err(|_| SaveError::Format)?; | ||
|
||
storage | ||
.set_item("state", &json) | ||
.map_err(|_| SaveError::Write)?; | ||
|
||
let _ = wasm_timer::Delay::new(std::time::Duration::from_secs(2)).await; | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.