use std::io::Cursor;
use murmur3::murmur3_32;
use crate::entity::Entity;
use crate::{Feature, Value};
use super::feature_snapshot::FeatureSnapshot;
use super::AppConfigurationClient;
pub struct FeatureProxy<'a> {
client: &'a dyn AppConfigurationClient,
feature_id: String,
}
impl<'a> FeatureProxy<'a> {
pub(crate) fn new(client: &'a dyn AppConfigurationClient, feature_id: String) -> Self {
Self { client, feature_id }
}
pub fn snapshot(&self) -> crate::errors::Result<FeatureSnapshot> {
self.client.get_feature(&self.feature_id)
}
}
impl<'a> Feature for FeatureProxy<'a> {
fn get_name(&self) -> crate::errors::Result<String> {
self.client.get_feature(&self.feature_id)?.get_name()
}
fn is_enabled(&self) -> crate::errors::Result<bool> {
self.client.get_feature(&self.feature_id)?.is_enabled()
}
fn get_value(&self, entity: &impl Entity) -> crate::errors::Result<Value> {
self.client.get_feature(&self.feature_id)?.get_value(entity)
}
fn get_value_into<T: TryFrom<Value, Error = crate::Error>>(
&self,
entity: &impl Entity,
) -> crate::errors::Result<T> {
self.client
.get_feature(&self.feature_id)?
.get_value_into(entity)
}
}
pub(crate) fn random_value(v: &str) -> u32 {
let max_hash = u32::MAX;
(f64::from(hash(v)) / f64::from(max_hash) * 100.0) as u32
}
fn hash(v: &str) -> u32 {
murmur3_32(&mut Cursor::new(v), 0).expect("Cannot hash the value.")
}