From 004fe2cc4df70001abb8ccaab193b5cf61aac6c1 Mon Sep 17 00:00:00 2001 From: Alex Snaps Date: Wed, 2 Oct 2024 09:07:21 -0400 Subject: [PATCH] =?UTF-8?q?=20=F0=9F=94=A5=20all=20HashMap=20usages=20wher?= =?UTF-8?q?e=20we=20want=20actual=20ordering?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Alex Snaps --- .../src/http_api/request_types.rs | 4 ++-- limitador/src/counter.rs | 19 ++++--------------- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/limitador-server/src/http_api/request_types.rs b/limitador-server/src/http_api/request_types.rs index d9419c05..6f2fb900 100644 --- a/limitador-server/src/http_api/request_types.rs +++ b/limitador-server/src/http_api/request_types.rs @@ -2,7 +2,7 @@ use limitador::counter::Counter as LimitadorCounter; use limitador::limit::Limit as LimitadorLimit; use paperclip::actix::Apiv2Schema; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; +use std::collections::{BTreeMap, HashMap}; // We need to define the Limit and Counter types. They're basically the same as // defined in the lib but with some modifications to be able to derive @@ -73,7 +73,7 @@ impl From for LimitadorLimit { #[derive(Debug, Eq, PartialEq, Serialize, Apiv2Schema)] pub struct Counter { limit: Limit, - set_variables: HashMap, + set_variables: BTreeMap, remaining: Option, expires_in_seconds: Option, } diff --git a/limitador/src/counter.rs b/limitador/src/counter.rs index 1f7ce923..e9f2b71a 100644 --- a/limitador/src/counter.rs +++ b/limitador/src/counter.rs @@ -1,5 +1,5 @@ use crate::limit::{Limit, Namespace}; -use serde::{Deserialize, Serialize, Serializer}; +use serde::{Deserialize, Serialize}; use std::collections::{BTreeMap, HashMap}; use std::hash::{Hash, Hasher}; use std::sync::Arc; @@ -9,23 +9,12 @@ use std::time::Duration; pub struct Counter { limit: Arc, - // Need to sort to generate the same object when using the JSON as a key or - // value in Redis. - #[serde(serialize_with = "ordered_map")] - set_variables: HashMap, + set_variables: BTreeMap, remaining: Option, expires_in: Option, } -fn ordered_map(value: &HashMap, serializer: S) -> Result -where - S: Serializer, -{ - let ordered: BTreeMap<_, _> = value.iter().collect(); - ordered.serialize(serializer) -} - impl Counter { pub fn new>>(limit: L, set_variables: HashMap) -> Self { // TODO: check that all the variables defined in the limit are set. @@ -36,7 +25,7 @@ impl Counter { Self { limit, - set_variables: vars, + set_variables: vars.into_iter().collect(), remaining: None, expires_in: None, } @@ -80,7 +69,7 @@ impl Counter { self.limit.namespace() } - pub fn set_variables(&self) -> &HashMap { + pub fn set_variables(&self) -> &BTreeMap { &self.set_variables }