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