diff --git a/Cargo.toml b/Cargo.toml index 51aef1b7..1ab47fe7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ authors = ["Sergey Shandar"] [dependencies] libc = "0.2.149" +io-trait = "0.1.1" [dev-dependencies] wasm-bindgen-test = "0.3.37" diff --git a/src/app.rs b/src/app.rs index 3a045928..f0a3b08b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,16 +1,16 @@ use std::io::{self, Read, Write}; +use io_trait::{DirEntry, Io, Metadata}; + use crate::{ base32::{StrEx, ToBase32}, file_table::{FileTable, CDT0, PARTS, ROOTS}, - io::{DirEntry, Io}, level_storage::LevelStorage, state::{mb, progress, State}, storage::{Null, Storage}, table::{Table, Type}, tree::Tree, u224::U224, - Metadata, }; trait ResultEx { @@ -153,6 +153,7 @@ pub fn run(io: &impl Io) -> Result<(), String> { #[cfg(test)] mod test { + use io_trait::Io; use wasm_bindgen_test::wasm_bindgen_test; use crate::{ @@ -161,7 +162,6 @@ mod test { sha224::{compress, compress_one}, u256::{to_u224, U256}, virtual_io::VirtualIo, - Io, }; #[wasm_bindgen_test] diff --git a/src/file_table.rs b/src/file_table.rs index 242ebf5b..0421e7b4 100644 --- a/src/file_table.rs +++ b/src/file_table.rs @@ -1,10 +1,11 @@ use std::io; +use io_trait::Io; + use crate::{ base32::ToBase32, table::{Table, Type}, u224::U224, - Io, }; pub struct FileTable<'a, T: Io>(pub &'a T); diff --git a/src/io.rs b/src/io.rs index 5fdcb3b8..59cd5cb8 100644 --- a/src/io.rs +++ b/src/io.rs @@ -1,92 +1,10 @@ -use std::{ - ffi::CStr, - fmt, - io::{self, Read, Write}, -}; - -#[allow(clippy::len_without_is_empty)] -pub trait Metadata { - fn len(&self) -> u64; - fn is_dir(&self) -> bool; -} - -pub trait DirEntry { - type Metadata: Metadata; - fn path(&self) -> String; - fn metadata(&self) -> io::Result; -} - -pub trait Io { - type Args: Iterator; - type File: Read + Write + fmt::Debug; - type Stdout: Write; - type Metadata: Metadata; - type DirEntry: DirEntry; - fn args(&self) -> Self::Args; - fn stdout(&self) -> Self::Stdout; - fn metadata(&self, path: &str) -> io::Result; - fn create_dir(&self, path: &str) -> io::Result<()>; - fn create(&self, path: &str) -> io::Result; - fn open(&self, path: &str) -> io::Result; - fn read(&self, path: &str) -> io::Result> { - let mut file = self.open(path)?; - let mut result = Vec::default(); - file.read_to_end(&mut result)?; - Ok(result) - } - fn read_dir(&self, path: &str) -> io::Result>; - fn read_to_string(&self, path: &str) -> io::Result { - let mut file = self.open(path)?; - let mut result = String::default(); - file.read_to_string(&mut result)?; - Ok(result) - } - fn write(&self, path: &str, data: &[u8]) -> io::Result<()> { - let mut file = self.create(path)?; - file.write_all(data)?; - Ok(()) - } - fn create_dir_recursively(&self, path: &str) -> io::Result<()> { - let mut x = String::default(); - let mut e = Ok(()); - for i in path.split('/') { - x += i; - e = self.create_dir(&x); - x += "/"; - } - e - } - fn write_recursively(&self, path: &str, data: &[u8]) -> io::Result<()> { - let e = self.write(path, data); - if let Err(er) = e { - return if let Some((p, _)) = path.rsplit_once('/') { - self.create_dir_recursively(p)?; - self.write(path, data) - } else { - Err(er) - }; - } - Ok(()) - } - fn read_dir_type(&self, path: &str, is_dir: bool) -> io::Result> { - let mut result = Vec::default(); - for i in self.read_dir(path)? { - if i.metadata()?.is_dir() == is_dir { - result.push(i); - } - } - Ok(result) - } -} - #[cfg(test)] mod test { + use io_trait::Io; use wasm_bindgen_test::wasm_bindgen_test; use crate::virtual_io::VirtualIo; - use super::Io; - #[wasm_bindgen_test] #[test] fn test() { diff --git a/src/lib.rs b/src/lib.rs index 06e19017..225dbecc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,5 +34,4 @@ mod static_assert; mod virtual_io; pub use app::run; -pub use io::{Io, Metadata}; pub use real_io::RealIo; diff --git a/src/real_io.rs b/src/real_io.rs index 5313efdc..45af1068 100644 --- a/src/real_io.rs +++ b/src/real_io.rs @@ -6,26 +6,7 @@ use std::{ io::{self, Stdout}, }; -use crate::{io::DirEntry, Io, Metadata}; - -impl Metadata for fs::Metadata { - fn len(&self) -> u64 { - self.len() - } - fn is_dir(&self) -> bool { - self.is_dir() - } -} - -impl DirEntry for fs::DirEntry { - type Metadata = fs::Metadata; - fn path(&self) -> String { - self.path().to_str().unwrap().to_string() - } - fn metadata(&self) -> io::Result { - self.metadata() - } -} +use io_trait::Io; #[derive(Default)] pub struct RealIo(); diff --git a/src/virtual_io.rs b/src/virtual_io.rs index 18a9744f..c21bca43 100644 --- a/src/virtual_io.rs +++ b/src/virtual_io.rs @@ -7,7 +7,7 @@ use std::{ vec, }; -use crate::io::Io; +use io_trait::Io; #[derive(Debug, Clone)] pub struct Metadata { @@ -15,7 +15,7 @@ pub struct Metadata { is_dir: bool, } -impl crate::io::Metadata for Metadata { +impl io_trait::Metadata for Metadata { fn len(&self) -> u64 { self.len } @@ -100,7 +100,7 @@ pub struct DirEntry { metadata: Metadata, } -impl crate::io::DirEntry for DirEntry { +impl io_trait::DirEntry for DirEntry { type Metadata = Metadata; fn path(&self) -> String { self.path.clone()