use serde::{Deserialize, Serialize};
use super::color::Color;
#[allow(clippy::module_name_repetitions)]
#[derive(Serialize, Deserialize, Debug, Clone, Default, specta::Type)]
#[serde(rename_all = "camelCase")]
pub struct Tile {
character: String,
#[serde(skip_serializing_if = "Option::is_none")]
alt_character: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
color: Option<Color>,
#[serde(skip_serializing_if = "Option::is_none")]
glow_character: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
glow_color: Option<Color>,
}
impl Tile {
pub fn set_character(&mut self, character: &str) {
self.character = String::from(character);
}
pub fn set_alt_character(&mut self, character: &str) {
self.alt_character = Some(String::from(character));
}
pub fn set_color(&mut self, color: &str) {
self.color = Some(Color::from_value(color));
}
pub fn set_glow_color(&mut self, color: &str) {
self.glow_color = Some(Color::from_value(color));
}
pub fn set_glow_character(&mut self, character: &str) {
self.glow_character = Some(String::from(character));
}
pub fn is_default(&self) -> bool {
self.character.is_empty()
&& self.alt_character.is_none()
&& self.color.is_none()
&& self.glow_character.is_none()
&& self.glow_color.is_none()
}
pub fn get_character(&self) -> &str {
&self.character
}
pub fn get_alt_character(&self) -> &str {
self.alt_character
.as_ref()
.map_or("", |alt_character| alt_character)
}
#[must_use]
pub fn get_color(&self) -> Color {
self.color.as_ref().map_or_else(
|| {
tracing::info!("Had to coerce a default color for a tile");
Color::default()
},
std::clone::Clone::clone,
)
}
#[must_use]
pub fn get_glow_color(&self) -> Color {
self.glow_color.as_ref().map_or_else(
|| {
tracing::info!("Had to coerce a default color for a tile");
Color::default()
},
std::clone::Clone::clone,
)
}
#[must_use]
pub fn with_character(mut self, character: &str) -> Self {
self.set_character(character);
self
}
#[must_use]
pub fn with_alt_character(mut self, character: &str) -> Self {
self.set_alt_character(character);
self
}
#[must_use]
pub fn with_color(mut self, color: &str) -> Self {
self.set_color(color);
self
}
#[must_use]
pub fn with_glow_color(mut self, color: &str) -> Self {
self.set_glow_color(color);
self
}
#[must_use]
pub fn with_glow_character(mut self, character: &str) -> Self {
self.set_glow_character(character);
self
}
}