parse_wiki_text_2/
warning.rs

1// Copyright 2019 Fredrik Portström <https://portstrom.com>
2// This is free software distributed under the terms specified in
3// the file LICENSE at the top-level directory of this distribution.
4
5use std::fmt;
6
7/// Warning from the parser telling that something is not well-formed.
8#[derive(Debug)]
9pub struct Warning {
10	/// The byte position in the wiki text where the warning ends.
11	pub end: usize,
12
13	/// An identifier for the kind of warning.
14	pub message: WarningMessage,
15
16	/// The byte position in the wiki text where the warning starts.
17	pub start: usize,
18}
19
20/// Identifier for a kind of warning from the parser.
21#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
22pub enum WarningMessage {
23	/// List broken by definition term.
24	DefinitionTermContinuation,
25
26	/// End tag in comment.
27	EndTagInComment,
28
29	/// Invalid character.
30	InvalidCharacter,
31
32	/// Invalid heading syntax. Rewinding.
33	InvalidHeadingSyntaxRewinding,
34
35	/// Invalid link syntax.
36	InvalidLinkSyntax,
37
38	/// Invalid parameter syntax.
39	InvalidParameterSyntax,
40
41	/// Invalid tag syntax.
42	InvalidTagSyntax,
43
44	/// Missing end tag. Rewinding.
45	MissingEndTagRewinding,
46
47	/// Repeated empty line.
48	RepeatedEmptyLine,
49
50	/// Stray text in table.
51	StrayTextInTable,
52
53	/// Wiki text comes after a redirect.
54	TextAfterRedirect,
55
56	/// The end tag does not match the last start tag. Rewinding.
57	UnexpectedEndTagRewinding,
58
59	/// An end tag was found with no preceeding start tag.
60	UnexpectedEndTag,
61
62	/// Expected heading of higher level. Correcting start of heading.
63	UnexpectedHeadingLevelCorrecting,
64
65	/// A tag with an unrecognized tag name was found.
66	UnrecognizedTagName,
67
68	/// Useless text in parameter.
69	UselessTextInParameter,
70
71	/// Useless text in redirect.
72	UselessTextInRedirect,
73}
74
75impl WarningMessage {
76	/// Human-readable description of the warning.
77	pub fn message(self) -> &'static str {
78		match self {
79			WarningMessage::DefinitionTermContinuation => {
80				"List broken by definition term."
81			}
82			WarningMessage::EndTagInComment => "End tag in comment.",
83			WarningMessage::InvalidCharacter => "Invalid character.",
84			WarningMessage::InvalidHeadingSyntaxRewinding => {
85				"Invalid heading syntax. Rewinding."
86			}
87			WarningMessage::InvalidLinkSyntax => "Invalid link syntax.",
88			WarningMessage::InvalidParameterSyntax => {
89				"Invalid parameter syntax."
90			}
91			WarningMessage::InvalidTagSyntax => "Invalid tag syntax.",
92			WarningMessage::MissingEndTagRewinding => {
93				"Missing end tag. Rewinding."
94			}
95			WarningMessage::RepeatedEmptyLine => "Repeated empty line.",
96			WarningMessage::StrayTextInTable => "Stray text in table.",
97			WarningMessage::TextAfterRedirect => {
98				"Wiki text comes after a redirect."
99			}
100			WarningMessage::UnexpectedEndTagRewinding => {
101				"The end tag does not match the last start tag. Rewinding."
102			}
103			WarningMessage::UnexpectedEndTag => {
104				"An end tag was found with no preceeding start tag."
105			}
106			WarningMessage::UnexpectedHeadingLevelCorrecting => {
107				"Expected heading of higher level. Correcting start of heading."
108			}
109			WarningMessage::UnrecognizedTagName => {
110				"A tag with an unrecognized tag name was found."
111			}
112			WarningMessage::UselessTextInParameter => {
113				"Useless text in parameter."
114			}
115			WarningMessage::UselessTextInRedirect => {
116				"Useless text in redirect."
117			}
118		}
119	}
120}
121
122impl fmt::Display for WarningMessage {
123	fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
124		formatter.write_str(self.message())
125	}
126}