🆕 Add YAML input and output format support
This commit is contained in:
Generated
+26
@@ -153,6 +153,7 @@ dependencies = [
|
||||
"json5",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_yaml",
|
||||
"toml",
|
||||
]
|
||||
|
||||
@@ -196,6 +197,12 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.228"
|
||||
@@ -248,6 +255,19 @@ dependencies = [
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_yaml"
|
||||
version = "0.9.34+deprecated"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47"
|
||||
dependencies = [
|
||||
"indexmap",
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
"unsafe-libyaml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.11.1"
|
||||
@@ -316,6 +336,12 @@ version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
||||
|
||||
[[package]]
|
||||
name = "unsafe-libyaml"
|
||||
version = "0.2.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.2"
|
||||
|
||||
@@ -16,6 +16,7 @@ clap = { version = "4.6", features = ["derive"] }
|
||||
json5 = "1.3.1"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
serde_yaml = "0.9.34"
|
||||
toml = "1.1"
|
||||
|
||||
[profile.release]
|
||||
|
||||
+25
-28
@@ -34,6 +34,10 @@ struct Args {
|
||||
#[arg(short = 'e', long)]
|
||||
input_env: bool,
|
||||
|
||||
/// input YAML format
|
||||
#[arg(short = 'y', long)]
|
||||
input_yaml: bool,
|
||||
|
||||
/// output toml format
|
||||
#[arg(short = 'T', long)]
|
||||
output_toml: bool,
|
||||
@@ -42,23 +46,19 @@ struct Args {
|
||||
#[arg(short = 'J', long)]
|
||||
output_json: bool,
|
||||
|
||||
/// output YAML format
|
||||
#[arg(short = 'Y', long)]
|
||||
output_yaml: bool,
|
||||
|
||||
/// the TOML to convert
|
||||
#[arg(default_value = "-")]
|
||||
input: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum InputFormat {
|
||||
JSON,
|
||||
JSON5,
|
||||
Toml,
|
||||
ENV,
|
||||
Auto,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Eq, PartialEq)]
|
||||
enum OutputFormat {
|
||||
JSON,
|
||||
YAML,
|
||||
Toml,
|
||||
}
|
||||
|
||||
@@ -81,33 +81,16 @@ fn main() -> Result<()> {
|
||||
.with_context(|| format!("failed to collect from input: {input}"))?,
|
||||
};
|
||||
|
||||
let input_format = if args.input_json {
|
||||
InputFormat::JSON
|
||||
} else if args.input_json5 {
|
||||
InputFormat::JSON5
|
||||
} else if args.input_toml {
|
||||
InputFormat::Toml
|
||||
} else if args.input_env {
|
||||
InputFormat::ENV
|
||||
} else {
|
||||
InputFormat::Auto
|
||||
};
|
||||
let output_format = if args.output_json {
|
||||
OutputFormat::JSON
|
||||
} else if args.output_toml {
|
||||
OutputFormat::Toml
|
||||
} else if args.output_yaml {
|
||||
OutputFormat::YAML
|
||||
} else {
|
||||
OutputFormat::JSON
|
||||
};
|
||||
|
||||
match input_format {
|
||||
InputFormat::JSON => {}
|
||||
InputFormat::JSON5 => {}
|
||||
InputFormat::Toml => {}
|
||||
InputFormat::ENV => {}
|
||||
InputFormat::Auto => {}
|
||||
}
|
||||
|
||||
if args.input_toml {
|
||||
try_convert_toml(&input_buf, output_format, args.pretty)?;
|
||||
} else if args.input_json {
|
||||
@@ -116,6 +99,8 @@ fn main() -> Result<()> {
|
||||
try_convert_json5(&input_buf, output_format, args.pretty)?;
|
||||
} else if args.input_env {
|
||||
try_convert_env(&input_buf, output_format, args.pretty)?;
|
||||
} else if args.input_yaml {
|
||||
try_convert_yaml(&input_buf, output_format, args.pretty)?;
|
||||
} else {
|
||||
if let Ok(_) = try_convert_json(input_buf.as_str(), output_format, args.pretty) {
|
||||
// pass
|
||||
@@ -123,6 +108,8 @@ fn main() -> Result<()> {
|
||||
// pass
|
||||
} else if let Ok(_) = try_convert_toml(input_buf.as_str(), output_format, args.pretty) {
|
||||
// pass
|
||||
} else if let Ok(_) = try_convert_yaml(input_buf.as_str(), output_format, args.pretty) {
|
||||
// pass
|
||||
} else if let Ok(_) = try_convert_env(input_buf.as_str(), output_format, args.pretty) {
|
||||
// pass
|
||||
} else {
|
||||
@@ -144,6 +131,9 @@ where
|
||||
serde_json::to_writer(io::stdout(), &value)
|
||||
}
|
||||
.with_context(|| "JSON serialization and/or stdout streaming failed")?;
|
||||
} else if output_format == OutputFormat::YAML {
|
||||
serde_yaml::to_writer(io::stdout(), &value)
|
||||
.with_context(|| "JSON serialization and/or stdout streaming failed")?;
|
||||
} else {
|
||||
let value = if pretty {
|
||||
toml::to_string(&value)
|
||||
@@ -186,6 +176,13 @@ fn try_convert_env(input_buf: &str, output_format: OutputFormat, pretty: bool) -
|
||||
do_print(&value, output_format, pretty)
|
||||
}
|
||||
|
||||
fn try_convert_yaml(input_buf: &str, output_format: OutputFormat, pretty: bool) -> Result<()> {
|
||||
let value = serde_yaml::from_str::<serde_yaml::Value>(input_buf)
|
||||
.with_context(|| format!("parsing JSON from {input_buf} failed"))?;
|
||||
|
||||
do_print(&value, output_format, pretty)
|
||||
}
|
||||
|
||||
fn parse_env(input: &str) -> HashMap<String, String> {
|
||||
let mut map: HashMap<String, String> = HashMap::new();
|
||||
input.lines().for_each(|line| {
|
||||
|
||||
Reference in New Issue
Block a user