use std::path::Path;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Format {
Org,
Latex,
Markdown,
Rst,
Plaintext,
}
impl Format {
pub fn from_path(path: &Path) -> Self {
match path.extension().and_then(|e| e.to_str()) {
Some("org") => Format::Org,
Some("tex" | "latex" | "ltx" | "sty" | "cls") => Format::Latex,
Some("md" | "markdown" | "mkd" | "mdx") => Format::Markdown,
Some("rst" | "rest") => Format::Rst,
_ => Format::Plaintext,
}
}
pub fn from_extension(ext: &str) -> Self {
match ext {
"org" => Format::Org,
"tex" | "latex" | "ltx" | "sty" | "cls" => Format::Latex,
"md" | "markdown" | "mkd" | "mdx" => Format::Markdown,
"rst" | "rest" => Format::Rst,
_ => Format::Plaintext,
}
}
pub fn config_key(self) -> &'static str {
match self {
Format::Org => "org",
Format::Latex => "latex",
Format::Markdown => "markdown",
Format::Rst => "rst",
Format::Plaintext => "plaintext",
}
}
#[cfg(feature = "cli")]
pub fn from_arg(arg: crate::cli::FormatArg) -> Self {
match arg {
crate::cli::FormatArg::Org => Format::Org,
crate::cli::FormatArg::Latex => Format::Latex,
crate::cli::FormatArg::Markdown => Format::Markdown,
crate::cli::FormatArg::Rst => Format::Rst,
crate::cli::FormatArg::Plaintext => Format::Plaintext,
}
}
}