From 80c3ad4f8397623e7cceb15792acc3fc505f07de Mon Sep 17 00:00:00 2001 From: Cristiano Piemontese Date: Tue, 5 Mar 2024 14:31:03 +0100 Subject: [PATCH] move country --- benches/basic_incr.rs | 3 +- src/configuration/country.rs | 95 ++++++++++++++++++++++++++ src/configuration/mod.rs | 127 ++--------------------------------- 3 files changed, 101 insertions(+), 124 deletions(-) create mode 100644 src/configuration/country.rs diff --git a/benches/basic_incr.rs b/benches/basic_incr.rs index 21ae161..be9584e 100644 --- a/benches/basic_incr.rs +++ b/benches/basic_incr.rs @@ -1,6 +1,6 @@ use criterion::{criterion_group, criterion_main, Criterion}; use prima_datadog::{ - configuration::{Configuration, Country}, + configuration::{Configuration, Country, Environment}, Datadog, TagTrackerConfiguration, }; @@ -11,6 +11,7 @@ fn setup(_: &mut Criterion) { .with_custom_action(|_, _, _| {}); let configuration = Configuration::new("0.0.0.0:1234", "prima_datadog_benchmarks") .with_country(Country::It) + .with_environment(Environment::Dev) .with_tracker_configuration(tracker_config); Datadog::init(configuration).unwrap(); } diff --git a/src/configuration/country.rs b/src/configuration/country.rs new file mode 100644 index 0000000..0428bcb --- /dev/null +++ b/src/configuration/country.rs @@ -0,0 +1,95 @@ +use std::{fmt::Display, str::FromStr}; + +use crate::error::Error; + +/// Represents the country in which the datadog client runs. +/// This is useful for enforcing rules based on country for every application that uses the library. +#[derive(PartialEq, Eq, Debug, Clone)] +pub enum Country { + Common, + It, + Es, + Uk, +} + +impl Country { + /// Returns the string representation of the country. + pub fn as_str(&self) -> &'static str { + match self { + Country::Common => "common", + Country::It => "it", + Country::Es => "es", + Country::Uk => "uk", + } + } +} + +impl FromStr for Country { + type Err = Error; + + fn from_str(s: &str) -> Result { + match s { + "common" => Ok(Self::Common), + "it" => Ok(Self::It), + "es" => Ok(Self::Es), + "uk" => Ok(Self::Uk), + _ => Err(Error::WrongCountryDefinition), + } + } +} + +impl Display for Country { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.as_str()) + } +} + +#[cfg_attr(docsrs, doc(cfg(feature = "serde")))] +#[cfg(feature = "serde")] +impl<'de> serde::Deserialize<'de> for Country { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + struct CountryVisitor; + impl<'de> serde::de::Visitor<'de> for CountryVisitor { + type Value = Country; + + fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { + formatter.write_str("a country tag") + } + + fn visit_str(self, v: &str) -> Result + where + E: serde::de::Error, + { + Country::from_str(v).map_err(|_| E::custom("unknown country tag")) + } + } + deserializer.deserialize_str(CountryVisitor) + } +} + +#[cfg(test)] +mod tests { + use crate::configuration::Configuration; + + use super::*; + + #[test] + pub fn test_country() { + let config = Configuration::new("to_addr", "namespace") + .with_from_addr("from_addr") + .with_country(Country::It); + + assert_eq!(config.default_tags(), vec!["prima:country:it"]); + + let config = Configuration::new("to_addr", "namespace") + .with_from_addr("from_addr") + .with_country(Country::It) + .with_country(Country::Es); + + // Datadog tag keys are allowed to map to multiple values, and I suppose we're ok with that too (e.g. cross-country infra down the line?) + assert_eq!(config.default_tags(), vec!["prima:country:it", "prima:country:es"]); + } +} diff --git a/src/configuration/mod.rs b/src/configuration/mod.rs index b597bb8..95c3292 100644 --- a/src/configuration/mod.rs +++ b/src/configuration/mod.rs @@ -1,13 +1,13 @@ //! Configuration module +mod country; mod env; -use crate::error::Error as PrimaDatadogError; +pub use country::Country; +pub use env::Environment; + use crate::TagTrackerConfiguration; use std::fmt::Display; -use std::str::FromStr; - -use self::env::Environment; /// By binding to 0.0.0.0:0 we're just letting the OS assign us a port, and letting anyone send us UDP packets on that port /// @@ -115,102 +115,10 @@ impl From for dogstatsd::Options { } } -/// Represents the country in which the datadog client runs. -/// This is useful for enforcing rules based on country for every application that uses the library. -#[derive(PartialEq, Eq, Debug, Clone)] -pub enum Country { - Common, - It, - Es, - Uk, -} - -impl Country { - /// Returns the string representation of the country. - pub fn as_str(&self) -> &'static str { - match self { - Country::Common => "common", - Country::It => "it", - Country::Es => "es", - Country::Uk => "uk", - } - } -} - -impl FromStr for Country { - type Err = PrimaDatadogError; - - fn from_str(s: &str) -> Result { - match s { - "common" => Ok(Self::Common), - "it" => Ok(Self::It), - "es" => Ok(Self::Es), - "uk" => Ok(Self::Uk), - _ => Err(PrimaDatadogError::WrongCountryDefinition), - } - } -} - -impl Display for Country { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str(self.as_str()) - } -} - -#[cfg_attr(docsrs, doc(cfg(feature = "serde")))] -#[cfg(feature = "serde")] -impl serde::Serialize for Country { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - serializer.serialize_str(self.as_str()) - } -} - -#[cfg_attr(docsrs, doc(cfg(feature = "serde")))] -#[cfg(feature = "serde")] -impl<'de> serde::Deserialize<'de> for Country { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - struct CountryVisitor; - impl<'de> serde::de::Visitor<'de> for CountryVisitor { - type Value = Country; - - fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { - formatter.write_str("a country tag") - } - - fn visit_str(self, v: &str) -> Result - where - E: serde::de::Error, - { - Country::from_str(v).map_err(|_| E::custom("unknown country tag")) - } - } - deserializer.deserialize_str(CountryVisitor) - } -} - #[cfg(test)] mod tests { use super::*; - #[test] - pub fn test_from_str() { - assert_eq!(Some(Environment::Dev), "dev".parse().ok()); - assert_eq!(Some(Environment::Staging), "staging".parse().ok()); - assert_eq!(Some(Environment::Production), "production".parse().ok()); - } - - #[test] - pub fn test_from_str_err() { - //assert_eq!(None, "".parse::().err()); - assert_eq!(None, "whatever".parse::().ok()); - } - #[test] pub fn test_tags() { let count = 1; @@ -221,31 +129,4 @@ mod tests { assert_eq!(config.default_tags(), vec!["key:value", "count:1"]); } - - #[test] - pub fn test_country() { - let config = Configuration::new("to_addr", "namespace") - .with_from_addr("from_addr") - .with_country(Country::It); - - assert_eq!(config.default_tags(), vec!["prima:country:it"]); - - let config = Configuration::new("to_addr", "namespace") - .with_from_addr("from_addr") - .with_country(Country::It) - .with_country(Country::Es); - - // Datadog tag keys are allowed to map to multiple values, and I suppose we're ok with that too (e.g. cross-country infra down the line?) - assert_eq!(config.default_tags(), vec!["prima:country:it", "prima:country:es"]); - } - - #[cfg(feature = "serde")] - #[test] - fn test_country_serde() { - for country in [Country::Common, Country::It, Country::Es, Country::Uk] { - let serialized = serde_json::to_string(&country).unwrap(); - let deserialized: Country = serde_json::from_str(&serialized).unwrap(); - assert_eq!(country, deserialized); - } - } }