Skip to content

Commit

Permalink
move country
Browse files Browse the repository at this point in the history
  • Loading branch information
cpiemontese committed Mar 5, 2024
1 parent a960ffb commit 80c3ad4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 124 deletions.
3 changes: 2 additions & 1 deletion benches/basic_incr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use criterion::{criterion_group, criterion_main, Criterion};
use prima_datadog::{
configuration::{Configuration, Country},
configuration::{Configuration, Country, Environment},
Datadog, TagTrackerConfiguration,
};

Expand All @@ -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();
}
Expand Down
95 changes: 95 additions & 0 deletions src/configuration/country.rs
Original file line number Diff line number Diff line change
@@ -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<Self, Self::Err> {
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<D>(deserializer: D) -> Result<Self, D::Error>
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<E>(self, v: &str) -> Result<Self::Value, E>
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"]);
}
}
127 changes: 4 additions & 123 deletions src/configuration/mod.rs
Original file line number Diff line number Diff line change
@@ -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
///
Expand Down Expand Up @@ -115,102 +115,10 @@ impl From<Configuration> 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<Self, Self::Err> {
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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<D>(deserializer: D) -> Result<Self, D::Error>
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<E>(self, v: &str) -> Result<Self::Value, E>
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::<Environment>().err());
assert_eq!(None, "whatever".parse::<Environment>().ok());
}

#[test]
pub fn test_tags() {
let count = 1;
Expand All @@ -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);
}
}
}

0 comments on commit 80c3ad4

Please sign in to comment.