1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//! Additional types that aren't present in the Haskell package but are useful.
use super::{Block, Inline};

/// A utility type to provide better error messages when
/// an array of blocks doesn't match an expected pattern.
///
/// For example:
///
/// ```
/// use pandoc_types::definition::{extra::BlockType, Attr, Block, Inline};
///
/// fn parse_as_para(blocks: &[Block]) -> Result<&Vec<Inline>, String> {
///     match blocks {
///         [Block::Para(inlines)] => Ok(inlines),
///         unexpected => {
///             return Err(format!(
///                 "expected [Para] but found {:?}",
///                 unexpected.iter().map(BlockType::from).collect::<Vec<_>>(),
///             ))
///         }
///     }
/// }
///
/// assert_eq!(
///     parse_as_para(&[Block::CodeBlock(
///         Attr::default(),
///         "some very long string".into()
///     )]),
///     Err("expected [Para] but found [CodeBlock]".into())
/// );
/// ```
///
/// Note that if we didn't use `BlockType` the error message would include
/// the contained data of the CodeBlock which isn't actually relevant.
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum BlockType {
    Plain,
    Para,
    LineBlock,
    CodeBlock,
    RawBlock,
    BlockQuote,
    OrderedList,
    BulletList,
    DefinitionList,
    Header,
    HorizontalRule,
    Table,
    Div,
    Null,
}

impl From<&Block> for BlockType {
    fn from(block: &Block) -> Self {
        match block {
            Block::Plain(_) => Self::Plain,
            Block::Para(_) => Self::Para,
            Block::LineBlock(_) => Self::LineBlock,
            Block::CodeBlock(_, _) => Self::CodeBlock,
            Block::RawBlock(_, _) => Self::RawBlock,
            Block::BlockQuote(_) => Self::BlockQuote,
            Block::OrderedList(_, _) => Self::OrderedList,
            Block::BulletList(_) => Self::BulletList,
            Block::DefinitionList(_) => Self::DefinitionList,
            Block::Header(_, _, _) => Self::Header,
            Block::HorizontalRule => Self::HorizontalRule,
            Block::Table(_) => Self::Table,
            Block::Div(_, _) => Self::Div,
            Block::Null => Self::Null,
        }
    }
}

/// A utility type to provide better error messages when
/// an array of inlines doesn't match an expected pattern.
///
/// For example:
///
/// ```
/// use pandoc_types::definition::{extra::InlineType, Attr, Block, Inline, Target};
///
/// fn parse_as_link(inlines: &[Inline]) -> Result<(&Vec<Inline>, &Target), String> {
///     match inlines {
///         [Inline::Link(_, label, target)] => Ok((label, target)),
///         unexpected => {
///             return Err(format!(
///                 "expected [Link] but found {:?}",
///                 unexpected.iter().map(InlineType::from).collect::<Vec<_>>(),
///             ))
///         }
///     }
/// }
///
/// assert_eq!(
///     parse_as_link(&[Inline::Underline(vec![Inline::Str(
///         "some very long string".into()
///     )],)]),
///     Err("expected [Link] but found [Underline]".into())
/// );
/// ```
///
/// Note that if we didn't use `InlineType` the error message would include
/// the contained text of the Underline which isn't actually relevant.
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum InlineType {
    Str,
    Emph,
    Underline,
    Strong,
    Strikeout,
    Superscript,
    Subscript,
    SmallCaps,
    Quoted,
    Cite,
    Code,
    Space,
    SoftBreak,
    LineBreak,
    Math,
    RawInline,
    Link,
    Image,
    Note,
    Span,
}

impl From<&Inline> for InlineType {
    fn from(inline: &Inline) -> Self {
        match inline {
            Inline::Str(_) => Self::Str,
            Inline::Emph(_) => Self::Emph,
            Inline::Underline(_) => Self::Underline,
            Inline::Strong(_) => Self::Strong,
            Inline::Strikeout(_) => Self::Strikeout,
            Inline::Superscript(_) => Self::Superscript,
            Inline::Subscript(_) => Self::Subscript,
            Inline::SmallCaps(_) => Self::SmallCaps,
            Inline::Quoted(_, _) => Self::Quoted,
            Inline::Cite(_, _) => Self::Cite,
            Inline::Code(_, _) => Self::Code,
            Inline::Space => Self::Space,
            Inline::SoftBreak => Self::SoftBreak,
            Inline::LineBreak => Self::LineBreak,
            Inline::Math(_, _) => Self::Math,
            Inline::RawInline(_, _) => Self::RawInline,
            Inline::Link(_, _, _) => Self::Link,
            Inline::Image(_, _, _) => Self::Image,
            Inline::Note(_) => Self::Note,
            Inline::Span(_, _) => Self::Span,
        }
    }
}