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