1use 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 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 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}