Skip to main content

figlet_rs/
figlet.rs

1use crate::shared::{
2    load_font_file, parse_font_content, render, FIGcharacter, FIGure, FontData, HeaderLine,
3};
4use std::collections::HashMap;
5
6/// FIGlet font, which will hold the mapping from u32 code to FIGcharacter
7#[derive(Debug, Clone)]
8pub struct FIGlet {
9    pub header_line: HeaderLine,
10    pub comments: String,
11    pub fonts: HashMap<u32, FIGcharacter>,
12}
13
14impl FIGlet {
15    /// generate FIGlet font from string literal
16    pub fn from_content(contents: &str) -> Result<FIGlet, String> {
17        Ok(parse_font_content(contents)?.into())
18    }
19
20    /// generate FIGlet font from specified file
21    pub fn from_file(fontname: &str) -> Result<FIGlet, String> {
22        Ok(load_font_file(fontname)?.into())
23    }
24
25    /// the standard FIGlet font, which you can find [`fontdb`]
26    ///
27    /// [`fontdb`]: http://www.figlet.org/fontdb.cgi
28    pub fn standard() -> Result<FIGlet, String> {
29        Ok(parse_font_content(include_str!("../resources/standard.flf"))?.into())
30    }
31
32    /// the small FIGlet font bundled with the crate
33    pub fn small() -> Result<FIGlet, String> {
34        Ok(parse_font_content(include_str!("../resources/small.flf"))?.into())
35    }
36
37    /// the big FIGlet font bundled with the crate
38    pub fn big() -> Result<FIGlet, String> {
39        Ok(parse_font_content(include_str!("../resources/big.flf"))?.into())
40    }
41
42    /// the slant FIGlet font bundled with the crate
43    pub fn slant() -> Result<FIGlet, String> {
44        Ok(parse_font_content(include_str!("../resources/slant.flf"))?.into())
45    }
46
47    /// convert string literal to FIGure
48    pub fn convert(&self, message: &str) -> Option<FIGure<'_>> {
49        render(&self.header_line, &self.fonts, message)
50    }
51}
52
53impl From<FontData> for FIGlet {
54    fn from(data: FontData) -> Self {
55        Self {
56            header_line: data.header_line,
57            comments: data.comments,
58            fonts: data.fonts,
59        }
60    }
61}