-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial localstorage fs implementation
Based on iceiix/stevenarella#166
- Loading branch information
Showing
2 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -5,3 +5,6 @@ authors = ["ice_iix <[email protected]>"] | |
edition = "2018" | ||
|
||
[dependencies] | ||
log = { version = "0.4.6", features = ["std"] } | ||
stdweb = "0.4.17" | ||
hex = "0.3.2" |
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 |
---|---|---|
@@ -1,7 +1,88 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn it_works() { | ||
assert_eq!(2 + 2, 4); | ||
use log::info; | ||
use std::io::{Result, Read, Write}; | ||
use std::path::Path; | ||
use std::convert::AsRef; | ||
use stdweb::web::window; | ||
use hex; | ||
|
||
pub struct File { | ||
path: String, | ||
offset: usize, | ||
} | ||
|
||
impl File { | ||
pub fn open<P: AsRef<Path>>(path: P) -> Result<File> { | ||
let path: &str = path.as_ref().to_str().unwrap(); | ||
info!("fs open {:?}", path); | ||
|
||
if !window().local_storage().contains_key(path) { | ||
Err(std::io::Error::from_raw_os_error(1)) | ||
} else { | ||
Ok(File { path: path.to_string(), offset: 0 }) | ||
} | ||
} | ||
pub fn create<P: AsRef<Path>>(path: P) -> Result<File> { | ||
let path: &str = path.as_ref().to_str().unwrap(); | ||
info!("fs create {:?}", path); | ||
|
||
match window().local_storage().insert(path, "") { | ||
Ok(_) => Ok(File { path: path.to_string(), offset: 0 }), | ||
Err(_) => Err(std::io::Error::from_raw_os_error(1)), | ||
} | ||
} | ||
} | ||
|
||
impl Read for File { | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize> { | ||
if let Some(string) = window().local_storage().get(&self.path) { | ||
match hex::decode(&string) { | ||
Ok(data) => { | ||
info!("self.offset = {}", self.offset); | ||
info!("buf.len() = {}", buf.len()); | ||
|
||
let mut end = self.offset + buf.len(); | ||
if end > data.len() { | ||
end = data.len(); | ||
} | ||
info!("data.len() = {}", data.len()); | ||
info!("end = {}", end); | ||
|
||
info!("data = {:?}", data); | ||
|
||
let bytes = &data[self.offset..end]; | ||
|
||
info!("bytes = {:?}", bytes); | ||
buf[..bytes.len()].copy_from_slice(&bytes); | ||
self.offset = end; | ||
Ok(bytes.len()) | ||
}, | ||
Err(_) => { | ||
Err(std::io::Error::from_raw_os_error(8)) | ||
} | ||
} | ||
} else { | ||
Err(std::io::Error::from_raw_os_error(7)) | ||
} | ||
} | ||
} | ||
impl Read for &File { | ||
fn read(&mut self, _buf: &mut [u8]) -> Result<usize> { | ||
//Ok(0) | ||
unimplemented!() | ||
} | ||
} | ||
|
||
impl Write for File { | ||
fn write(&mut self, buf: &[u8]) -> Result<usize> { | ||
let string = window().local_storage().get(&self.path).unwrap(); | ||
let new_string = string + &hex::encode(buf); | ||
self.offset += buf.len(); | ||
match window().local_storage().insert(&self.path, &new_string) { | ||
Ok(_) => Ok(buf.len()), | ||
Err(_) => Err(std::io::Error::from_raw_os_error(1)) | ||
} | ||
} | ||
fn flush(&mut self) -> Result<()> { | ||
Ok(()) | ||
} | ||
} |