mirror of
https://github.com/m-lamonaca/json-to-env.git
synced 2025-04-05 10:26:40 +00:00
improve parser ergonomics
This commit is contained in:
parent
8cefe25d62
commit
15d7ee5dcc
1 changed files with 27 additions and 10 deletions
37
src/main.rs
37
src/main.rs
|
@ -37,10 +37,10 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
args.enumerate_array,
|
args.enumerate_array,
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut vars: Vec<EnvVar> = vec![];
|
let mut parser = JsonParser::new(options);
|
||||||
JsonParser::parse(&mut vars, "", &json, &options);
|
let keys = parser.parse(&json);
|
||||||
|
|
||||||
let environ = vars
|
let environ = keys
|
||||||
.iter()
|
.iter()
|
||||||
.map(ToString::to_string)
|
.map(ToString::to_string)
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
|
@ -88,7 +88,7 @@ struct Args {
|
||||||
enumerate_array: bool,
|
enumerate_array: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
struct ParseOptions {
|
struct ParseOptions {
|
||||||
key_separator: String,
|
key_separator: String,
|
||||||
array_separator: String,
|
array_separator: String,
|
||||||
|
@ -105,16 +105,32 @@ impl ParseOptions {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct JsonParser;
|
#[derive(Debug, Clone)]
|
||||||
|
struct JsonParser {
|
||||||
|
options: ParseOptions,
|
||||||
|
vars: Vec<EnvVar>,
|
||||||
|
}
|
||||||
|
|
||||||
impl JsonParser {
|
impl JsonParser {
|
||||||
fn parse(lines: &mut Vec<EnvVar>, key: &str, value: &Value, options: &ParseOptions) {
|
fn new(options: ParseOptions) -> Self {
|
||||||
|
Self {
|
||||||
|
options,
|
||||||
|
vars: vec![],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse(&mut self, json: &Value) -> &Vec<EnvVar> {
|
||||||
|
Self::parse_keys(&mut self.vars, "", json, &self.options);
|
||||||
|
&self.vars
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_keys(lines: &mut Vec<EnvVar>, key: &str, value: &Value, options: &ParseOptions) {
|
||||||
match value {
|
match value {
|
||||||
Value::Array(array) => {
|
Value::Array(array) => {
|
||||||
if options.enumerate_array {
|
if options.enumerate_array {
|
||||||
for (index, item) in array.iter().enumerate() {
|
for (index, item) in array.iter().enumerate() {
|
||||||
let key = Self::build_key(key, &index.to_string(), &options.key_separator);
|
let key = Self::build_key(key, &index.to_string(), &options.key_separator);
|
||||||
Self::parse(lines, &key, item, options)
|
Self::parse_keys(lines, &key, item, options)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let value = array
|
let value = array
|
||||||
|
@ -125,13 +141,13 @@ impl JsonParser {
|
||||||
|
|
||||||
let item = serde_json::Value::String(value);
|
let item = serde_json::Value::String(value);
|
||||||
|
|
||||||
Self::parse(lines, key, &item, options)
|
Self::parse_keys(lines, key, &item, options)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Value::Object(object) => {
|
Value::Object(object) => {
|
||||||
for (name, value) in object {
|
for (name, value) in object {
|
||||||
let key = Self::build_key(key, name.as_str(), &options.key_separator);
|
let key = Self::build_key(key, name, &options.key_separator);
|
||||||
Self::parse(lines, &key, value, options)
|
Self::parse_keys(lines, &key, value, options)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => lines.push(EnvVar(key.trim().to_owned(), value.clone())),
|
_ => lines.push(EnvVar(key.trim().to_owned(), value.clone())),
|
||||||
|
@ -146,6 +162,7 @@ impl JsonParser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
struct EnvVar(String, Value);
|
struct EnvVar(String, Value);
|
||||||
|
|
||||||
impl Display for EnvVar {
|
impl Display for EnvVar {
|
||||||
|
|
Loading…
Add table
Reference in a new issue