pass refs to the parser

This commit is contained in:
Marcello 2024-02-14 23:35:26 +01:00
parent 5b6736b1fd
commit 246231332f
No known key found for this signature in database

View file

@ -33,7 +33,7 @@ fn main() -> Result<()> {
let mut vars: Vec<EnvVar> = vec![]; let mut vars: Vec<EnvVar> = vec![];
let separator = args.separator.unwrap_or("__".to_string()); let separator = args.separator.unwrap_or("__".to_string());
JsonParser::parse(&mut vars, "", json, &separator); JsonParser::parse(&mut vars, "", &json, &separator);
let environ = vars let environ = vars
.iter() .iter()
@ -78,27 +78,27 @@ struct Args {
struct JsonParser; struct JsonParser;
impl JsonParser { impl JsonParser {
fn parse(lines: &mut Vec<EnvVar>, key: &str, value: Value, separator: &str) { fn parse(lines: &mut Vec<EnvVar>, key: &str, value: &Value, separator: &str) {
match value { match value {
Value::Array(array) => { Value::Array(array) => {
for (index, item) in array.into_iter().enumerate() { for (index, item) in array.iter().enumerate() {
let key = Self::build_key(key, index.to_string(), separator); let key = Self::build_key(key, index.to_string().as_str(), separator);
Self::parse(lines, key.as_str(), item, separator) Self::parse(lines, &key, item, separator)
} }
} }
Value::Object(object) => { Value::Object(object) => {
for (name, value) in object { for (name, value) in object {
let key = Self::build_key(key, name, separator); let key = Self::build_key(key, name.as_str(), separator);
Self::parse(lines, key.as_str(), value, separator) Self::parse(lines, &key, value, separator)
} }
} }
_ => lines.push(EnvVar::new(key.trim().to_owned(), value)), _ => lines.push(EnvVar::new(key.trim().to_owned(), value.clone())),
} }
} }
fn build_key(prefix: &str, key: String, separator: &str) -> String { fn build_key(prefix: &str, key: &str, separator: &str) -> String {
match prefix.is_empty() { match prefix.is_empty() {
true => key, true => key.to_string(),
false => format!("{prefix}{separator}{key}"), false => format!("{prefix}{separator}{key}"),
} }
} }
@ -149,7 +149,7 @@ mod tests {
let expected = KEY; let expected = KEY;
// ACT // ACT
let result = JsonParser::build_key("", input, separator); let result = JsonParser::build_key("", &input, separator);
// ASSERT // ASSERT
assert_eq!(result, expected); assert_eq!(result, expected);
@ -163,7 +163,7 @@ mod tests {
let expected = format!("prefix{separator}{KEY}"); let expected = format!("prefix{separator}{KEY}");
// ACT // ACT
let actual = JsonParser::build_key("prefix", input, separator); let actual = JsonParser::build_key("prefix", &input, separator);
// ASSERT // ASSERT
assert_eq!(actual, expected); assert_eq!(actual, expected);