Skip to content

Instantly share code, notes, and snippets.

@lily-mara
Created July 27, 2021 04:36
Show Gist options
  • Save lily-mara/bddb1f5be7f85b9e5b9b83ad957d748b to your computer and use it in GitHub Desktop.
Save lily-mara/bddb1f5be7f85b9e5b9b83ad957d748b to your computer and use it in GitHub Desktop.
use std::fs::File;
use nu_errors::ShellError;
use nu_plugin::{serve_plugin, Plugin};
use nu_protocol::{CallInfo, ReturnValue, Signature, SyntaxShape};
struct FromJson;
impl Plugin for FromJson {
fn config(&mut self) -> Result<Signature, ShellError> {
Ok(Signature::build("bad-from-json")
.required("path", SyntaxShape::String, "json file path")
.filter())
}
fn begin_filter(&mut self, info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
let arg = info.args.expect_nth(0)?.expect_string();
let mut f = File::open(arg).unwrap();
let value: serde_json::Value = serde_json::from_reader(&mut f).unwrap();
let values = serde_nu::to_success_return_values(vec![value]).unwrap();
Ok(values)
}
}
fn main() {
serve_plugin(&mut FromJson);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment