Skip to content

Commit

Permalink
add_entry
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Mar 11, 2024
1 parent 52286ef commit f0c9891
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
20 changes: 11 additions & 9 deletions blockset-lib/src/app/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use nanvm_lib::{
serializer::to_json,
};

use crate::{cdt::tree_add::TreeAdd, common::print::Print};
use crate::{cdt::tree_add::TreeAdd, common::{print::Print, status_line::StatusLine}};

use super::{invalid_input, read_to_tree, read_to_tree_file};

Expand Down Expand Up @@ -64,33 +64,35 @@ fn dir_to_json<M: Manager>(
}

impl<'a, T: Io, S: 'a + TreeAdd, F: Fn(&'a T) -> S> Add<'a, T, S, F> {
pub fn add_file(&self, path: &str) -> io::Result<String> {
pub fn add_file(&self, state: &mut StatusLine<'a, T>, path: &str) -> io::Result<String> {
read_to_tree_file(
self.to_posix_eol,
(self.storage)(self.io),
self.io.open(path)?,
self.io,
state,
self.display_new,
)
}
fn path_to_json(&self, path: &str) -> io::Result<String> {
fn path_to_json(&self, state: &mut StatusLine<'a, T>, path: &str) -> io::Result<String> {
let files = read_dir_recursive(self.io, path)?;
let mut list = Vec::default();
for e in files {
let f = e.path();
let hash = self.add_file(f.as_str())?;
let hash = self.add_file(state, f.as_str())?;
list.push(property(GLOBAL, path.len(), f, hash));
}
dir_to_json(GLOBAL, list.into_iter())
}
pub fn add_dir(&self, path: &str) -> io::Result<()> {
let json = self.path_to_json(path)?;
let hash = read_to_tree(
pub fn add_dir(&self, state: &mut StatusLine<'a, T>, path: &str) -> io::Result<String> {
let json = self.path_to_json(state, path)?;
let mut state = StatusLine::new(self.io);
read_to_tree(
(self.storage)(self.io),
Cursor::new(&json),
self.io,
&mut state,
self.display_new,
)?;
self.io.stdout().println([hash.as_str()])
)
}
}
17 changes: 10 additions & 7 deletions blockset-lib/src/app/add_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io;

use io_trait::{Io, Metadata};

use crate::{cdt::tree_add::TreeAdd, common::print::Print};
use crate::{cdt::tree_add::TreeAdd, common::{print::Print, status_line::StatusLine}};

use super::{add::Add, invalid_input, is_to_posix_eol};

Expand All @@ -20,10 +20,13 @@ pub fn add_entry<'a, T: Io, S: 'a + TreeAdd>(
to_posix_eol,
display_new,
};
if io.metadata(&path)?.is_dir() {
add.add_dir(&path)
} else {
let k = add.add_file(&path)?;
io.stdout().println([k.as_str()])
}
let k = {
let mut state = StatusLine::new(io);
if io.metadata(&path)?.is_dir() {
add.add_dir(&mut state, &path)?
} else {
add.add_file(&mut state, &path)?
}
};
io.stdout().println([k.as_str()])
}
10 changes: 6 additions & 4 deletions blockset-lib/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ fn read_to_tree<T: TreeAdd>(
s: T,
mut file: impl Read + Progress,
io: &impl Io,
state: &mut StatusLine<'_, impl Io>,
display_new: bool,
) -> io::Result<String> {
let mut tree = MainTreeAdd::new(s);
let mut state = StatusLine::new(io);
// let mut state = StatusLine::new(io);
let mut new = 0;
loop {
let pr = file.progress();
set_progress(&mut state, display_new, new, pr?)?;
set_progress(state, display_new, new, pr?)?;
if file_read(&mut file, &mut tree, &mut new)? {
return Ok(tree.end()?.0.to_base32());
}
Expand All @@ -93,12 +94,13 @@ fn read_to_tree_file(
s: impl TreeAdd,
f: impl Read + Progress,
io: &impl Io,
state: &mut StatusLine<'_, impl Io>,
display_new: bool,
) -> io::Result<String> {
if to_posix_eol {
read_to_tree(s, ToPosixEol::new(f), io, display_new)
read_to_tree(s, ToPosixEol::new(f), io, state, display_new)
} else {
read_to_tree(s, f, io, display_new)
read_to_tree(s, f, io, state, display_new)
}
}

Expand Down

0 comments on commit f0c9891

Please sign in to comment.