Skip to content

Commit

Permalink
Dir (#163)
Browse files Browse the repository at this point in the history
* app

* trait Print

* fmt

* print

* error state

* intersection

* fmt

* eq

* ok

* app

* app
  • Loading branch information
sergey-shandar authored Mar 4, 2024
1 parent 1e85693 commit 464b99b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 32 deletions.
37 changes: 9 additions & 28 deletions blockset-lib/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
common::{
base32::{StrEx, ToBase32},
eol::ToPosixEol,
print::Print,
progress::{self, Progress},
status_line::{mb, StatusLine},
},
Expand Down Expand Up @@ -43,13 +44,10 @@ fn file_read(
) -> io::Result<bool> {
let mut buf = [0; 1024];
let size = file.read(buf.as_mut())?;
if size == 0 {
return Ok(true);
}
for c in buf[0..size].iter() {
for c in buf[..size].iter() {
*new += tree.push(*c)?;
}
Ok(false)
Ok(size == 0)
}

fn read_to_tree<T: TreeAdd>(
Expand All @@ -65,23 +63,13 @@ fn read_to_tree<T: TreeAdd>(
let pr = file.progress();
set_progress(&mut state, display_new, new, pr?)?;
if file_read(&mut file, &mut tree, &mut new)? {
break;
return Ok(tree.end()?.0.to_base32());
}
}
Ok(tree.end()?.0.to_base32())
}

fn print(w: &mut impl Write, s: &str) -> io::Result<()> {
w.write_all(s.as_bytes())
}

fn println(w: &mut impl Write, s: &str) -> io::Result<()> {
print(w, s)?;
print(w, "\n")
}

fn invalid_input(s: &str) -> io::Error {
io::Error::new(ErrorKind::InvalidInput, s)
fn invalid_input(error: &str) -> io::Error {
io::Error::new(ErrorKind::InvalidInput, error)
}

fn is_to_posix_eol(a: &mut impl Iterator<Item = String>) -> io::Result<bool> {
Expand All @@ -103,9 +91,6 @@ fn read_to_tree_file(
display_new: bool,
) -> io::Result<String> {
if to_posix_eol {
// this may lead to incorrect progress bar because, a size of a file with replaced CRLF
// is smaller than `len`. Proposed solution:
// a Read implementation which can also report a progress.
read_to_tree(s, ToPosixEol::new(f), io, display_new)
} else {
read_to_tree(s, f, io, display_new)
Expand All @@ -121,10 +106,9 @@ fn add<'a, T: Io, S: 'a + TreeAdd>(
let stdout = &mut io.stdout();
let path = a.next().ok_or(invalid_input("missing file name"))?;
let to_posix_eol = is_to_posix_eol(a)?;
// let len = io.metadata(&path)?.len();
let f = io.open(&path)?;
let k = read_to_tree_file(to_posix_eol, storage(io), f, io, display_new)?;
println(stdout, &k)
stdout.println([k.as_str()])
}

fn get_hash(a: &mut impl Iterator<Item = String>) -> io::Result<U224> {
Expand All @@ -135,7 +119,7 @@ fn get_hash(a: &mut impl Iterator<Item = String>) -> io::Result<U224> {

fn validate(a: &mut impl Iterator<Item = String>, stdout: &mut impl Write) -> io::Result<()> {
let d = get_hash(a)?.to_base32();
println(stdout, &("valid: ".to_owned() + &d))
stdout.println(["valid: ", d.as_str()])
}

pub fn run(io: &impl Io) -> io::Result<()> {
Expand All @@ -153,10 +137,7 @@ pub fn run(io: &impl Io) -> io::Result<()> {
let w = &mut io.create(&path)?;
FileForest(io).restore(&ForestNodeId::new(NodeType::Root, &d), w, io)
}
"info" => println(
stdout,
&("size: ".to_owned() + &calculate_total(io)?.to_string() + " B."),
),
"info" => stdout.println(["size: ", calculate_total(io)?.to_string().as_str(), " B."]),
_ => Err(invalid_input("unknown command")),
}
}
Expand Down
1 change: 1 addition & 0 deletions blockset-lib/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ pub mod ascii;
pub mod base32;
pub mod bit_vec;
pub mod eol;
pub mod print;
pub mod progress;
pub mod status_line;
52 changes: 52 additions & 0 deletions blockset-lib/src/common/print.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use core::iter::once;
use std::io::{self, Write};

pub trait Print: Write {
fn print<'a>(&mut self, s: impl IntoIterator<Item = &'a str>) -> io::Result<()> {
for i in s {
self.write_all(i.as_bytes())?;
}
Ok(())
}
fn println<'a>(&mut self, s: impl IntoIterator<Item = &'a str>) -> io::Result<()> {
self.print(s.into_iter().chain(once("\n")))
}
}

impl<T: Write> Print for T {}

#[cfg(test)]
mod test {
use std::io::{self, Cursor, Write};

use wasm_bindgen_test::wasm_bindgen_test;

use super::Print;

#[wasm_bindgen_test]
#[test]
fn test() {
let mut w = Cursor::new(Vec::new());
w.print(["a", "b"]).unwrap();
w.println(["c", "d"]).unwrap();
assert_eq!(w.into_inner(), b"abcd\n");
}

struct X();

impl Write for X {
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
Err(io::Error::new(io::ErrorKind::Other, "x"))
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}

#[wasm_bindgen_test]
#[test]
fn test_error() {
let mut w = X();
w.print(["a", "b"]).unwrap_err();
}
}
27 changes: 23 additions & 4 deletions blockset-lib/src/info/node_type_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,39 @@ mod test {
use crate::cdt::node_type::NodeType;

#[inline(never)]
fn check(x: NodeType, y: NodeType, union: fn(NodeTypeSet, NodeTypeSet) -> NodeTypeSet) {
fn check(
x: NodeType,
y: NodeType,
union: fn(NodeTypeSet, NodeTypeSet) -> NodeTypeSet,
intersection: fn(NodeTypeSet, NodeTypeSet) -> NodeTypeSet,
eq: fn(NodeTypeSet, NodeTypeSet) -> bool,
) {
let xi = 1 << x as u8;
let yi = 1 << y as u8;
let xs = NodeTypeSet::new(x);
let ys = NodeTypeSet::new(y);
assert!(eq(xs, xs));
assert_eq!(union(xs, ys).0, xi | yi);
assert_eq!(xs.intersection(ys).0, xi & yi);
assert_eq!(intersection(xs, ys).0, xi & yi);
}

#[test]
#[wasm_bindgen_test]
fn test() {
check(NodeType::Child, NodeType::Child, NodeTypeSet::union);
check(NodeType::Child, NodeType::Root, NodeTypeSet::union);
check(
NodeType::Child,
NodeType::Child,
NodeTypeSet::union,
NodeTypeSet::intersection,
NodeTypeSet::eq,
);
check(
NodeType::Child,
NodeType::Root,
NodeTypeSet::union,
NodeTypeSet::intersection,
NodeTypeSet::eq,
);
let x = NodeTypeSet::new(NodeType::Root);
assert_eq!(x.0, 1);
let y = NodeTypeSet::new(NodeType::Child);
Expand Down

0 comments on commit 464b99b

Please sign in to comment.