-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PLATFORM-1580]: Retire 0.7.0 and fix configuration mixup (#61)
- Loading branch information
1 parent
5b0b6a2
commit e4fbfe2
Showing
12 changed files
with
349 additions
and
426 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use std::str::FromStr; | ||
|
||
use crate::error::Error; | ||
|
||
/// Represent an environment in which the datadog client runs. | ||
/// This is useful for enforcing rules based on environment for every application that uses the library. | ||
#[derive(PartialEq, Eq, Debug, Clone)] | ||
pub enum Environment { | ||
Dev, | ||
Staging, | ||
Production, | ||
} | ||
|
||
impl Environment { | ||
/// Returns the string representation of the environment. | ||
pub fn as_str(&self) -> &'static str { | ||
match self { | ||
Environment::Dev => "dev", | ||
Environment::Staging => "staging", | ||
Environment::Production => "production", | ||
} | ||
} | ||
} | ||
|
||
impl FromStr for Environment { | ||
type Err = Error; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"dev" => Ok(Self::Dev), | ||
"staging" => Ok(Self::Staging), | ||
"production" => Ok(Self::Production), | ||
_ => Err(Error::WrongEnvironmentDefinition), | ||
} | ||
} | ||
} | ||
|
||
impl std::fmt::Display for Environment { | ||
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 Environment { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
struct EnvironmentVisitor; | ||
impl<'de> serde::de::Visitor<'de> for EnvironmentVisitor { | ||
type Value = Environment; | ||
|
||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
formatter.write_str("a environment tag") | ||
} | ||
|
||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> | ||
where | ||
E: serde::de::Error, | ||
{ | ||
Environment::from_str(v).map_err(|_| E::custom("unknown environment tag")) | ||
} | ||
} | ||
deserializer.deserialize_str(EnvironmentVisitor) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::configuration::Configuration; | ||
|
||
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_environment() { | ||
let config = Configuration::new("to_addr", "namespace") | ||
.with_from_addr("from_addr") | ||
.with_environment(Environment::Dev); | ||
|
||
assert_eq!(config.default_tags(), vec!["env:dev"]); | ||
} | ||
} |
Oops, something went wrong.