-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add --argjson support #250
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,6 +134,15 @@ fn binds(cli: &Cli) -> Result<Vec<(String, Val)>, Error> { | |
let s = s.to_owned(); | ||
Ok((k.to_owned(), Val::Str(s.into()))) | ||
}); | ||
let argjson = cli.argjson.iter().map(|(k, s)| { | ||
let s = s.to_owned(); | ||
use hifijson::token::Lex; | ||
let mut lexer = hifijson::SliceLexer::new(s.as_bytes()); | ||
let v = lexer | ||
.exactly_one(Val::parse) | ||
.map_err(|e| Error::Parse(format!("cannot parse {s} as JSON: {e}"))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably skip or truncate |
||
Ok::<(std::string::String, Val), Error>((k.to_owned(), v?)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My rust knowledge fails me here, compiler not happy about deriving type parameter E for Result without this. Also looking at the other code i guess maybe you would like to refactor this to share parsing of json somehow? |
||
}); | ||
let rawfile = cli.rawfile.iter().map(|(k, path)| { | ||
let s = std::fs::read_to_string(path).map_err(|e| Error::Io(Some(format!("{path:?}")), e)); | ||
Ok((k.to_owned(), Val::Str(s?.into()))) | ||
|
@@ -146,7 +155,7 @@ fn binds(cli: &Cli) -> Result<Vec<(String, Val)>, Error> { | |
let positional = cli.args.iter().cloned().map(|s| Ok(Val::from(s))); | ||
let positional = positional.collect::<Result<Vec<_>, Error>>()?; | ||
|
||
let var_val = arg.chain(rawfile).chain(slurpfile); | ||
let var_val = arg.chain(rawfile).chain(slurpfile).chain(argjson); | ||
let mut var_val = var_val.collect::<Result<Vec<_>, Error>>()?; | ||
|
||
var_val.push(("ARGS".to_string(), args(&positional, &var_val))); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As i understand code i cli just collects stings, reading files, parsing etc should happen in binds?