Skip to content

Commit

Permalink
Use %v to print jf version
Browse files Browse the repository at this point in the history
  • Loading branch information
sayanarijit committed May 14, 2023
1 parent 910801c commit 88734f7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jf"
version = "0.2.1"
version = "0.2.2"
edition = "2021"
authors = ["Arijit Basu <[email protected]>"]
description = 'jf: %q "JSON Formatted"'
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ And [VALUE]... [[NAME=]VALUE]... are the values for the placeholders.
- Pass values for positional placeholders in the same order as in the template.
- Pass values for named placeholders using `NAME=VALUE` syntax.
- Do not declare or pass positional placeholders or values after named ones.
- To get the `jf` version number, use `jf %v`.

Example:

Expand Down
21 changes: 21 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use serde_json as json;
use serde_yaml as yaml;
use std::{collections::HashMap, env, fmt::Display};

const VERSION: &str = env!("CARGO_PKG_VERSION");
const USAGE: &str = r#"not enough arguments
USAGE: jf TEMPLATE [VALUE]... [NAME=VALUE]...
Expand All @@ -19,6 +20,7 @@ USAGE: jf TEMPLATE [VALUE]... [NAME=VALUE]...
Pass values for positional placeholders in the same order as in the template.
Pass values for named placeholders using `NAME=VALUE` syntax.
Do not declare or pass positional placeholders or values after named ones.
To get the `jf` version number, use `jf %v`.
EXAMPLE:
Expand Down Expand Up @@ -176,6 +178,10 @@ where
val.push_str(&arg);
last_char = None;
}
('v', Some('%')) => {
val.push_str(&VERSION);
last_char = None;
}
('(', Some('%')) => {
if !is_reading_named_placeholders {
is_reading_named_placeholders = true;
Expand Down Expand Up @@ -418,3 +424,18 @@ fn test_invalid_named_placeholder_error() {
format!("jf: invalid named placeholder '%(foo)x' at column 6, use '%(foo)q' for quoted strings and '%(foo)s' for other values")
);
}

#[test]
fn test_print_version() {
let arg = ["jf v%v"].into_iter().map(Into::into);
assert_eq!(format(arg).unwrap().to_string(), r#""jf v0.2.2""#);

let args = ["{foo: %q, bar: %(bar)q, version: %v}", "foo", "bar=bar"]
.into_iter()
.map(Into::into);

assert_eq!(
format(args).unwrap().to_string(),
r#"{"foo":"foo","bar":"bar","version":"0.2.2"}"#
);
}

0 comments on commit 88734f7

Please sign in to comment.