use crate::error::FigletError;
use crate::filter::RenderGrid;
#[cfg(feature = "output-html")]
pub mod html;
#[cfg(feature = "output-irc")]
pub mod irc;
#[cfg(feature = "output-svg")]
pub mod svg;
#[cfg(any(feature = "output-html", feature = "output-svg"))]
mod common;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ExportFormat {
Html,
Irc,
Svg,
AnsiTrue,
Ansi256,
Ansi16,
}
impl ExportFormat {
#[must_use]
pub const fn name(&self) -> &'static str {
match self {
ExportFormat::Html => "html",
ExportFormat::Irc => "irc",
ExportFormat::Svg => "svg",
ExportFormat::AnsiTrue => "ansi-true",
ExportFormat::Ansi256 => "ansi-256",
ExportFormat::Ansi16 => "ansi-16",
}
}
}
pub fn write_export(grid: &RenderGrid, fmt: ExportFormat) -> Result<Vec<u8>, FigletError> {
let _ = grid;
match fmt {
#[cfg(feature = "output-html")]
ExportFormat::Html => Ok(html::write_html(grid).into_bytes()),
#[cfg(not(feature = "output-html"))]
ExportFormat::Html => Err(unsupported("html")),
#[cfg(feature = "output-irc")]
ExportFormat::Irc => Ok(irc::write_irc(grid, false)),
#[cfg(not(feature = "output-irc"))]
ExportFormat::Irc => Err(unsupported("irc")),
#[cfg(feature = "output-svg")]
ExportFormat::Svg => Ok(svg::write_svg(grid).into_bytes()),
#[cfg(not(feature = "output-svg"))]
ExportFormat::Svg => Err(unsupported("svg")),
ExportFormat::AnsiTrue | ExportFormat::Ansi256 | ExportFormat::Ansi16 => {
Err(unsupported(fmt.name()))
}
}
}
fn unsupported(requested: &str) -> FigletError {
let available: Vec<String> = available_format_names();
FigletError::UnsupportedExportFormat {
requested: requested.to_owned(),
available,
}
}
fn available_format_names() -> Vec<String> {
#[allow(unused_mut)]
let mut v: Vec<String> = Vec::with_capacity(3);
#[cfg(feature = "output-html")]
{
v.push("html".to_owned());
}
#[cfg(feature = "output-irc")]
{
v.push("irc".to_owned());
}
#[cfg(feature = "output-svg")]
{
v.push("svg".to_owned());
}
v
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn export_format_names_are_canonical() {
assert_eq!(ExportFormat::Html.name(), "html");
assert_eq!(ExportFormat::Irc.name(), "irc");
assert_eq!(ExportFormat::Svg.name(), "svg");
}
#[test]
fn dispatch_returns_unsupported_for_ansi_in_phase7() {
let grid = RenderGrid::blank(1, 1);
let err = write_export(&grid, ExportFormat::AnsiTrue).unwrap_err();
match err {
FigletError::UnsupportedExportFormat { requested, .. } => {
assert_eq!(requested, "ansi-true");
}
other => panic!("expected UnsupportedExportFormat, got {other:?}"),
}
}
}