hyaenidae/toolkit/src/tile.rs
2021-01-21 23:42:19 -06:00

79 lines
1.8 KiB
Rust

use crate::image::Image;
use std::rc::Rc;
#[derive(Clone, Copy, Debug)]
pub enum TileFlag {
Normal,
Primary,
}
#[derive(Clone)]
pub struct Tile {
pub(crate) title: Option<String>,
pub(crate) description: Option<String>,
pub(crate) link: Option<String>,
pub(crate) flag: TileFlag,
image: Rc<dyn Image>,
}
impl Tile {
pub fn new(image: impl Image + 'static) -> Self {
Tile {
title: None,
description: None,
link: None,
flag: TileFlag::Normal,
image: Rc::new(image),
}
}
pub fn title(mut self, title: &str) -> Self {
self.title = Some(title.to_owned());
self
}
pub fn description(mut self, description: &str) -> Self {
self.description = Some(description.to_owned());
self
}
pub fn link(mut self, link: &str) -> Self {
self.link = Some(link.to_owned());
self
}
pub fn flag(mut self, flag: TileFlag) -> Self {
self.flag = flag;
self
}
pub(crate) fn image(&self) -> &dyn Image {
&*self.image
}
pub(crate) fn class_string(&self) -> String {
let mut classes = vec!["toolkit-tile".to_owned()];
match self.flag {
TileFlag::Normal => (),
TileFlag::Primary => {
classes.push("toolkit-tile__primary".to_owned());
}
}
classes.join(" ")
}
}
impl std::fmt::Debug for Tile {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Tile")
.field("title", &self.title)
.field("description", &self.description)
.field("link", &self.link)
.field("flag", &self.flag)
.field("image", &"Box<dyn Image>")
.finish()
}
}