diff --git a/limitador-server/src/http_api/request_types.rs b/limitador-server/src/http_api/request_types.rs index c563e411..d9419c05 100644 --- a/limitador-server/src/http_api/request_types.rs +++ b/limitador-server/src/http_api/request_types.rs @@ -30,7 +30,7 @@ pub struct Limit { impl From<&LimitadorLimit> for Limit { fn from(ll: &LimitadorLimit) -> Self { Self { - id: ll.id().clone(), + id: ll.id().map(|id| id.to_string()), namespace: ll.namespace().as_ref().to_string(), max_value: ll.max_value(), seconds: ll.seconds(), diff --git a/limitador/src/counter.rs b/limitador/src/counter.rs index 0b55bd6c..1f7ce923 100644 --- a/limitador/src/counter.rs +++ b/limitador/src/counter.rs @@ -72,7 +72,7 @@ impl Counter { Duration::from_secs(self.limit.seconds()) } - pub fn id(&self) -> &Option { + pub fn id(&self) -> Option<&str> { self.limit.id() } diff --git a/limitador/src/limit.rs b/limitador/src/limit.rs index 883cf2d8..7fe15fd2 100644 --- a/limitador/src/limit.rs +++ b/limitador/src/limit.rs @@ -364,8 +364,8 @@ impl Limit { &self.namespace } - pub fn id(&self) -> &Option { - &self.id + pub fn id(&self) -> Option<&str> { + self.id.as_deref() } pub fn max_value(&self) -> u64 { @@ -1043,6 +1043,6 @@ mod tests { vec!["app_id"], ); - assert_eq!(limit.id().clone(), Some("test_id".to_string())) + assert_eq!(limit.id(), Some("test_id")) } } diff --git a/limitador/src/storage/keys.rs b/limitador/src/storage/keys.rs index 06a3d037..037d5f9b 100644 --- a/limitador/src/storage/keys.rs +++ b/limitador/src/storage/keys.rs @@ -40,26 +40,25 @@ pub fn key_for_counter(counter: &Counter) -> Vec { } pub fn key_for_counters_of_limit(limit: &Limit) -> Vec { - if limit.id().is_none() { - let namespace = limit.namespace().as_ref(); - format!( - "namespace:{{{namespace}}},counters_of_limit:{}", - serde_json::to_string(limit).unwrap() - ) - .into_bytes() - } else { + if let Some(id) = limit.id() { #[derive(PartialEq, Debug, Serialize, Deserialize)] struct IdLimitKey<'a> { id: &'a str, } - let id = limit.id().as_ref().unwrap(); - let key = IdLimitKey { id: id.as_ref() }; + let key = IdLimitKey { id }; let mut encoded_key = Vec::new(); encoded_key = postcard::to_extend(&2u8, encoded_key).unwrap(); encoded_key = postcard::to_extend(&key, encoded_key).unwrap(); encoded_key + } else { + let namespace = limit.namespace().as_ref(); + format!( + "namespace:{{{namespace}}},counters_of_limit:{}", + serde_json::to_string(limit).unwrap() + ) + .into_bytes() } } @@ -182,7 +181,7 @@ pub mod bin { impl<'a> From<&'a Counter> for IdCounterKey<'a> { fn from(counter: &'a Counter) -> Self { IdCounterKey { - id: counter.id().as_ref().unwrap().as_ref(), + id: counter.id().unwrap(), variables: counter.variables_for_key(), } }