Skip to main content

manabrew_engine/spellability/
optional_cost_value.rs

1//! OptionalCostValue — pairs an optional cost type with a description.
2//!
3//! Mirrors Java's `OptionalCostValue.java`.
4
5use serde::{Deserialize, Serialize};
6
7use super::optional_cost::OptionalCost;
8
9/// A valued optional cost with a description string.
10/// Mirrors Java's `OptionalCostValue` — pairs a cost type with
11/// descriptive text for display purposes.
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct OptionalCostValue {
14    pub cost_type: OptionalCost,
15    pub cost_description: String,
16}
17
18impl OptionalCostValue {
19    pub fn new(cost_type: OptionalCost, cost_description: String) -> Self {
20        OptionalCostValue {
21            cost_type,
22            cost_description,
23        }
24    }
25
26    /// Get the optional cost type.
27    /// Mirrors Java's `OptionalCostValue.getType()`.
28    pub fn get_type(&self) -> OptionalCost {
29        self.cost_type
30    }
31
32    /// Get the cost description.
33    /// Mirrors Java's `OptionalCostValue.getCost()`.
34    pub fn get_cost(&self) -> &str {
35        &self.cost_description
36    }
37}
38
39impl std::fmt::Display for OptionalCostValue {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        let name = self.cost_type.name();
42        let is_tag = name.starts_with('(');
43        if self.cost_type != OptionalCost::Generic && !is_tag {
44            write!(f, "{} – {}", name, self.cost_description)?;
45        } else if is_tag {
46            write!(f, "{} {}", self.cost_description, name)?;
47        } else {
48            write!(f, "{}", self.cost_description)?;
49        }
50        Ok(())
51    }
52}