From 2701bfb9b2666436b4143df8301e143f7ebb1de0 Mon Sep 17 00:00:00 2001 From: Lennart Kloock Date: Fri, 7 Jul 2023 12:23:28 +0200 Subject: [PATCH] feat: remove unnecessary clones Remove unnecessary clones when calling get_key --- config/config/src/lib.rs | 4 ++-- config/config/src/sources/cli.rs | 2 +- config/config/src/sources/env.rs | 2 +- config/config/src/sources/file/mod.rs | 2 +- config/config/src/sources/utils.rs | 6 +++--- config/config/src/tests.rs | 16 ++++++++-------- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/config/config/src/lib.rs b/config/config/src/lib.rs index 1a2390e0..09948d2c 100644 --- a/config/config/src/lib.rs +++ b/config/config/src/lib.rs @@ -70,7 +70,7 @@ pub enum ParseError { } pub trait Source { - fn get_key(&self, path: KeyPath) -> Result>; + fn get_key(&self, path: &KeyPath) -> Result>; } /// You don't need to implemented this trait manually. @@ -186,7 +186,7 @@ impl ConfigBuilder { Ok(self .sources .iter() - .map(|s| s.get_key(key_path.clone())) + .map(|s| s.get_key(&key_path)) .collect::>>()? .into_iter() .flatten() diff --git a/config/config/src/sources/cli.rs b/config/config/src/sources/cli.rs index b3381632..4cd38067 100644 --- a/config/config/src/sources/cli.rs +++ b/config/config/src/sources/cli.rs @@ -439,7 +439,7 @@ impl CliSource { } impl Source for CliSource { - fn get_key(&self, path: KeyPath) -> Result> { + fn get_key(&self, path: &KeyPath) -> Result> { utils::get_key(&self.value, path) } } diff --git a/config/config/src/sources/env.rs b/config/config/src/sources/env.rs index 59567eb9..7abb2456 100644 --- a/config/config/src/sources/env.rs +++ b/config/config/src/sources/env.rs @@ -142,7 +142,7 @@ fn parse_to_value(key_type: KeyType, s: &str) -> Result { } impl Source for EnvSource { - fn get_key(&self, path: KeyPath) -> Result> { + fn get_key(&self, path: &KeyPath) -> Result> { utils::get_key(&self.value, path) } } diff --git a/config/config/src/sources/file/mod.rs b/config/config/src/sources/file/mod.rs index 53c322c4..71f56f7d 100644 --- a/config/config/src/sources/file/mod.rs +++ b/config/config/src/sources/file/mod.rs @@ -83,7 +83,7 @@ impl FileSource { } impl Source for FileSource { - fn get_key(&self, path: KeyPath) -> Result> { + fn get_key(&self, path: &KeyPath) -> Result> { utils::get_key(&self.content, path) } } diff --git a/config/config/src/sources/utils.rs b/config/config/src/sources/utils.rs index e98a771e..21cb982f 100644 --- a/config/config/src/sources/utils.rs +++ b/config/config/src/sources/utils.rs @@ -2,14 +2,14 @@ use serde_value::Value; use crate::{KeyPath, Result}; -pub fn get_key(mut current: &Value, path: KeyPath) -> Result> { - for segment in path.clone() { +pub fn get_key(mut current: &Value, path: &KeyPath) -> Result> { + for segment in path { let Value::Map(map) = current else { // Trying to access a field on a non-map type // I'm not sure if we should return an error here panic!("Trying to access a field on a non-map type: {}, {:#?}", path, current); }; - let Some(value) = map.get(&Value::String(segment)) else { + let Some(value) = map.get(&Value::String(segment.clone())) else { return Ok(None); }; diff --git a/config/config/src/tests.rs b/config/config/src/tests.rs index 68e9bd51..f23cda8a 100644 --- a/config/config/src/tests.rs +++ b/config/config/src/tests.rs @@ -56,7 +56,7 @@ fn env() { std::env::set_var("SCUF_ENABLED", "true"); let config = sources::EnvSource::::with_prefix("SCUF").unwrap(); assert_eq!( - config.get_key("enabled".into()).unwrap().unwrap(), + config.get_key(&"enabled".into()).unwrap().unwrap(), Value::Bool(true), ); @@ -65,7 +65,7 @@ fn env() { std::env::set_var("LOGGING__JSON", "false"); let config = sources::EnvSource::::with_joiner(None, "__").unwrap(); assert_eq!( - config.get_key("logging.json".into()).unwrap().unwrap(), + config.get_key(&"logging.json".into()).unwrap().unwrap(), Value::Bool(false), ); @@ -74,7 +74,7 @@ fn env() { std::env::set_var("LOGGING_JSON", "true"); let config = sources::EnvSource::::new().unwrap(); assert_eq!( - config.get_key("logging.json".into()).unwrap().unwrap(), + config.get_key(&"logging.json".into()).unwrap().unwrap(), Value::Bool(true), ); } @@ -87,10 +87,10 @@ fn file() { "#; let config = sources::FileSource::::toml(data).unwrap(); assert_eq!( - config.get_key("test.key".into()).unwrap().unwrap(), + config.get_key(&"test.key".into()).unwrap().unwrap(), Value::String("test_value".to_string()) ); - assert_eq!(config.get_key("test.not_defined".into()).unwrap(), None); + assert_eq!(config.get_key(&"test.not_defined".into()).unwrap(), None); } #[test] @@ -108,15 +108,15 @@ fn cli() { ]); let cli = sources::CliSource::::with_matches(matches).unwrap(); assert_eq!( - cli.get_key("enabled".into()).unwrap().unwrap(), + cli.get_key(&"enabled".into()).unwrap().unwrap(), Value::Bool(true), ); assert_eq!( - cli.get_key("logging.level".into()).unwrap().unwrap(), + cli.get_key(&"logging.level".into()).unwrap().unwrap(), Value::String("INFO".to_string()), ); assert_eq!( - cli.get_key("logging.json".into()).unwrap().unwrap(), + cli.get_key(&"logging.json".into()).unwrap().unwrap(), Value::Bool(false), ); }