Skip to content

Commit

Permalink
recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Oct 17, 2023
1 parent 2d7bc1c commit a9a6089
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
12 changes: 2 additions & 10 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io::{Read, Write};

use crate::{
base32::{StrEx, ToBase32},
file_table::{FileTable, DIR},
file_table::FileTable,
io::Io,
level_storage::LevelStorage,
state::{mb, progress, State},
Expand Down Expand Up @@ -96,15 +96,7 @@ pub fn run(io: &impl Io) -> Result<(), String> {
Ok(())
}
"address" => add(io, &mut a, |_| Null(), false),
"add" => add(
io,
&mut a,
|io| {
let _ = io.create_dir(DIR);
LevelStorage::new(FileTable(io))
},
true,
),
"add" => add(io, &mut a, |io| LevelStorage::new(FileTable(io)), true),
"get" => {
let b32 = a.next().ok_or("missing address")?;
let d = b32.from_base32::<U224>().ok_or("invalid address")?;
Expand Down
2 changes: 1 addition & 1 deletion src/file_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'a, T: Io> Table for FileTable<'a, T> {
) -> io::Result<()> {
let x = value.collect::<Vec<_>>();
let p = path(t, key);
self.0.write(&p, &x)
self.0.write_recursively(&p, &x)
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ pub trait Io {
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<()> {
if let Err(er) = self.write(&path, data) {
return if let Some((p, _)) = path.split_once('/') {
self.create_dir_recursively(p)?;
self.write(&path, data)
} else {
Err(er)
}
}
Ok(())
}
}

#[cfg(test)]
Expand Down

0 comments on commit a9a6089

Please sign in to comment.