From fd6399b5c8b534ed842db464e835652239d56c67 Mon Sep 17 00:00:00 2001 From: sergey-shandar Date: Wed, 13 Mar 2024 08:02:47 -0700 Subject: [PATCH] map_err --- blockset-lib/src/app/get.rs | 7 +++++-- blockset-lib/src/app/mod.rs | 28 +++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/blockset-lib/src/app/get.rs b/blockset-lib/src/app/get.rs index 1d19b0bd..1a34b914 100644 --- a/blockset-lib/src/app/get.rs +++ b/blockset-lib/src/app/get.rs @@ -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::{ @@ -17,7 +20,7 @@ pub fn restore(io: &impl Io, hash: &U224, w: &mut impl Write) -> io::Result<()> pub fn parse_json(io: &impl Io, manager: M, v: Vec) -> io::Result> { 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()), diff --git a/blockset-lib/src/app/mod.rs b/blockset-lib/src/app/mod.rs index 9599744c..3d067f0c 100644 --- a/blockset-lib/src/app/mod.rs +++ b/blockset-lib/src/app/mod.rs @@ -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}, @@ -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>, @@ -133,6 +139,15 @@ fn validate(a: &mut impl Iterator, stdout: &mut impl Write) -> io stdout.println(["valid: ", d.as_str()]) } +fn js_string_to_string(s: &JsStringRef) -> io::Result { + String::from_utf16(s.items()) + .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "Invalid UTF-16")) +} + +fn try_move>(o: Any) -> io::Result { + 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(); @@ -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)?)