Skip to main content

asciidork_opts/
lib.rs

1use std::fmt::{self, Display, Formatter};
2use std::str::FromStr;
3
4#[derive(Debug, Default, Clone, Copy)]
5pub struct Opts {
6  pub doc_type: DocType,
7  pub attribute_missing: AttributeMissing,
8  pub strict: bool,
9}
10
11#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
12pub enum AttributeMissing {
13  Warn,
14  Drop,
15  #[default]
16  Skip,
17  // dr. also has "drop-line", i'd rather not support it
18}
19
20#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
21pub enum DocType {
22  #[default]
23  Article,
24  Book,
25  Manpage,
26  Inline,
27}
28
29impl DocType {
30  pub const fn to_str(&self) -> &'static str {
31    match self {
32      Self::Article => "article",
33      Self::Book => "book",
34      Self::Manpage => "manpage",
35      Self::Inline => "inline",
36    }
37  }
38}
39
40impl Display for DocType {
41  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
42    write!(f, "{}", self.to_str())
43  }
44}
45
46impl FromStr for DocType {
47  type Err = &'static str;
48  fn from_str(s: &str) -> Result<Self, Self::Err> {
49    Ok(match s {
50      "article" => Self::Article,
51      "book" => Self::Book,
52      "manpage" => Self::Manpage,
53      "inline" => Self::Inline,
54      _ => return Err("Invalid doc type"),
55    })
56  }
57}
58
59impl DocType {
60  // https://docs.asciidoctor.org/asciidoc/latest/sections/styles/
61  pub fn supports_special_section(&self, name: &str) -> bool {
62    matches!(
63      (self, name),
64      (
65        DocType::Article,
66        "abstract" | "appendix" | "glossary" | "bibliography" | "index"
67      ) | (
68        DocType::Book,
69        "abstract"
70          | "colophon"
71          | "dedication"
72          | "acknowledgments"
73          | "preface"
74          | "partintro"
75          | "appendix"
76          | "glossary"
77          | "bibliography"
78          | "index",
79      )
80    )
81  }
82}
83
84impl Opts {
85  pub fn embedded() -> Self {
86    Self {
87      doc_type: DocType::Inline,
88      ..Self::default()
89    }
90  }
91}