Skip to main content

snapper_fmt/
format.rs

1//! Document format detection and representation.
2//!
3//! The [`Format`] enum identifies the markup language of a document,
4//! enabling format-specific parsing in the pipeline. Format is detected
5//! from file extensions or can be specified explicitly via CLI flags.
6
7use std::path::Path;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum Format {
11    Org,
12    Latex,
13    Markdown,
14    Rst,
15    Plaintext,
16}
17
18impl Format {
19    /// Detect format from file extension, defaulting to Plaintext.
20    pub fn from_path(path: &Path) -> Self {
21        match path.extension().and_then(|e| e.to_str()) {
22            Some("org") => Format::Org,
23            Some("tex" | "latex" | "ltx" | "sty" | "cls") => Format::Latex,
24            Some("md" | "markdown" | "mkd" | "mdx") => Format::Markdown,
25            Some("rst" | "rest") => Format::Rst,
26            _ => Format::Plaintext,
27        }
28    }
29
30    /// Detect format from a bare file extension string (without the dot).
31    pub fn from_extension(ext: &str) -> Self {
32        match ext {
33            "org" => Format::Org,
34            "tex" | "latex" | "ltx" | "sty" | "cls" => Format::Latex,
35            "md" | "markdown" | "mkd" | "mdx" => Format::Markdown,
36            "rst" | "rest" => Format::Rst,
37            _ => Format::Plaintext,
38        }
39    }
40
41    pub fn config_key(self) -> &'static str {
42        match self {
43            Format::Org => "org",
44            Format::Latex => "latex",
45            Format::Markdown => "markdown",
46            Format::Rst => "rst",
47            Format::Plaintext => "plaintext",
48        }
49    }
50
51    #[cfg(feature = "cli")]
52    pub fn from_arg(arg: crate::cli::FormatArg) -> Self {
53        match arg {
54            crate::cli::FormatArg::Org => Format::Org,
55            crate::cli::FormatArg::Latex => Format::Latex,
56            crate::cli::FormatArg::Markdown => Format::Markdown,
57            crate::cli::FormatArg::Rst => Format::Rst,
58            crate::cli::FormatArg::Plaintext => Format::Plaintext,
59        }
60    }
61}