robotparser_fork/parser/
warning_reason.rs

1use std::fmt;
2use std::num::{ParseFloatError, ParseIntError};
3use url::ParseError as ParseUrlError;
4
5#[derive(Clone, Debug)]
6/// Warning reason of robots.txt parser about problems when parsing robots.txt file.
7pub enum WarningReason {
8    /// Invalid directive format. Invalid directive example: `:`
9    InvalidDirectiveFormat,
10    /// Directive key is empty. Invalid directive example: `: <Value>`
11    DirectiveKeyIsEmpty,
12    /// Directive key is not suppored by this parser.
13    UnsupportedDirectiveKey(String),
14    /// Passed directive key is `User-Agent` and passed value is empty.
15    UserAgentCannotBeEmpty,
16    /// It is impossible to process this directive before the `User-Agent` directive has not been processed.
17    DirectiveWithoutUserAgent,
18    /// It is impossible to process the `Crawl-Delay` directive because of an error when parsing a floating point number.
19    ParseCrawlDelayError(ParseFloatError),
20    /// Incorrect format of the `Request-Rate` directive. Example of the correct format: `Request-rate: 1/5`
21    WrongRequestRateFormat,
22    /// Incorrect format of the `Request-Rate` directive. Example of the correct format: `Request-rate: 1/5`
23    ParseRequestRate(ParseIntError),
24    /// Parsing URL error.
25    ParseUrl(ParseUrlError),
26    /// Incorrect format of the `Clean-Param` directive.
27    /// Parameters must be matched to regular expression: `A-Za-z0-9.-_`.
28    /// Example of the correct format: `Clean-param: ref1&ref2 /some_dir/get_book.pl`
29    WrongCleanParamFormat,
30    /// Some parameters of the `Clean-Param` directive has wrong symbols.
31    /// Parameters must be matched to regular expression: `A-Za-z0-9.-_`.
32    /// Example of the correct format: `Clean-param: ref1&ref2 /some_dir/get_book.pl`
33    IgnoredCleanParams(Vec<String>),
34    /// Error in URL path format.
35    WrongPathFormat,
36}
37
38/// Displays text of warning reason.
39impl fmt::Display for WarningReason {
40    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
41        match self {
42            Self::InvalidDirectiveFormat => {
43                write!(f, "Invalid directive format.")
44            },
45            Self::DirectiveKeyIsEmpty => {
46                write!(f, "Directive key is empty.")
47            },
48            Self::UnsupportedDirectiveKey(key) => {
49                write!(f, "Directive key `{}` is not suppored by this parser.", key)
50            },
51            Self::UserAgentCannotBeEmpty => {
52                write!(f, "Passed directive key is `User-Agent` and passed value is empty.")
53            },
54            Self::DirectiveWithoutUserAgent => {
55                write!(f, "It is impossible to process this directive before `User-Agent` directive has not been processed.")
56            },
57            Self::ParseCrawlDelayError(err) => {
58                write!(f, "It is impossible to process the `Crawl-Delay` directive because of an error when parsing a floating point number: {}", err)
59            },
60            Self::WrongRequestRateFormat => {
61                write!(f, "Incorrect format of the `Request-Rate` directive")
62            },
63            Self::ParseRequestRate(err) => {
64                write!(f, "Incorrect format of the `Request-Rate` directive: {}", err)
65            },
66            Self::ParseUrl(err) => {
67                write!(f, "Parsing URL error: {}", err)
68            },
69            Self::WrongCleanParamFormat => {
70                write!(f, "Incorrect format of the `Clean-Param` directive.")
71            },
72            Self::IgnoredCleanParams(ref params) => {
73                write!(f, "Directive `Clean-Param` directive has incorrect parameters: {:?}", params)
74            },
75            Self::WrongPathFormat => {
76                write!(f, "Error in URL path format.")
77            },
78        }
79    }
80}