Skip to content

Commit

Permalink
map_err
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Mar 13, 2024
1 parent 1c64996 commit fd6399b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
7 changes: 5 additions & 2 deletions blockset-lib/src/app/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::io::{self, Write};

use io_trait::Io;
use nanvm_lib::{
js::any::Any, mem::manager::Manager, parser::{parse_with_tokens, Context}, tokenizer::tokenize
js::any::Any,
mem::manager::Manager,
parser::{parse_with_tokens, Context},
tokenizer::tokenize,
};

use crate::{
Expand All @@ -17,7 +20,7 @@ pub fn restore(io: &impl Io, hash: &U224, w: &mut impl Write) -> io::Result<()>

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)
.or_else(|_| Err(io::Error::new(io::ErrorKind::InvalidData, "Invalid UTF-8")))?;
.map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "Invalid UTF-8"))?;
let tokens = tokenize(s);
let result = parse_with_tokens(
&Context::new(manager, io, String::default()),
Expand Down
28 changes: 25 additions & 3 deletions blockset-lib/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use std::io::{self, Cursor, ErrorKind, Read, Write};
use add_entry::add_entry;

use io_trait::Io;
use nanvm_lib::mem::global::GLOBAL;
use nanvm_lib::{
js::{any::Any, any_cast::AnyCast, js_object::JsObjectRef, js_string::JsStringRef},
mem::{global::GLOBAL, manager::Dealloc},
};

use crate::{
cdt::{main_tree::MainTreeAdd, tree_add::TreeAdd},
Expand All @@ -23,7 +26,10 @@ use crate::{
uint::u224::U224,
};

use self::{add::posix_path, get::{parse_json, restore}};
use self::{
add::posix_path,
get::{parse_json, restore},
};

fn set_progress(
state: &mut StatusLine<'_, impl Io>,
Expand Down Expand Up @@ -133,6 +139,15 @@ fn validate(a: &mut impl Iterator<Item = String>, stdout: &mut impl Write) -> io
stdout.println(["valid: ", d.as_str()])
}

fn js_string_to_string(s: &JsStringRef<impl Dealloc>) -> io::Result<String> {
String::from_utf16(s.items())
.map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "Invalid UTF-16"))
}

fn try_move<D: Dealloc, T: AnyCast<D>>(o: Any<D>) -> io::Result<T> {
o.try_move().map_err(|_| invalid_input("invalid JSON"))
}

pub fn run(io: &impl Io) -> io::Result<()> {
let stdout = &mut io.stdout();
let mut a = io.args();
Expand All @@ -149,7 +164,14 @@ pub fn run(io: &impl Io) -> io::Result<()> {
let mut buffer = Vec::default();
let mut w = Cursor::new(&mut buffer);
restore(io, &d, &mut w)?;
let json = parse_json(io, GLOBAL, buffer)?;
let json: JsObjectRef<_> = try_move(parse_json(io, GLOBAL, buffer)?)?;
for (k, v) in json.items() {
stdout.println([
js_string_to_string(k)?.as_str(),
": ",
js_string_to_string(&try_move(v.clone())?)?.as_str(),
])?;
}
Ok(())
} else {
restore(io, &d, &mut io.create(&path)?)
Expand Down

0 comments on commit fd6399b

Please sign in to comment.