Skip to main content

manabrew_engine/keyword/
devour.rs

1//! Devour keyword implementation.
2//!
3//! Ported from Java's `Devour.java` in `forge/game/keyword/`.
4
5use super::keyword_instance::Keyword;
6use super::keyword_with_amount::KeywordWithAmount;
7use super::keyword_with_type_interface::KeywordWithTypeTrait;
8
9/// Devour keyword data.
10/// As this creature enters, you may sacrifice creatures. It enters with
11/// +1/+1 counters for each creature sacrificed.
12#[derive(Debug, Clone)]
13pub struct Devour {
14    pub inner: KeywordWithAmount,
15    /// The type to devour (default "Creature").
16    pub type_str: Option<String>,
17    /// Human-readable type description.
18    pub desc_type: String,
19    /// Plural type for reminder text.
20    pub reminder_type: String,
21    /// Extra text.
22    pub extra: Option<String>,
23}
24
25impl Devour {
26    /// Create a new Devour keyword.
27    pub fn new(original: String) -> Self {
28        Self {
29            inner: KeywordWithAmount::new(Keyword::Devour, original),
30            type_str: None,
31            desc_type: "Creature".to_string(),
32            reminder_type: "creatures".to_string(),
33            extra: None,
34        }
35    }
36
37    /// Parse the details string.
38    pub fn parse(&mut self, details: &str) {
39        let d: Vec<&str> = details.split(':').collect();
40        if d[0].starts_with('X') {
41            self.inner.with_x = true;
42        } else {
43            self.inner.amount = d[0].parse::<i32>().unwrap_or(0);
44        }
45        if d.len() > 1 && !d[1].is_empty() {
46            self.type_str = Some(d[1].to_string());
47            self.desc_type = d[1].to_string();
48            // Simple pluralization
49            self.reminder_type = format!("{}s", d[1].to_lowercase());
50        }
51        if d.len() > 2 {
52            self.extra = Some(d[2].to_string());
53        }
54    }
55
56    /// Get the display title.
57    pub fn get_title(&self) -> String {
58        let mut sb = self.inner.base.keyword.display_name().to_string();
59        if self.type_str.is_some() {
60            sb.push(' ');
61            sb.push_str(&self.desc_type);
62        }
63        sb.push(' ');
64        sb.push_str(&self.inner.get_amount_string());
65        if let Some(ref extra) = self.extra {
66            sb.push_str(extra);
67        }
68        sb
69    }
70
71    /// Format reminder text.
72    pub fn format_reminder_text(&self, reminder_text: &str) -> String {
73        if self.inner.with_x {
74            reminder_text
75                .replace("%d", "X")
76                .replace("%1$d", "X")
77                .replace("%1$s", "X")
78                .replace("%2$s", &self.reminder_type)
79                .replace("%s", "X")
80        } else {
81            reminder_text
82                .replace("%d", &self.inner.amount.to_string())
83                .replace("%1$d", &self.inner.amount.to_string())
84                .replace("%1$s", &self.inner.amount.to_string())
85                .replace("%2$s", &self.reminder_type)
86                .replace("%s", &self.inner.amount.to_string())
87        }
88    }
89}
90
91impl KeywordWithTypeTrait for Devour {
92    fn get_valid_type(&self) -> &str {
93        self.type_str.as_deref().unwrap_or("Creature")
94    }
95
96    fn get_type_description(&self) -> &str {
97        &self.desc_type
98    }
99}