Skip to content

Commit

Permalink
io-trait
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Oct 27, 2023
1 parent ec7c077 commit 9fbfc26
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 111 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 3 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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::{
Expand All @@ -161,7 +162,6 @@ mod test {
sha224::{compress, compress_one},
u256::{to_u224, U256},
virtual_io::VirtualIo,
Io,
};

#[wasm_bindgen_test]
Expand Down
3 changes: 2 additions & 1 deletion src/file_table.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
84 changes: 1 addition & 83 deletions src/io.rs
Original file line number Diff line number Diff line change
@@ -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<Self::Metadata>;
}

pub trait Io {
type Args: Iterator<Item = String>;
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<Self::Metadata>;
fn create_dir(&self, path: &str) -> io::Result<()>;
fn create(&self, path: &str) -> io::Result<Self::File>;
fn open(&self, path: &str) -> io::Result<Self::File>;
fn read(&self, path: &str) -> io::Result<Vec<u8>> {
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<Vec<Self::DirEntry>>;
fn read_to_string(&self, path: &str) -> io::Result<String> {
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<Vec<Self::DirEntry>> {
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() {
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ mod static_assert;
mod virtual_io;

pub use app::run;
pub use io::{Io, Metadata};
pub use real_io::RealIo;
21 changes: 1 addition & 20 deletions src/real_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
self.metadata()
}
}
use io_trait::Io;

#[derive(Default)]
pub struct RealIo();
Expand Down
6 changes: 3 additions & 3 deletions src/virtual_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use std::{
vec,
};

use crate::io::Io;
use io_trait::Io;

#[derive(Debug, Clone)]
pub struct Metadata {
len: u64,
is_dir: bool,
}

impl crate::io::Metadata for Metadata {
impl io_trait::Metadata for Metadata {
fn len(&self) -> u64 {
self.len
}
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 9fbfc26

Please sign in to comment.