use crate::{
Error, Result,
v4::metric::{Metric, MetricType},
};
use alloc::borrow::ToOwned;
use core::{fmt, str::FromStr};
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub enum UserInteraction {
Active,
Passive,
None,
}
impl Default for UserInteraction {
fn default() -> Self {
Self::None
}
}
impl Metric for UserInteraction {
const TYPE: MetricType = MetricType::UI;
fn as_str(self) -> &'static str {
match self {
UserInteraction::None => "N",
UserInteraction::Passive => "P",
UserInteraction::Active => "A",
}
}
}
impl fmt::Display for UserInteraction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", Self::name(), self.as_str())
}
}
impl FromStr for UserInteraction {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
match s {
"N" => Ok(UserInteraction::None),
"P" => Ok(UserInteraction::Passive),
"A" => Ok(UserInteraction::Active),
_ => Err(Error::InvalidMetricV4 {
metric_type: Self::TYPE,
value: s.to_owned(),
}),
}
}
}
#[cfg(feature = "std")]
pub(crate) mod merge {
use super::*;
use crate::{
Error,
v4::{
MetricType,
metric::{MetricLevel, environmental::ModifiedUserInteraction},
},
};
use alloc::borrow::ToOwned;
use core::str::FromStr;
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub(crate) enum MergedUserInteraction {
Active,
Passive,
None,
}
impl Default for MergedUserInteraction {
fn default() -> Self {
Self::None
}
}
impl FromStr for MergedUserInteraction {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
match s {
"A" => Ok(MergedUserInteraction::Active),
"P" => Ok(MergedUserInteraction::Passive),
"N" => Ok(MergedUserInteraction::None),
_ => Err(Error::InvalidMetricV4 {
metric_type: MetricType::UI,
value: s.to_owned(),
}),
}
}
}
impl MetricLevel for MergedUserInteraction {
fn level(self) -> f64 {
match self {
Self::Active => 0.2,
Self::Passive => 0.1,
Self::None => 0.0,
}
}
}
impl UserInteraction {
pub(crate) fn merge(self, value: Option<ModifiedUserInteraction>) -> MergedUserInteraction {
match value {
Some(ModifiedUserInteraction::NotDefined) | None => match self {
Self::Passive => MergedUserInteraction::Passive,
Self::Active => MergedUserInteraction::Active,
Self::None => MergedUserInteraction::None,
},
Some(ModifiedUserInteraction::Passive) => MergedUserInteraction::Passive,
Some(ModifiedUserInteraction::Active) => MergedUserInteraction::Active,
Some(ModifiedUserInteraction::None) => MergedUserInteraction::None,
}
}
}
}