-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get
can create directories recursevly. (#168)
* state.total * fmt * coverage * c.json * clippy * parse JSON * map_err * coverage * to recursive dir * cov * 0.0.4
- Loading branch information
1 parent
4b2069e
commit 7ce783a
Showing
5 changed files
with
137 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::io::{self, Write}; | ||
|
||
use io_trait::Io; | ||
use nanvm_lib::{ | ||
js::any::Any, | ||
mem::manager::Manager, | ||
parser::{parse_with_tokens, Context, ParseError, ParseResult}, | ||
tokenizer::tokenize, | ||
}; | ||
|
||
use crate::{ | ||
cdt::node_type::NodeType, | ||
forest::{file::FileForest, node_id::ForestNodeId, Forest}, | ||
uint::u224::U224, | ||
}; | ||
|
||
use super::invalid_input; | ||
|
||
pub fn restore(io: &impl Io, hash: &U224, w: &mut impl Write) -> io::Result<()> { | ||
FileForest(io).restore(&ForestNodeId::new(NodeType::Root, hash), w, io) | ||
} | ||
|
||
fn tokenize_and_parse<M: Manager>( | ||
io: &impl Io, | ||
manager: M, | ||
s: String, | ||
) -> Result<ParseResult<M>, ParseError> { | ||
parse_with_tokens( | ||
&Context::new(manager, io, String::default()), | ||
tokenize(s).into_iter(), | ||
) | ||
} | ||
|
||
pub fn parse_json<M: Manager>(io: &impl Io, manager: M, v: Vec<u8>) -> io::Result<Any<M::Dealloc>> { | ||
let s = String::from_utf8(v).map_err(|_| invalid_input("Invalid UTF-8"))?; | ||
let result = tokenize_and_parse(io, manager, s).map_err(|_| invalid_input("Invalid JSON"))?; | ||
Ok(result.any) | ||
} | ||
|
||
fn dir(path: &str) -> Option<&str> { | ||
path.rsplit_once('/').map(|(d, _)| d) | ||
} | ||
|
||
fn create_file_path_recursively<T: Io>(io: &T, path: &str) -> io::Result<()> { | ||
dir(path).map_or(Ok(()), |d| io.create_dir_recursively(d)) | ||
} | ||
|
||
pub fn create_file_recursively<T: Io>(io: &T, path: &str) -> io::Result<T::File> { | ||
create_file_path_recursively(io, path)?; | ||
io.create(path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters