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