use crate::{Job, Result};
pub type ConvertFn = fn(&Job) -> Result<()>;
#[derive(Clone, Copy)]
pub struct Entry {
pub direct: Option<ConvertFn>,
pub intermediates: &'static [&'static str],
}
impl Entry {
const fn direct(f: ConvertFn) -> Self {
Self {
direct: Some(f),
intermediates: &[],
}
}
const fn chain(intermediates: &'static [&'static str]) -> Self {
Self {
direct: None,
intermediates,
}
}
}
use crate::converters::*;
const INPUT_IMAGES: &[&str] = &[
"bmp", "dds", "exr", "ff", "gif", "hdr", "ico", "jpeg", "png", "pnm", "qoi",
"tga", "tif", "webp",
];
const OUTPUT_IMAGES: &[&str] = &[
"avif", "bmp", "exr", "ff", "gif", "hdr", "ico", "jpeg", "png", "pnm", "qoi",
"tga", "tif", "webp",
];
fn is_image_cross(from: &str, to: &str) -> bool {
INPUT_IMAGES.contains(&from) && OUTPUT_IMAGES.contains(&to) && from != to
}
pub fn lookup(from: &str, to: &str) -> Option<Entry> {
if is_image_cross(from, to) {
return Some(Entry::direct(image::convert));
}
if INPUT_IMAGES.contains(&from) && to == "jp2" {
return Some(Entry::direct(image::to_jp2));
}
if from == "psd" && OUTPUT_IMAGES.contains(&to) {
return Some(Entry::direct(image::from_psd));
}
if from == "psd" && to == "jp2" {
return Some(Entry::direct(image::psd_to_jp2));
}
if from == "jp2" && OUTPUT_IMAGES.contains(&to) {
return Some(Entry::direct(image::from_jp2));
}
if (INPUT_IMAGES.contains(&from) || from == "psd" || from == "jp2")
&& to == "pdf"
{
return Some(Entry::direct(pdf_out::image_to_pdf));
}
if from == "heic" && (OUTPUT_IMAGES.contains(&to) || to == "jpg") {
return Some(Entry::direct(shell_img::heic_to_image));
}
Some(match (from, to) {
("abc", "ly") => Entry::direct(lilypond::abc_to_ly),
("abc", "midi") => Entry::chain(&["ly"]),
("abc", "pdf") => Entry::chain(&["ly"]),
("abc", "png") => Entry::chain(&["ly"]),
("abc", "ps") => Entry::chain(&["ly"]),
("abc", "svg") => Entry::chain(&["ly"]),
("abc", "jpeg") => Entry::chain(&["ly", "png"]),
("csv", "json") => Entry::direct(csv_tsv::csv_to_json),
("csv", "tsv") => Entry::direct(csv_tsv::csv_to_tsv),
("csv", "html") => Entry::direct(csv_tsv::csv_to_html),
("csv", "jsonl") => Entry::direct(jsonl::csv_to_jsonl),
("csv", "json5") => Entry::chain(&["json"]),
("csv", "yaml") => Entry::chain(&["json"]),
("csv", "toml") => Entry::chain(&["json"]),
("csv", "xml") => Entry::chain(&["json"]),
("cbor", "json") => Entry::direct(cbor::cbor_to_json),
("cbor", "json5") => Entry::chain(&["json"]),
("cbor", "yaml") => Entry::chain(&["json"]),
("cbor", "toml") => Entry::chain(&["json"]),
("cbor", "xml") => Entry::chain(&["json"]),
("epub", "html") => Entry::direct(document::epub_to_html),
("epub", "md") => Entry::direct(document::epub_to_md),
("epub", "txt") => Entry::chain(&["html"]),
("flac", "wav") => Entry::direct(audio::flac_to_wav),
("flac", "mp3") => Entry::chain(&["wav"]),
("flac", "ogg") => Entry::chain(&["wav"]),
("flac", "aac") => Entry::chain(&["wav"]),
("html", "md") => Entry::direct(html_md::to_md),
("html", "txt") => Entry::direct(html_md::html_to_txt),
("html", "pdf") => Entry::direct(pdf_out::html_to_pdf),
("ics", "json") => Entry::direct(ical_fmt::ics_to_json),
("ics", "json5") => Entry::chain(&["json"]),
("ics", "yaml") => Entry::chain(&["json"]),
("ini", "json") => Entry::direct(ini_fmt::ini_to_json),
("ini", "json5") => Entry::chain(&["json"]),
("ini", "yaml") => Entry::chain(&["json"]),
("ini", "toml") => Entry::chain(&["json"]),
("json", "json5") => Entry::direct(json_yaml::json_to_json5),
("json", "yaml") => Entry::direct(json_yaml::json_to_yaml),
("json", "toml") => Entry::direct(toml_fmt::json_to_toml),
("json", "xml") => Entry::direct(xml::json_to_xml),
("json", "ini") => Entry::direct(ini_fmt::json_to_ini),
("json", "jsonl") => Entry::direct(jsonl::json_to_jsonl),
("json", "cbor") => Entry::direct(cbor::json_to_cbor),
("json", "plist") => Entry::direct(plist_fmt::json_to_plist),
("json5", "json") => Entry::direct(json_yaml::json5_to_json),
("json5", "yaml") => Entry::chain(&["json"]),
("json5", "toml") => Entry::chain(&["json"]),
("json5", "xml") => Entry::chain(&["json"]),
("json5", "ini") => Entry::chain(&["json"]),
("json5", "jsonl") => Entry::chain(&["json"]),
("json5", "cbor") => Entry::chain(&["json"]),
("json5", "plist") => Entry::chain(&["json"]),
("jsonl", "json") => Entry::direct(jsonl::jsonl_to_json),
("jsonl", "csv") => Entry::direct(jsonl::jsonl_to_csv),
("jsonl", "tsv") => Entry::direct(jsonl::jsonl_to_tsv),
("jsonl", "json5") => Entry::chain(&["json"]),
("jsonl", "yaml") => Entry::chain(&["json"]),
("jsonl", "toml") => Entry::chain(&["json"]),
("jsonl", "xml") => Entry::chain(&["json"]),
("ly", "midi")
| ("ly", "pdf")
| ("ly", "png")
| ("ly", "ps")
| ("ly", "svg") => Entry::direct(lilypond::ly_to),
("md", "html") => Entry::direct(html_md::md_to_html),
("md", "txt") => Entry::direct(html_md::md_to_txt),
("md", "pdf") => Entry::chain(&["html"]),
("midi", "wav") => Entry::direct(audio::midi_to_wav),
("mp3", "wav") => Entry::direct(audio::mp3_to_wav),
("mp3", "flac") => Entry::chain(&["wav"]),
("mp3", "ogg") => Entry::chain(&["wav"]),
("mp3", "aac") => Entry::chain(&["wav"]),
("ods", "csv") => Entry::direct(spreadsheet::to_csv),
("ods", "tsv") => Entry::direct(spreadsheet::to_tsv),
("ods", "json") => Entry::direct(spreadsheet::to_json),
("ods", "json5") => Entry::chain(&["json"]),
("ods", "yaml") => Entry::chain(&["json"]),
("obj", "stl") => Entry::direct(stl::obj_to_stl),
("ogg", "wav") => Entry::direct(audio::ogg_to_wav),
("ogg", "mp3") => Entry::chain(&["wav"]),
("ogg", "flac") => Entry::chain(&["wav"]),
("ogg", "aac") => Entry::chain(&["wav"]),
("opus", "wav") => Entry::direct(audio::opus_to_wav),
("opus", "flac") => Entry::chain(&["wav"]),
("opus", "mp3") => Entry::chain(&["wav"]),
("opus", "ogg") => Entry::chain(&["wav"]),
("opus", "aac") => Entry::chain(&["wav"]),
("parquet", "csv") => Entry::direct(parquet_fmt::parquet_to_csv),
("parquet", "tsv") => Entry::direct(parquet_fmt::parquet_to_tsv),
("parquet", "json") => Entry::direct(parquet_fmt::parquet_to_json),
("parquet", "json5") => Entry::chain(&["json"]),
("parquet", "yaml") => Entry::chain(&["json"]),
("parquet", "jsonl") => Entry::chain(&["json"]),
("pdf", "txt") => Entry::direct(document::pdf_to_txt),
("pdf", "md") => Entry::chain(&["txt"]),
("pdf", "png") => Entry::direct(shell_img::pdf_to_png),
("plist", "json") => Entry::direct(plist_fmt::plist_to_json),
("plist", "yaml") => Entry::chain(&["json"]),
("plist", "json5") => Entry::chain(&["json"]),
("plist", "toml") => Entry::chain(&["json"]),
("plist", "xml") => Entry::chain(&["json"]),
("rtf", "txt") => Entry::direct(document::rtf_to_txt),
("rtf", "md") => Entry::direct(document::rtf_to_md),
("rtf", "html") => Entry::chain(&["md"]),
("scad", "csg") => Entry::direct(scad::to_csg),
("scad", "off") => Entry::direct(scad::to_off),
("scad", "png") => Entry::direct(scad::to_png),
("scad", "stl") => Entry::direct(scad::to_stl),
("stl", "obj") => Entry::direct(stl::stl_to_obj),
("stl", "png") => Entry::direct(stl::stl_to_png),
("svg", "png") => Entry::direct(svg::to_png),
("svg", "pdf") => Entry::direct(svg::to_pdf),
("toml", "json") => Entry::direct(toml_fmt::toml_to_json),
("toml", "json5") => Entry::chain(&["json"]),
("toml", "yaml") => Entry::chain(&["json"]),
("toml", "xml") => Entry::chain(&["json"]),
("typ", "pdf") | ("typ", "png") | ("typ", "svg") | ("typ", "html") => {
Entry::direct(typst_fmt::typ_to)
}
("tsv", "csv") => Entry::direct(csv_tsv::tsv_to_csv),
("tsv", "json") => Entry::direct(csv_tsv::tsv_to_json),
("tsv", "html") => Entry::direct(csv_tsv::tsv_to_html),
("tsv", "jsonl") => Entry::direct(jsonl::tsv_to_jsonl),
("tsv", "json5") => Entry::chain(&["json"]),
("tsv", "yaml") => Entry::chain(&["json"]),
("tsv", "toml") => Entry::chain(&["json"]),
("tsv", "xml") => Entry::chain(&["json"]),
("txt", "pdf") => Entry::direct(pdf_out::txt_to_pdf),
("txt", "md") => Entry::direct(html_md::txt_to_md),
("vcf", "json") => Entry::direct(vcard::vcf_to_json),
("vcf", "csv") => Entry::direct(vcard::vcf_to_csv),
("vcf", "tsv") => Entry::chain(&["csv"]),
("vcf", "json5") => Entry::chain(&["json"]),
("vcf", "yaml") => Entry::chain(&["json"]),
("wav", "aac") => Entry::direct(audio::wav_to_aac),
("wav", "flac") => Entry::direct(audio::wav_to_flac),
("wav", "mp3") => Entry::direct(audio::wav_to_mp3),
("wav", "ogg") => Entry::direct(audio::wav_to_ogg),
("xlsx", "csv") => Entry::direct(spreadsheet::to_csv),
("xlsx", "tsv") => Entry::direct(spreadsheet::to_tsv),
("xlsx", "json") => Entry::direct(spreadsheet::to_json),
("xlsx", "json5") => Entry::chain(&["json"]),
("xlsx", "yaml") => Entry::chain(&["json"]),
("xml", "json") => Entry::direct(xml::xml_to_json),
("xml", "json5") => Entry::chain(&["json"]),
("xml", "yaml") => Entry::chain(&["json"]),
("xml", "toml") => Entry::chain(&["json"]),
("yaml", "json") => Entry::direct(json_yaml::yaml_to_json),
("yaml", "json5") => Entry::chain(&["json"]),
("yaml", "toml") => Entry::chain(&["json"]),
("yaml", "xml") => Entry::chain(&["json"]),
("yaml", "ini") => Entry::chain(&["json"]),
("yaml", "jsonl") => Entry::chain(&["json"]),
("yaml", "cbor") => Entry::chain(&["json"]),
("yaml", "plist") => Entry::chain(&["json"]),
_ => return None,
})
}
pub fn list_all() -> Vec<(&'static str, &'static str)> {
let mut pairs: Vec<(&'static str, &'static str)> = Vec::new();
let image_inputs: Vec<&'static str> = INPUT_IMAGES
.iter()
.chain(std::iter::once(&"psd"))
.chain(std::iter::once(&"jp2"))
.copied()
.collect();
for from in &image_inputs {
for to in OUTPUT_IMAGES {
if from != to {
pairs.push((*from, *to));
}
}
if INPUT_IMAGES.contains(from) || *from == "psd" {
pairs.push((*from, "jp2"));
}
pairs.push((*from, "pdf"));
}
for to in OUTPUT_IMAGES {
pairs.push(("heic", *to));
}
const NON_IMAGE: &[(&str, &str)] = &[
("abc", "ly"),
("abc", "midi"),
("abc", "pdf"),
("abc", "png"),
("abc", "ps"),
("abc", "svg"),
("abc", "jpeg"),
("cbor", "json"),
("cbor", "json5"),
("cbor", "toml"),
("cbor", "xml"),
("cbor", "yaml"),
("csv", "html"),
("csv", "json"),
("csv", "json5"),
("csv", "jsonl"),
("csv", "toml"),
("csv", "tsv"),
("csv", "xml"),
("csv", "yaml"),
("epub", "html"),
("epub", "md"),
("epub", "txt"),
("flac", "aac"),
("flac", "mp3"),
("flac", "ogg"),
("flac", "wav"),
("html", "md"),
("html", "pdf"),
("html", "txt"),
("ics", "json"),
("ics", "json5"),
("ics", "yaml"),
("ini", "json"),
("ini", "json5"),
("ini", "toml"),
("ini", "yaml"),
("json", "cbor"),
("json", "ini"),
("json", "json5"),
("json", "jsonl"),
("json", "plist"),
("json", "toml"),
("json", "xml"),
("json", "yaml"),
("json5", "cbor"),
("json5", "ini"),
("json5", "json"),
("json5", "jsonl"),
("json5", "plist"),
("json5", "toml"),
("json5", "xml"),
("json5", "yaml"),
("jsonl", "csv"),
("jsonl", "json"),
("jsonl", "json5"),
("jsonl", "toml"),
("jsonl", "tsv"),
("jsonl", "xml"),
("jsonl", "yaml"),
("ly", "midi"),
("ly", "pdf"),
("ly", "png"),
("ly", "ps"),
("ly", "svg"),
("md", "html"),
("md", "pdf"),
("md", "txt"),
("midi", "wav"),
("mp3", "aac"),
("mp3", "flac"),
("mp3", "ogg"),
("mp3", "wav"),
("obj", "stl"),
("ods", "csv"),
("ods", "json"),
("ods", "json5"),
("ods", "tsv"),
("ods", "yaml"),
("ogg", "aac"),
("ogg", "flac"),
("ogg", "mp3"),
("ogg", "wav"),
("opus", "aac"),
("opus", "flac"),
("opus", "mp3"),
("opus", "ogg"),
("opus", "wav"),
("parquet", "csv"),
("parquet", "json"),
("parquet", "json5"),
("parquet", "jsonl"),
("parquet", "tsv"),
("parquet", "yaml"),
("pdf", "md"),
("pdf", "png"),
("pdf", "txt"),
("plist", "json"),
("plist", "json5"),
("plist", "toml"),
("plist", "xml"),
("plist", "yaml"),
("rtf", "html"),
("rtf", "md"),
("rtf", "txt"),
("scad", "csg"),
("scad", "off"),
("scad", "png"),
("scad", "stl"),
("stl", "obj"),
("stl", "png"),
("svg", "pdf"),
("svg", "png"),
("toml", "json"),
("toml", "json5"),
("toml", "xml"),
("toml", "yaml"),
("tsv", "csv"),
("tsv", "html"),
("tsv", "json"),
("tsv", "json5"),
("tsv", "jsonl"),
("tsv", "toml"),
("tsv", "xml"),
("tsv", "yaml"),
("txt", "md"),
("txt", "pdf"),
("typ", "html"),
("typ", "pdf"),
("typ", "png"),
("typ", "svg"),
("vcf", "csv"),
("vcf", "json"),
("vcf", "json5"),
("vcf", "tsv"),
("vcf", "yaml"),
("wav", "aac"),
("wav", "flac"),
("wav", "mp3"),
("wav", "ogg"),
("xlsx", "csv"),
("xlsx", "json"),
("xlsx", "json5"),
("xlsx", "tsv"),
("xlsx", "yaml"),
("xml", "json"),
("xml", "json5"),
("xml", "toml"),
("xml", "yaml"),
("yaml", "cbor"),
("yaml", "ini"),
("yaml", "json"),
("yaml", "json5"),
("yaml", "jsonl"),
("yaml", "plist"),
("yaml", "toml"),
("yaml", "xml"),
];
pairs.extend_from_slice(NON_IMAGE);
pairs.sort();
pairs.dedup();
pairs
}