🔧 Update .gitignore to exclude JetBrains IDEA configuration directories.

This commit is contained in:
2026-06-20 00:58:15 +08:00
parent 30bc828065
commit a616a21427
4 changed files with 531 additions and 0 deletions
+1
View File
@@ -1,3 +1,4 @@
.idea/
# ---> Rust
# Generated by Cargo
# will have compiled files and executables
Generated
+333
View File
@@ -0,0 +1,333 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "anstream"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
[[package]]
name = "anstyle-parse"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys",
]
[[package]]
name = "anyhow"
version = "1.0.102"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c"
[[package]]
name = "clap"
version = "4.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2ce8604710f6733aa641a2b3731eaa1e8b3d9973d5e3565da11800813f997a9"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"
[[package]]
name = "colorchoice"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
[[package]]
name = "equivalent"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
[[package]]
name = "hashbrown"
version = "0.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "indexmap"
version = "2.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
[[package]]
name = "itoa"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
[[package]]
name = "json-toml"
version = "1.4.1"
dependencies = [
"anyhow",
"clap",
"serde",
"serde_json",
"toml",
]
[[package]]
name = "memchr"
version = "2.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88904434abc2901f197fe8cc55f0445e7ded921dba5911dad2e2b39b48e663c4"
[[package]]
name = "once_cell_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
[[package]]
name = "proc-macro2"
version = "1.0.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
dependencies = [
"proc-macro2",
]
[[package]]
name = "serde"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
dependencies = [
"serde_core",
"serde_derive",
]
[[package]]
name = "serde_core"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.150"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9"
dependencies = [
"itoa",
"memchr",
"serde",
"serde_core",
"zmij",
]
[[package]]
name = "serde_spanned"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6662b5879511e06e8999a8a235d848113e942c9124f211511b16466ee2995f26"
dependencies = [
"serde_core",
]
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.118"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b9ae57f904213ebb649ce6895b8a66c66f0203b9319718f69a5612a065b1422"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "toml"
version = "1.1.2+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81f3d15e84cbcd896376e6730314d59fb5a87f31e4b038454184435cd57defee"
dependencies = [
"indexmap",
"serde_core",
"serde_spanned",
"toml_datetime",
"toml_parser",
"toml_writer",
"winnow",
]
[[package]]
name = "toml_datetime"
version = "1.1.1+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3165f65f62e28e0115a00b2ebdd37eb6f3b641855f9d636d3cd4103767159ad7"
dependencies = [
"serde_core",
]
[[package]]
name = "toml_parser"
version = "1.1.2+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2abe9b86193656635d2411dc43050282ca48aa31c2451210f4202550afb7526"
dependencies = [
"winnow",
]
[[package]]
name = "toml_writer"
version = "1.1.1+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "756daf9b1013ebe47a8776667b466417e2d4c5679d441c26230efd9ef78692db"
[[package]]
name = "unicode-ident"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]
[[package]]
name = "winnow"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0592e1c9d151f854e6fd382574c3a0855250e1d9b2f99d9281c6e6391af352f1"
[[package]]
name = "zmij"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
+21
View File
@@ -0,0 +1,21 @@
[package]
name = "json-toml"
description = "Convert TOML and JSON"
license = "MIT"
version = "1.4.1"
authors = ["William Woodruff <william@yossarian.net>"]
edition = "2021"
homepage = "https://github.com/woodruffw/toml2json"
readme = "README.md"
keywords = ["cli", "toml", "json"]
categories = ["command-line-utilities", "development-tools", "encoding"]
[dependencies]
anyhow = "1.0"
clap = { version = "4.6", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
toml = "1.1"
[profile.release]
lto = true
+176
View File
@@ -0,0 +1,176 @@
#![forbid(unsafe_code)]
use std::collections::HashMap;
use std::fs;
use std::io::{self, Read, Write};
use anyhow::{anyhow, Context, Result};
use clap::Parser;
/// Convert TOML to JSON
#[derive(Parser)]
#[command(name = env!("CARGO_PKG_NAME"))]
#[command(version = env!("CARGO_PKG_VERSION"))]
#[command(about = env!("CARGO_PKG_DESCRIPTION"))]
struct Args {
/// pretty print the JSON
#[arg(short, long)]
pretty: bool,
/// input toml format
#[arg(short = 't', long)]
input_toml: bool,
/// import JOSN format
#[arg(short = 'j', long)]
input_json: bool,
/// import ENV format
#[arg(short = 'e', long)]
input_env: bool,
/// the TOML to convert
#[arg(default_value = "-")]
input: String,
}
fn main() -> Result<()> {
let args = Args::parse();
// Get our input source (which can be - or a filename) and its
// corresponding buffer. We don't bother streaming or chunking,
// since the `toml` crate only supports slices and strings.
let input_src = &args.input;
let input_buf = match input_src.as_ref() {
"-" => {
let mut input_buf = String::new();
io::stdin()
.read_to_string(&mut input_buf)
.with_context(|| "failed to collect stdin")?;
input_buf
}
input => fs::read_to_string(input)
.with_context(|| format!("failed to collect from input: {input}"))?,
};
if args.input_toml && args.input_json {
return Err(anyhow!(
"--input-toml and --input-json cannot be used together"
));
}
if args.input_toml {
try_convert_toml(&input_buf, args.pretty)?;
} else if args.input_json {
try_convert_json(&input_buf, args.pretty)?;
} else if args.input_env {
try_convert_env(&input_buf, args.pretty)?;
} else {
if let Ok(_) = try_convert_json(input_buf.as_str(), args.pretty) {
// pass
} else if let Ok(_) = try_convert_toml(input_buf.as_str(), args.pretty) {
// pass
} else {
return Err(anyhow!("parse JSON or toml both failed"));
}
}
Ok(())
}
fn try_convert_toml(input_buf: &str, pretty: bool) -> Result<()> {
// Turn our collected input into a value. We can't be more specific than
// value, since we're doing arbitrary valid TOML conversions.
let value = toml::from_str::<toml::Value>(&input_buf)
.with_context(|| format!("parsing TOML from {input_buf} failed"))?;
// Spit back out, but as JSON. `serde_json` *does* support streaming, so
// we do it.
if pretty {
serde_json::to_writer_pretty(io::stdout(), &value)
} else {
serde_json::to_writer(io::stdout(), &value)
}
.with_context(|| "JSON serialization and/or stdout streaming failed")?;
Ok(())
}
fn try_convert_json(input_buf: &str, pretty: bool) -> Result<()> {
let value = serde_json::from_str::<serde_json::Value>(input_buf)
.with_context(|| format!("parsing JSON from {input_buf} failed"))?;
let value = if pretty {
toml::to_string(&value)
} else {
toml::to_string_pretty(&value)
}
.with_context(|| "Toml serialization and/or stdout streaming failed")?;
io::stdout()
.write_all(&value.as_bytes())
.with_context(|| "stdout failed")?;
Ok(())
}
fn try_convert_env(input_buf: &str, pretty: bool) -> Result<()> {
let value = parse_env(&input_buf);
if pretty {
serde_json::to_writer_pretty(io::stdout(), &value)
} else {
serde_json::to_writer(io::stdout(), &value)
}
.with_context(|| "JSON serialization and/or stdout streaming failed")?;
Ok(())
}
fn parse_env(input: &str) -> HashMap<String, String> {
let mut map: HashMap<String, String> = HashMap::new();
input.lines().for_each(|line| {
let ln = extract_env_line(line);
if let Some((key, value)) = ln.split_once("=") {
map.insert(key.to_string(), value.to_string());
}
});
map
}
fn extract_env_line(line: &str) -> String {
let mut in_simple_quoted = false;
let mut in_double_quoted = false;
let mut previous_char = 0 as char;
let mut previous2_char = 0 as char;
let mut buff = String::new();
for ch in line.chars() {
if in_simple_quoted {
if ch == '\'' {
in_simple_quoted = false;
}
} else if in_double_quoted {
if ch == '"' {
in_double_quoted = false;
}
} else if ch == '"' {
in_simple_quoted = true;
} else if ch == '\'' {
in_double_quoted = true;
} else if ch == '#' && is_char_space(previous_char) {
return buff;
} else if ch == '/' {
if previous_char == '/' && is_char_space(previous2_char) {
return buff;
}
}
previous2_char = previous_char;
previous_char = ch;
buff.push(ch);
}
buff
}
fn is_char_space(ch: char) -> bool {
ch == ' ' || ch == '\t'
}