From 246231332fee977bcf8905a93051829df39eed35 Mon Sep 17 00:00:00 2001 From: Marcello Lamonaca Date: Wed, 14 Feb 2024 23:35:26 +0100 Subject: [PATCH] pass refs to the parser --- src/main.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0848863..9e2dc8d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ fn main() -> Result<()> { let mut vars: Vec = vec![]; let separator = args.separator.unwrap_or("__".to_string()); - JsonParser::parse(&mut vars, "", json, &separator); + JsonParser::parse(&mut vars, "", &json, &separator); let environ = vars .iter() @@ -78,27 +78,27 @@ struct Args { struct JsonParser; impl JsonParser { - fn parse(lines: &mut Vec, key: &str, value: Value, separator: &str) { + fn parse(lines: &mut Vec, key: &str, value: &Value, separator: &str) { match value { Value::Array(array) => { - for (index, item) in array.into_iter().enumerate() { - let key = Self::build_key(key, index.to_string(), separator); - Self::parse(lines, key.as_str(), item, separator) + for (index, item) in array.iter().enumerate() { + let key = Self::build_key(key, index.to_string().as_str(), separator); + Self::parse(lines, &key, item, separator) } } Value::Object(object) => { for (name, value) in object { - let key = Self::build_key(key, name, separator); - Self::parse(lines, key.as_str(), value, separator) + let key = Self::build_key(key, name.as_str(), 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() { - true => key, + true => key.to_string(), false => format!("{prefix}{separator}{key}"), } } @@ -149,7 +149,7 @@ mod tests { let expected = KEY; // ACT - let result = JsonParser::build_key("", input, separator); + let result = JsonParser::build_key("", &input, separator); // ASSERT assert_eq!(result, expected); @@ -163,7 +163,7 @@ mod tests { let expected = format!("prefix{separator}{KEY}"); // ACT - let actual = JsonParser::build_key("prefix", input, separator); + let actual = JsonParser::build_key("prefix", &input, separator); // ASSERT assert_eq!(actual, expected);