use super::keyword_instance::{Keyword, KeywordInstanceData};
use super::keyword_with_cost_interface::KeywordWithCostTrait;
#[derive(Debug, Clone)]
pub struct KeywordWithCostAndAmount {
pub base: KeywordInstanceData,
pub cost_string: String,
pub with_x: bool,
pub amount: i32,
}
impl KeywordWithCostAndAmount {
pub fn new(keyword: Keyword, original: String) -> Self {
Self {
base: KeywordInstanceData::new(keyword, original),
cost_string: String::new(),
with_x: false,
amount: 0,
}
}
pub fn get_amount(&self) -> i32 {
self.amount
}
pub fn get_amount_string(&self) -> String {
if self.with_x {
"X".to_string()
} else {
self.amount.to_string()
}
}
pub fn get_title(&self) -> String {
format!("{}{}", self.get_title_without_cost(), self.cost_string)
}
pub fn parse(&mut self, details: &str) {
let k: Vec<&str> = details.split(':').collect();
if k[0].starts_with('X') {
self.with_x = true;
} else {
self.amount = k[0].parse::<i32>().unwrap_or(0);
}
if k.len() > 1 {
self.cost_string = k[1].split('|').next().unwrap_or("").trim().to_string();
}
}
pub fn format_reminder_text(&self, reminder_text: &str) -> String {
let format_str = if self.with_x {
reminder_text
.replace("%d", "%s")
.replace("%1$d", "%1$s")
.replace("%2$d", "%2$s")
} else {
reminder_text.to_string()
};
format_str
.replace("%s", &self.cost_string)
.replace("%d", &self.amount.to_string())
.replace("%1$s", &self.cost_string)
.replace(
"%2$s",
&if self.with_x {
"X".to_string()
} else {
self.amount.to_string()
},
)
.replace("%1$d", &self.cost_string)
.replace("%2$d", &self.amount.to_string())
}
}
impl KeywordWithCostTrait for KeywordWithCostAndAmount {
fn get_cost_string(&self) -> &str {
&self.cost_string
}
fn get_title_without_cost(&self) -> String {
format!("{} {}\u{2014}", self.base.keyword, self.get_amount_string())
}
}