retex-kpath 0.9.0

kpathsea-compatible file lookup for retex
Documentation
//! File formats known to kpathsea and their search parameters.

/// A kind of file to look up, mirroring kpathsea's `kpse_file_format_type`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FileFormat {
    /// TeX input files (`.tex`, and anything `\input` can read).
    Tex,
    /// Lua modules (`require` from LuaTeX).
    Lua,
    /// Format files (`.fmt`).
    Fmt,
    /// TeX font metrics (`.tfm`).
    Tfm,
    /// OpenType fonts (`.otf`).
    OpenType,
    /// TrueType fonts and collections (`.ttf`, `.ttc`).
    TrueType,
    /// Type 1 fonts (`.pfb`, `.pfa`).
    Type1,
    /// Encoding vectors (`.enc`).
    Enc,
    /// Font map files (`.map`).
    Map,
    /// Virtual fonts (`.vf`).
    Vf,
    /// BibTeX databases (`.bib`).
    Bib,
    /// BibTeX styles (`.bst`).
    Bst,
    /// Graphics and other files read by the backend (`TEXPICTS`).
    Pict,
    /// Files opened with `kpse.find_file(name, "other text files")`.
    Text,
}

/// Search parameters for a [`FileFormat`].
#[derive(Debug, Clone, Copy)]
pub struct FormatInfo {
    /// Variables holding the search path, in order of precedence.
    pub env_vars: &'static [&'static str],
    /// The path to use when no variable is set.
    pub default_path: &'static str,
    /// Standard suffixes, tried before the bare name.
    pub suffixes: &'static [&'static str],
}

impl FileFormat {
    /// The search parameters for this format.
    pub fn info(self) -> FormatInfo {
        let (env_vars, suffixes): (&[&str], &[&str]) = match self {
            FileFormat::Tex => (&["TEXINPUTS"], &[".tex"]),
            FileFormat::Lua => (
                &["LUAINPUTS"],
                &[
                    ".lua", ".luatex", ".luc", ".luctex", ".texlua", ".texluc", ".tlu",
                ],
            ),
            FileFormat::Fmt => (&["TEXFORMATS"], &[".fmt"]),
            FileFormat::Tfm => (&["TFMFONTS"], &[".tfm"]),
            FileFormat::OpenType => (&["OPENTYPEFONTS"], &[".otf", ".OTF"]),
            FileFormat::TrueType => (&["TTFONTS"], &[".ttf", ".ttc", ".TTF", ".TTC", ".dfont"]),
            FileFormat::Type1 => (&["T1FONTS", "T1INPUTS"], &[".pfa", ".pfb"]),
            FileFormat::Enc => (&["ENCFONTS"], &[".enc"]),
            FileFormat::Map => (&["TEXFONTMAPS"], &[".map"]),
            FileFormat::Vf => (&["VFFONTS"], &[".vf"]),
            FileFormat::Bib => (&["BIBINPUTS"], &[".bib"]),
            FileFormat::Bst => (&["BSTINPUTS"], &[".bst"]),
            FileFormat::Pict => (&["TEXPICTS", "TEXINPUTS"], &[]),
            FileFormat::Text => (&["TEXINPUTS"], &[]),
        };
        FormatInfo {
            env_vars,
            default_path: ".",
            suffixes,
        }
    }
}