use serde::Deserialize;
use super::{
audience::Audience, rollout::Rollout, Attribute, AttributeMap, AudienceMap, Experiment, ExperimentMap, FeatureFlag,
FeatureFlagMap, Revision, RolloutMap,
};
#[cfg(feature = "online")]
use super::{Event, EventMap};
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Environment {
account_id: String,
project_id: String,
environment_key: String,
sdk_key: String,
revision: Revision,
bot_filtering: bool,
#[serde(rename = "anonymizeIP")]
anonymize_ip: bool,
#[cfg(feature = "online")]
events: EventMap,
attributes: AttributeMap,
#[serde(rename = "typedAudiences")]
#[allow(dead_code)]
audiences: AudienceMap,
experiments: ExperimentMap,
rollouts: RolloutMap,
feature_flags: FeatureFlagMap,
}
impl Environment {
pub fn account_id(&self) -> &str {
&self.account_id
}
pub fn sdk_key(&self) -> &str {
&self.sdk_key
}
#[allow(dead_code)]
pub fn project_id(&self) -> &str {
&self.project_id
}
#[allow(dead_code)]
pub fn environment_key(&self) -> &str {
&self.environment_key
}
pub fn revision(&self) -> u32 {
*self.revision
}
#[allow(dead_code)]
pub fn bot_filtering(&self) -> bool {
self.bot_filtering
}
#[allow(dead_code)]
pub fn anonymize_ip(&self) -> bool {
self.anonymize_ip
}
pub(crate) fn flag(&self, flag_key: &str) -> Option<&FeatureFlag> {
self.feature_flags.get(flag_key).or_else(|| {
log::warn!("Flag key '{flag_key}' does not exist in datafile");
None
})
}
pub(crate) fn experiment(&self, experiment_id: &str) -> Option<&Experiment> {
self.experiments.get(experiment_id).or_else(|| {
log::warn!("Experiment ID '{experiment_id}' does not exist in datafile");
None
})
}
pub(crate) fn rollout(&self, rollout_id: &str) -> Option<&Rollout> {
self.rollouts.get(rollout_id).or_else(|| {
log::warn!("Rollout ID '{rollout_id}' does not exist in datafile");
None
})
}
#[cfg(feature = "online")]
pub(crate) fn event(&self, event_key: &str) -> Option<&Event> {
self.events.get(event_key).or_else(|| {
log::warn!("Event key '{event_key}' does not exist in datafile");
None
})
}
pub(crate) fn attribute(&self, attribute_key: &str) -> Option<&Attribute> {
self.attributes.get(attribute_key).or_else(|| {
log::warn!("Attribute key '{attribute_key}' does not exist in datafile");
None
})
}
pub(crate) fn audience(&self, audience_id: &str) -> Option<&Audience> {
self.audiences.get(audience_id).or_else(|| {
log::warn!("Audience id '{audience_id}' does not exist in datafile");
None
})
}
}