Skip to content

Commit 1f05e94

Browse files
committed
Add initial localstorage fs implementation
Based on iceiix/stevenarella#166
1 parent 3a9699c commit 1f05e94

File tree

2 files changed

+89
-5
lines changed

2 files changed

+89
-5
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ authors = ["ice_iix <ice_ix@protonmail.ch>"]
55
edition = "2018"
66

77
[dependencies]
8+
log = { version = "0.4.6", features = ["std"] }
9+
stdweb = "0.4.17"
10+
hex = "0.3.2"

src/lib.rs

+86-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,88 @@
1-
#[cfg(test)]
2-
mod tests {
3-
#[test]
4-
fn it_works() {
5-
assert_eq!(2 + 2, 4);
1+
use log::info;
2+
use std::io::{Result, Read, Write};
3+
use std::path::Path;
4+
use std::convert::AsRef;
5+
use stdweb::web::window;
6+
use hex;
7+
8+
pub struct File {
9+
path: String,
10+
offset: usize,
11+
}
12+
13+
impl File {
14+
pub fn open<P: AsRef<Path>>(path: P) -> Result<File> {
15+
let path: &str = path.as_ref().to_str().unwrap();
16+
info!("fs open {:?}", path);
17+
18+
if !window().local_storage().contains_key(path) {
19+
Err(std::io::Error::from_raw_os_error(1))
20+
} else {
21+
Ok(File { path: path.to_string(), offset: 0 })
22+
}
23+
}
24+
pub fn create<P: AsRef<Path>>(path: P) -> Result<File> {
25+
let path: &str = path.as_ref().to_str().unwrap();
26+
info!("fs create {:?}", path);
27+
28+
match window().local_storage().insert(path, "") {
29+
Ok(_) => Ok(File { path: path.to_string(), offset: 0 }),
30+
Err(_) => Err(std::io::Error::from_raw_os_error(1)),
31+
}
32+
}
33+
}
34+
35+
impl Read for File {
36+
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
37+
if let Some(string) = window().local_storage().get(&self.path) {
38+
match hex::decode(&string) {
39+
Ok(data) => {
40+
info!("self.offset = {}", self.offset);
41+
info!("buf.len() = {}", buf.len());
42+
43+
let mut end = self.offset + buf.len();
44+
if end > data.len() {
45+
end = data.len();
46+
}
47+
info!("data.len() = {}", data.len());
48+
info!("end = {}", end);
49+
50+
info!("data = {:?}", data);
51+
52+
let bytes = &data[self.offset..end];
53+
54+
info!("bytes = {:?}", bytes);
55+
buf[..bytes.len()].copy_from_slice(&bytes);
56+
self.offset = end;
57+
Ok(bytes.len())
58+
},
59+
Err(_) => {
60+
Err(std::io::Error::from_raw_os_error(8))
61+
}
62+
}
63+
} else {
64+
Err(std::io::Error::from_raw_os_error(7))
65+
}
66+
}
67+
}
68+
impl Read for &File {
69+
fn read(&mut self, _buf: &mut [u8]) -> Result<usize> {
70+
//Ok(0)
71+
unimplemented!()
72+
}
73+
}
74+
75+
impl Write for File {
76+
fn write(&mut self, buf: &[u8]) -> Result<usize> {
77+
let string = window().local_storage().get(&self.path).unwrap();
78+
let new_string = string + &hex::encode(buf);
79+
self.offset += buf.len();
80+
match window().local_storage().insert(&self.path, &new_string) {
81+
Ok(_) => Ok(buf.len()),
82+
Err(_) => Err(std::io::Error::from_raw_os_error(1))
83+
}
84+
}
85+
fn flush(&mut self) -> Result<()> {
86+
Ok(())
687
}
788
}

0 commit comments

Comments
 (0)