| 1 |
|
| 2 |
|
| 3 |
use crate::{ |
| 4 |
Error, Result, |
| 5 |
v4::metric::{Metric, MetricType}, |
| 6 |
}; |
| 7 |
use alloc::borrow::ToOwned; |
| 8 |
use core::{fmt, str::FromStr}; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
| 21 |
pub enum UserInteraction { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
vely |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
Active, |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
ry |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
Passive, |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
None, |
| 57 |
} |
| 58 |
|
| 59 |
impl Default for UserInteraction { |
| 60 |
fn default() -> Self { |
| 61 |
Self::None |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
impl Metric for UserInteraction { |
| 66 |
const TYPE: MetricType = MetricType::UI; |
| 67 |
|
| 68 |
fn as_str(self) -> &'static str { |
| 69 |
match self { |
| 70 |
UserInteraction::None => "N", |
| 71 |
UserInteraction::Passive => "P", |
| 72 |
UserInteraction::Active => "A", |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
impl fmt::Display for UserInteraction { |
| 78 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 79 |
write!(f, "{}:{}", Self::name(), self.as_str()) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
impl FromStr for UserInteraction { |
| 84 |
type Err = Error; |
| 85 |
|
| 86 |
fn from_str(s: &str) -> Result<Self> { |
| 87 |
match s { |
| 88 |
"N" => Ok(UserInteraction::None), |
| 89 |
"P" => Ok(UserInteraction::Passive), |
| 90 |
"A" => Ok(UserInteraction::Active), |
| 91 |
_ => Err(Error::InvalidMetricV4 { |
| 92 |
metric_type: Self::TYPE, |
| 93 |
value: s.to_owned(), |
| 94 |
}), |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
#[cfg(feature = "std")] |
| 100 |
pub(crate) mod merge { |
| 101 |
use super::*; |
| 102 |
use crate::{ |
| 103 |
Error, |
| 104 |
v4::{ |
| 105 |
MetricType, |
| 106 |
metric::{MetricLevel, environmental::ModifiedUserInteraction}, |
| 107 |
}, |
| 108 |
}; |
| 109 |
use alloc::borrow::ToOwned; |
| 110 |
use core::str::FromStr; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
| 116 |
pub(crate) enum MergedUserInteraction { |
| 117 |
Active, |
| 118 |
Passive, |
| 119 |
None, |
| 120 |
} |
| 121 |
|
| 122 |
impl Default for MergedUserInteraction { |
| 123 |
fn default() -> Self { |
| 124 |
Self::None |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
impl FromStr for MergedUserInteraction { |
| 129 |
type Err = Error; |
| 130 |
|
| 131 |
fn from_str(s: &str) -> Result<Self> { |
| 132 |
match s { |
| 133 |
"A" => Ok(MergedUserInteraction::Active), |
| 134 |
"P" => Ok(MergedUserInteraction::Passive), |
| 135 |
"N" => Ok(MergedUserInteraction::None), |
| 136 |
_ => Err(Error::InvalidMetricV4 { |
| 137 |
metric_type: MetricType::UI, |
| 138 |
value: s.to_owned(), |
| 139 |
}), |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
impl MetricLevel for MergedUserInteraction { |
| 145 |
fn level(self) -> f64 { |
| 146 |
|
| 147 |
match self { |
| 148 |
Self::Active => 0.2, |
| 149 |
Self::Passive => 0.1, |
| 150 |
Self::None => 0.0, |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
impl UserInteraction { |
| 156 |
pub(crate) fn merge(self, value: Option<ModifiedUserInteraction>) -> MergedUserInteraction { |
| 157 |
match value { |
| 158 |
Some(ModifiedUserInteraction::NotDefined) | None => match self { |
| 159 |
Self::Passive => MergedUserInteraction::Passive, |
| 160 |
Self::Active => MergedUserInteraction::Active, |
| 161 |
Self::None => MergedUserInteraction::None, |
| 162 |
}, |
| 163 |
Some(ModifiedUserInteraction::Passive) => MergedUserInteraction::Passive, |
| 164 |
Some(ModifiedUserInteraction::Active) => MergedUserInteraction::Active, |
| 165 |
Some(ModifiedUserInteraction::None) => MergedUserInteraction::None, |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
} |