diff --git a/CHANGELOG.md b/CHANGELOG.md index f87e8d3..e74ea13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +### v0.1.7 + - minor changes and improved test coverage. + ### v0.1.6 - added option to have a string preceding each line, for example to comment it out. diff --git a/Cargo.lock b/Cargo.lock index e9716af..2659417 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -92,7 +92,7 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "glyphrs" -version = "0.1.6" +version = "0.1.7" dependencies = [ "clap", "serde", diff --git a/Cargo.toml b/Cargo.toml index 9dca698..d818606 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "glyphrs" -version = "0.1.6" +version = "0.1.7" edition = "2021" license = "MIT" description = "A text art generator written in Rust" diff --git a/src/fonts/font_handling.rs b/src/fonts/font_handling.rs index 59ceb64..129c70f 100644 --- a/src/fonts/font_handling.rs +++ b/src/fonts/font_handling.rs @@ -9,7 +9,7 @@ struct Outer { #[derive(Debug, Deserialize)] struct Font { name: String, - character: Vec, + character: Vec, key_values: Vec>, } @@ -21,6 +21,9 @@ pub fn get_fonts() -> Vec { font_list.push(keys); } + // make sure the fonts are in alphabetical order + font_list.sort(); + font_list } @@ -31,20 +34,53 @@ fn get_font_file() -> &'static str { contents } -pub fn define_fonts() -> HashMap>> { - let mut fonts: HashMap>> = HashMap::new(); +pub fn define_fonts() -> HashMap>> { + let mut fonts: HashMap>> = HashMap::new(); let contents = get_font_file(); let toml_value: Outer = toml::from_str(contents).expect("Failed to parse config.toml"); for font in toml_value.fonts { - let mut font_map: HashMap> = HashMap::new(); + let mut font_map: HashMap> = HashMap::new(); for i in 0..font.character.len() { - let key = font.character[i]; + let key = &font.character[i]; let value = &font.key_values[i]; - font_map.insert(key, value.to_vec()); + font_map.insert(key.to_string(), value.to_vec()); } fonts.insert(font.name, font_map); } fonts } + +#[cfg(test)] +pub mod tests { + use std::collections::HashMap; + + use crate::fonts::font_handling::{define_fonts, get_font_file, get_fonts, Outer}; + + #[test] + fn test_get_fonts() { + let expected = vec!["blocks_in_two_lines".to_string(), "pipes".to_string()]; + assert_eq!(get_fonts(), expected); + } + + #[test] + fn test_define_fonts() { + let contents = get_font_file(); + let toml_value: Outer = toml::from_str(contents).expect("Failed to parse config.toml"); + + let mut expected: HashMap>> = HashMap::new(); + for font in toml_value.fonts { + let mut font_map: HashMap> = HashMap::new(); + for i in 0..font.character.len() { + let key = &font.character[i]; + let value = &font.key_values[i]; + font_map.insert(key.to_string(), value.to_vec()); + } + + expected.insert(font.name, font_map); + } + + assert_eq!(define_fonts(), expected); + } +} diff --git a/src/message_gen.rs b/src/message_gen.rs index 611704f..272a61a 100644 --- a/src/message_gen.rs +++ b/src/message_gen.rs @@ -1,4 +1,3 @@ - use crate::fonts::font_handling::define_fonts; pub fn sanitize_input(input: String) -> String { @@ -11,7 +10,7 @@ pub fn sanitize_input(input: String) -> String { output } -pub fn map_search(key: char, font: &str) -> Vec { +pub fn map_search(key: String, font: &str) -> Vec { let fonts = define_fonts(); let map = fonts.get(font).unwrap().clone(); @@ -21,7 +20,7 @@ pub fn map_search(key: char, font: &str) -> Vec { value } -fn conserve_spaces(input: Vec) -> Vec { +fn remove_blank_lines(input: Vec) -> Vec { let mut output: Vec = vec![]; for line in input { @@ -43,8 +42,8 @@ pub fn string_composite(characters: Vec>, prefix: String) -> Vec>, prefix: String) -> Vec Vec> = vec![]; for c in input.chars() { - map_values.push(map_search(c, &font)); + map_values.push(map_search(c.to_string(), &font)); } let output: Vec = string_composite(map_values, prefix); @@ -76,35 +75,77 @@ pub mod tests { use std::collections::HashMap; #[test] - fn test_string_composite() { + fn test_remove_blank_lines() { + let input: Vec = vec![ + " ╔╗ ╔╗ ".to_string(), + "╔╝╚╗ ╔╝╚╗".to_string(), + "╚╗╔╝╔══╗╔══╗╚╗╔╝".to_string(), + " ║║ ║╔╗║║══╣ ║║ ".to_string(), + " ║╚╗║║═╣╠══║ ║╚╗".to_string(), + " ╚═╝╚══╝╚══╝ ╚═╝".to_string(), + " ".to_string(), + " ".to_string(), + ]; + let expected: Vec = vec![ + " ╔╗ ╔╗ ".to_string(), + "╔╝╚╗ ╔╝╚╗".to_string(), + "╚╗╔╝╔══╗╔══╗╚╗╔╝".to_string(), + " ║║ ║╔╗║║══╣ ║║ ".to_string(), + " ║╚╗║║═╣╠══║ ║╚╗".to_string(), + " ╚═╝╚══╝╚══╝ ╚═╝".to_string(), + ]; + let output = remove_blank_lines(input); + assert_eq!(output, expected); + } + + #[test] + fn test_prefix() { + let input = "abc"; + let mut map_values: Vec> = vec![]; + for c in input.chars() { + map_values.push(map_search(c.to_string(), "blocks_in_two_lines")); + } + + assert_eq!( + string_composite(map_values.clone(), "".to_string()), + vec!["▄▀▄ ██▄ ▄▀▀ ".to_string(), "█▀█ █▄█ ▀▄▄ ".to_string()] + ); + assert_eq!( + string_composite(map_values.clone(), "# ".to_string()), + vec!["# ▄▀▄ ██▄ ▄▀▀ ".to_string(), "# █▀█ █▄█ ▀▄▄ ".to_string()] + ); + } + + #[test] + fn test_message_gen() { let expected_map: HashMap> = HashMap::from([ ( "blocks_in_two_lines".to_string(), vec![ - "▀█▀ ██▀ ▄▀▀ ▀█▀ ▄▀▀ ▀█▀ █▀▄ █ █▄ █ ▄▀ ▄█ ▀█ ▀██ ".to_string(), - " █ █▄▄ ▄██ █ ▄██ █ █▀▄ █ █ ▀█ ▀▄█ █ █▄ ▄▄█ ".to_string(), + "▄▀▄ ██▄ ▄▀▀ █▀▄ ██▀ █▀ ▄▀ █▄█ █ █ █▄▀ █ █▄ ▄█ █▄ █ ▄▀▄ █▀▄ ▄▀▄ █▀▄ ▄▀▀ ▀█▀ █ █ █ █ █ █ ▀▄▀ ▀▄▀ ▀█▀ ▄█ ▀█ ▀██ █▄ █▄ █▀ ▀█ █▄█ ██ █▀█ █ ▀ ▀ ▀ ▄▀ ▀▄ ".to_string(), + "█▀█ █▄█ ▀▄▄ █▄▀ █▄▄ █▀ ▀▄█ █ █ █ ▀▄█ █ █ █▄▄ █ ▀ █ █ ▀█ ▀▄▀ █▀ ▀▄█ █▀▄ ▄██ █ ▀▄█ ▀▄▀ ▀▄▀▄▀ █ █ █ █▄▄ █ █▄ ▄▄█ █ ▄█ ██ █ █▄█ ▄█ █▄█ ▄ ▄ ▀▄ ▄▀ ".to_string(), ], ), ( "pipes".to_string(), vec![ - " ╔╗ ╔╗ ╔╗ ╔╗ ╔═══╗╔═══╗".to_string(), - "╔╝╚╗ ╔╝╚╗ ╔╝╚╗ ╔╝║ ║╔═╗║║╔═╗║".to_string(), - "╚╗╔╝╔══╗╔══╗╚╗╔╝ ╔══╗╚╗╔╝╔═╗╔╗╔═╗ ╔══╗ ╚╗║ ╚╝╔╝║╚╝╔╝║".to_string(), - " ║║ ║╔╗║║══╣ ║║ ║══╣ ║║ ║╔╝╠╣║╔╗╗║╔╗║ ║║ ╔═╝╔╝╔╗╚╗║".to_string(), - " ║╚╗║║═╣╠══║ ║╚╗ ╠══║ ║╚╗║║ ║║║║║║║╚╝║ ╔╝╚╗║║╚═╗║╚═╝║".to_string(), - " ╚═╝╚══╝╚══╝ ╚═╝ ╚══╝ ╚═╝╚╝ ╚╝╚╝╚╝╚═╗║ ╚══╝╚═══╝╚═══╝".to_string(), - " ╔═╝║ ".to_string(), - " ╚══╝ ".to_string(), + " ╔╗ ╔╗ ╔═╗ ╔╗ ╔╗ ╔╗ ╔╗ ╔╗ ╔═══╗╔═══╗╔╗ ╔╗╔═══╗╔═══╗╔═══╗╔═══╗╔═══╗╔═══╗ ╔╗╔╗╔╗╔╗ ╔═╗╔═╗ ".to_string(), + " ║║ ║║ ║╔╝ ║║ ╔╗║║ ║║ ╔╝╚╗ ╔╝║ ║╔═╗║║╔═╗║║║ ║║║╔══╝║╔══╝║╔═╗║║╔═╗║║╔═╗║║╔═╗║ ║║║║║║║║ ╔╝╔╝╚╗╚╗ ".to_string(), + "╔══╗ ║╚═╗╔══╗╔═╝║╔══╗╔╝╚╗╔══╗║╚═╗╔╗ ╚╝║║╔╗║║ ╔╗╔╗╔═╗ ╔══╗╔══╗╔══╗╔═╗╔══╗╚╗╔╝╔╗╔╗╔╗╔╗╔╗╔╗╔╗╔╗╔╗╔╗ ╔╗╔═══╗╚╗║ ╚╝╔╝║╚╝╔╝║║╚═╝║║╚══╗║╚══╗╚╝╔╝║║╚═╝║║╚═╝║║║ ║║ ║║╚╝╚╝╚╝╔╝╔╝ ╚╗╚╗".to_string(), + "╚ ╗║ ║╔╗║║╔═╝║╔╗║║╔╗║╚╗╔╝║╔╗║║╔╗║╠╣ ╔╗║╚╝╝║║ ║╚╝║║╔╗╗║╔╗║║╔╗║║╔╗║║╔╝║══╣ ║║ ║║║║║╚╝║║╚╝╚╝║╚╬╬╝║║ ║║╠══║║ ║║ ╔═╝╔╝╔╗╚╗║╚══╗║╚══╗║║╔═╗║ ║╔╝║╔═╗║╚══╗║║║ ║║ ╚╝ ║║║ ║║║".to_string(), + "║╚╝╚╗║╚╝║║╚═╗║╚╝║║║═╣ ║║ ║╚╝║║║║║║║ ║║║╔╗╗║╚╗║║║║║║║║║╚╝║║╚╝║║╚╝║║║ ╠══║ ║╚╗║╚╝║╚╗╔╝╚╗╔╗╔╝╔╬╬╗║╚═╝║║║══╣╔╝╚╗║║╚═╗║╚═╝║ ║║╔══╝║║╚═╝║ ║║ ║╚═╝║╔══╝║║╚═╝║╔╗╔╗ ║║║ ║║║".to_string(), + "╚═══╝╚══╝╚══╝╚══╝╚══╝ ╚╝ ╚═╗║╚╝╚╝╚╝ ║║╚╝╚╝╚═╝╚╩╩╝╚╝╚╝╚══╝║╔═╝╚═╗║╚╝ ╚══╝ ╚═╝╚══╝ ╚╝ ╚╝╚╝ ╚╝╚╝╚═╗╔╝╚═══╝╚══╝╚═══╝╚═══╝ ╚╝╚═══╝╚═══╝ ╚╝ ╚═══╝╚═══╝╚═══╝╚╝╚╝ ╚╗╚╗ ╔╝╔╝".to_string(), + " ╔═╝║ ╔╝║ ║║ ║║ ╔═╝║ ╚╗╚╗╔╝╔╝ ".to_string(), + " ╚══╝ ╚═╝ ╚╝ ╚╝ ╚══╝ ╚═╝╚═╝ ".to_string(), ], ), ]); for font in get_fonts() { - let input = "test string 123"; + let input = "abcdefghijklmnopqrstuvwxyz1234567890.!\"\'()"; let mut map_values: Vec> = vec![]; for c in input.chars() { - map_values.push(map_search(c, &font.to_string())); + map_values.push(map_search(c.to_string(), &font.to_string())); } let output: Vec = string_composite(map_values, "".to_string()); let expected: Vec = expected_map.get(&font).unwrap().clone();