1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! Protection keyword implementation.
//!
//! Ported from Java's `Protection.java` in `forge/game/keyword/`.
use super::keyword_instance::{Keyword, KeywordInstanceData};
/// Protection keyword data.
/// This creature can't be blocked, targeted, dealt damage, or
/// equipped/enchanted by the specified quality.
#[derive(Debug, Clone)]
pub struct Protection {
pub base: KeywordInstanceData,
/// What this creature has protection from (e.g. "red", "creatures").
pub from_what: String,
}
impl Protection {
/// Create a new Protection keyword.
pub fn new(original: String) -> Self {
// Extract "from what" from the original string if possible.
// E.g. "Protection from red" -> "red"
let from_what = if let Some(rest) = original.strip_prefix("Protection from ") {
rest.to_string()
} else {
String::new()
};
Self {
base: KeywordInstanceData::new(Keyword::Protection, original),
from_what,
}
}
/// Parse the details string.
pub fn parse(&mut self, _details: &str) {
// In Java, parse is a no-op. The from_what is set from the original string.
}
/// Get the display title.
pub fn get_title(&self) -> String {
format!("Protection from {}", self.from_what)
}
/// Format reminder text.
pub fn format_reminder_text(&self, reminder_text: &str) -> String {
reminder_text.replace("%s", &self.from_what)
}
}