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
pub mod flate;
#[cfg(not(target_arch = "wasm32"))]
use self::flate::{unzip, zip};
#[cfg(not(target_arch = "wasm32"))]
use anyhow::{anyhow, Context};
#[derive(Debug, Clone)]
pub struct Location {
pub bin_name: String,
pub version: String,
pub profile: String,
}
impl Location {
#[cfg(not(target_arch = "wasm32"))]
pub fn path(&self) -> anyhow::Result<std::path::PathBuf> {
let root = dirs::data_dir().ok_or_else(|| anyhow!("Couldn't find data dir!"))?;
Ok(root
.join(&self.bin_name)
.join(format!("v{}", &self.version))
.join(&self.profile)
.with_extension(".dat"))
}
#[cfg(target_arch = "wasm32")]
pub fn key(&self) -> String {
format!("{}/v{}/{}", &self.bin_name, &self.version, &self.profile)
}
}
impl Default for Location {
fn default() -> Self {
Self {
bin_name: env!("CARGO_PKG_NAME").to_string(),
version: env!("CARGO_PKG_VERSION").to_string(),
profile: String::from("default"),
}
}
}
pub fn save<T: AsRef<[u8]>>(data: T) -> anyhow::Result<()> {
#[cfg(target_arch = "wasm32")]
{
wasm::save(&Location::default().key(), data.as_ref())
}
#[cfg(not(target_arch = "wasm32"))]
{
save_to(data, &Location::default())
}
}
pub fn save_to<T: AsRef<[u8]>>(data: T, location: &Location) -> anyhow::Result<()> {
#[cfg(target_arch = "wasm32")]
{
wasm::save(&location.key(), data.as_ref())
}
#[cfg(not(target_arch = "wasm32"))]
{
let data = zip(data)?;
std::fs::write(location.path()?, &data).context("When writing to the file")?;
Ok(())
}
}
pub fn load() -> anyhow::Result<Vec<u8>> {
#[cfg(target_arch = "wasm32")]
{
wasm::load(&Location::default().key())
}
#[cfg(not(target_arch = "wasm32"))]
{
load_from(&Location::default())
}
}
pub fn load_from(location: &Location) -> anyhow::Result<Vec<u8>> {
#[cfg(target_arch = "wasm32")]
{
wasm::load(&location.key())
}
#[cfg(not(target_arch = "wasm32"))]
{
let zipped = std::fs::read(location.path()?).context("When reading the file")?;
unzip(&zipped)
}
}
#[cfg(target_arch = "wasm32")]
mod wasm {
use super::flate::zip64;
use crate::{js_convert::FromJsObject, storage::flate::unzip64};
use anyhow::{anyhow, bail, Context};
use sapp_jsutils::{JsObject, JsObjectWeak};
extern "C" {
fn storage_save(key: JsObjectWeak, val: JsObjectWeak) -> JsObject;
fn storage_load(key: JsObjectWeak) -> JsObject;
}
pub fn save(key: &str, val: &[u8]) -> anyhow::Result<()> {
let key = JsObject::string(&key);
let val = JsObject::string(&zip64(val)?);
let result = unsafe { storage_save(key.weak(), val.weak()) };
let result = Result::<(), String>::from_js(result)
.context("When trying to turn the returned value into a Result")?;
match result {
Ok(()) => Ok(()),
Err(oh_no) => bail!(anyhow!(oh_no).context("When trying to save to localstorage")),
}
}
pub fn load(key: &str) -> anyhow::Result<Vec<u8>> {
let key = JsObject::string(&key);
let result = unsafe { storage_load(key.weak()) };
let result = Result::<String, String>::from_js(result)
.context("When trying to turn the returned value into a Result")?;
let data =
result.map_err(|e| anyhow!(e).context("When trying to load from localstorage"))?;
unzip64(&data)
}
}