#![allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AsciiPalette {
Standard,
Extended,
BlockElements,
Minimal,
}
#[derive(Debug, Clone)]
pub struct AsciiArtView {
pub palette: AsciiPalette,
pub cell_width: u32,
pub cell_height: u32,
pub colored: bool,
pub invert: bool,
pub enabled: bool,
}
impl AsciiArtView {
pub fn new() -> Self {
AsciiArtView {
palette: AsciiPalette::Standard,
cell_width: 8,
cell_height: 16,
colored: false,
invert: false,
enabled: true,
}
}
}
impl Default for AsciiArtView {
fn default() -> Self {
Self::new()
}
}
pub fn new_ascii_art_view() -> AsciiArtView {
AsciiArtView::new()
}
pub fn aav_set_palette(view: &mut AsciiArtView, palette: AsciiPalette) {
view.palette = palette;
}
pub fn aav_set_cell_size(view: &mut AsciiArtView, width: u32, height: u32) {
view.cell_width = width.clamp(4, 32);
view.cell_height = height.clamp(4, 32);
}
pub fn aav_set_colored(view: &mut AsciiArtView, colored: bool) {
view.colored = colored;
}
pub fn aav_set_invert(view: &mut AsciiArtView, invert: bool) {
view.invert = invert;
}
pub fn aav_set_enabled(view: &mut AsciiArtView, enabled: bool) {
view.enabled = enabled;
}
pub fn aav_to_json(view: &AsciiArtView) -> String {
let palette = match view.palette {
AsciiPalette::Standard => "standard",
AsciiPalette::Extended => "extended",
AsciiPalette::BlockElements => "block_elements",
AsciiPalette::Minimal => "minimal",
};
format!(
r#"{{"palette":"{}","cell_width":{},"cell_height":{},"colored":{},"invert":{},"enabled":{}}}"#,
palette, view.cell_width, view.cell_height, view.colored, view.invert, view.enabled
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_palette() {
let v = new_ascii_art_view();
assert_eq!(
v.palette,
AsciiPalette::Standard
);
}
#[test]
fn test_set_palette() {
let mut v = new_ascii_art_view();
aav_set_palette(&mut v, AsciiPalette::BlockElements);
assert_eq!(
v.palette,
AsciiPalette::BlockElements
);
}
#[test]
fn test_cell_size_clamped() {
let mut v = new_ascii_art_view();
aav_set_cell_size(&mut v, 0, 0);
assert_eq!(v.cell_width, 4 );
assert_eq!(v.cell_height, 4 );
}
#[test]
fn test_cell_size_max_clamped() {
let mut v = new_ascii_art_view();
aav_set_cell_size(&mut v, 100, 100);
assert_eq!(v.cell_width, 32 );
}
#[test]
fn test_set_colored() {
let mut v = new_ascii_art_view();
aav_set_colored(&mut v, true);
assert!(v.colored );
}
#[test]
fn test_set_invert() {
let mut v = new_ascii_art_view();
aav_set_invert(&mut v, true);
assert!(v.invert );
}
#[test]
fn test_set_enabled() {
let mut v = new_ascii_art_view();
aav_set_enabled(&mut v, false);
assert!(!v.enabled );
}
#[test]
fn test_to_json_has_palette() {
let v = new_ascii_art_view();
let j = aav_to_json(&v);
assert!(j.contains("\"palette\"") );
}
#[test]
fn test_enabled_default() {
let v = new_ascii_art_view();
assert!(v.enabled );
}
#[test]
fn test_default_cell_size() {
let v = new_ascii_art_view();
assert_eq!(v.cell_width, 8 );
assert_eq!(v.cell_height, 16 );
}
}