diff --git a/Cargo.toml b/Cargo.toml index 0a7f21a..7f9bf73 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,6 @@ authors = ["ice_iix "] edition = "2018" [dependencies] +log = { version = "0.4.6", features = ["std"] } +stdweb = "0.4.17" +hex = "0.3.2" diff --git a/src/lib.rs b/src/lib.rs index 31e1bb2..a88f492 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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>(path: P) -> Result { + 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>(path: P) -> Result { + 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 { + 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 { + //Ok(0) + unimplemented!() + } +} + +impl Write for File { + fn write(&mut self, buf: &[u8]) -> Result { + 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(()) } }