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
// Copyright 2019 Fredrik Portström <https://portstrom.com>
// This is free software distributed under the terms specified in
// the file LICENSE at the top-level directory of this distribution.

use std::fmt;

/// Warning from the parser telling that something is not well-formed.
#[derive(Debug)]
pub struct Warning {
    /// The byte position in the wiki text where the warning ends.
    pub end: usize,

    /// An identifier for the kind of warning.
    pub message: WarningMessage,

    /// The byte position in the wiki text where the warning starts.
    pub start: usize,
}

/// Identifier for a kind of warning from the parser.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum WarningMessage {
    /// List broken by definition term.
    DefinitionTermContinuation,

    /// End tag in comment.
    EndTagInComment,

    /// Invalid character.
    InvalidCharacter,

    /// Invalid heading syntax. Rewinding.
    InvalidHeadingSyntaxRewinding,

    /// Invalid link syntax.
    InvalidLinkSyntax,

    /// Invalid parameter syntax.
    InvalidParameterSyntax,

    /// Invalid tag syntax.
    InvalidTagSyntax,

    /// Missing end tag. Rewinding.
    MissingEndTagRewinding,

    /// Repeated empty line.
    RepeatedEmptyLine,

    /// Stray text in table.
    StrayTextInTable,

    /// Wiki text comes after a redirect.
    TextAfterRedirect,

    /// The end tag does not match the last start tag. Rewinding.
    UnexpectedEndTagRewinding,

    /// An end tag was found with no preceeding start tag.
    UnexpectedEndTag,

    /// Expected heading of higher level. Correcting start of heading.
    UnexpectedHeadingLevelCorrecting,

    /// A tag with an unrecognized tag name was found.
    UnrecognizedTagName,

    /// Useless text in parameter.
    UselessTextInParameter,

    /// Useless text in redirect.
    UselessTextInRedirect,
}

impl WarningMessage {
    /// Human-readable description of the warning.
    pub fn message(self) -> &'static str {
        match self {
            WarningMessage::DefinitionTermContinuation => "List broken by definition term.",
            WarningMessage::EndTagInComment => "End tag in comment.",
            WarningMessage::InvalidCharacter => "Invalid character.",
            WarningMessage::InvalidHeadingSyntaxRewinding => "Invalid heading syntax. Rewinding.",
            WarningMessage::InvalidLinkSyntax => "Invalid link syntax.",
            WarningMessage::InvalidParameterSyntax => "Invalid parameter syntax.",
            WarningMessage::InvalidTagSyntax => "Invalid tag syntax.",
            WarningMessage::MissingEndTagRewinding => "Missing end tag. Rewinding.",
            WarningMessage::RepeatedEmptyLine => "Repeated empty line.",
            WarningMessage::StrayTextInTable => "Stray text in table.",
            WarningMessage::TextAfterRedirect => "Wiki text comes after a redirect.",
            WarningMessage::UnexpectedEndTagRewinding => {
                "The end tag does not match the last start tag. Rewinding."
            }
            WarningMessage::UnexpectedEndTag => {
                "An end tag was found with no preceeding start tag."
            }
            WarningMessage::UnexpectedHeadingLevelCorrecting => {
                "Expected heading of higher level. Correcting start of heading."
            }
            WarningMessage::UnrecognizedTagName => "A tag with an unrecognized tag name was found.",
            WarningMessage::UselessTextInParameter => "Useless text in parameter.",
            WarningMessage::UselessTextInRedirect => "Useless text in redirect.",
        }
    }
}

impl fmt::Display for WarningMessage {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str(self.message())
    }
}