robotparser_fork/parser/
warning.rs

1use super::line::Line;
2use super::warning_reason::WarningReason;
3use std::error::Error;
4use std::fmt;
5use std::num::{ParseFloatError, ParseIntError};
6use url::ParseError as ParseUrlError;
7
8#[derive(Clone, Debug)]
9/// Warning of robots.txt parser about problems when parsing robots.txt file.
10pub struct ParseWarning {
11    line_no: usize,
12    line: String,
13    reason: WarningReason,
14}
15
16impl Error for ParseWarning {}
17
18impl ParseWarning {
19    /// Returns the line number in the text of the robots.txt file.
20    pub fn get_line_no(&self) -> usize {
21        self.line_no
22    }
23
24    /// Returns the text of the robots.txt file string.
25    pub fn get_line_text(&self) -> &String {
26        &self.line
27    }
28
29    /// Returns the reason of warning.
30    pub fn get_reason(&self) -> &WarningReason {
31        &self.reason
32    }
33
34    pub(crate) fn invalid_directive_format(line: &Line) -> ParseWarning {
35        ParseWarning {
36            line_no: line.get_line_number(),
37            line: line.get_line_text().into(),
38            reason: WarningReason::InvalidDirectiveFormat,
39        }
40    }
41
42    pub(crate) fn directive_key_is_empty(line: &Line) -> ParseWarning {
43        ParseWarning {
44            line_no: line.get_line_number(),
45            line: line.get_line_text().into(),
46            reason: WarningReason::DirectiveKeyIsEmpty,
47        }
48    }
49
50    pub(crate) fn unsupported_directive_key(line: &Line, key: String) -> ParseWarning {
51        ParseWarning {
52            line_no: line.get_line_number(),
53            line: line.get_line_text().into(),
54            reason: WarningReason::UnsupportedDirectiveKey(key),
55        }
56    }
57
58    pub(crate) fn user_agent_cannot_be_empty(line: &Line) -> ParseWarning {
59        ParseWarning {
60            line_no: line.get_line_number(),
61            line: line.get_line_text().into(),
62            reason: WarningReason::UserAgentCannotBeEmpty,
63        }
64    }
65
66    pub(crate) fn wrong_path_format(line: &Line) -> ParseWarning {
67        ParseWarning {
68            line_no: line.get_line_number(),
69            line: line.get_line_text().into(),
70            reason: WarningReason::WrongPathFormat,
71        }
72    }
73
74    pub(crate) fn directive_without_user_agent(line: &Line) -> ParseWarning {
75        ParseWarning {
76            line_no: line.get_line_number(),
77            line: line.get_line_text().into(),
78            reason: WarningReason::DirectiveWithoutUserAgent,
79        }
80    }
81
82    pub(crate) fn parse_crawl_delay_error(line: &Line, error: ParseFloatError) -> ParseWarning {
83        ParseWarning {
84            line_no: line.get_line_number(),
85            line: line.get_line_text().into(),
86            reason: WarningReason::ParseCrawlDelayError(error),
87        }
88    }
89
90    pub(crate) fn wrong_request_rate_format(line: &Line) -> ParseWarning {
91        ParseWarning {
92            line_no: line.get_line_number(),
93            line: line.get_line_text().into(),
94            reason: WarningReason::WrongRequestRateFormat,
95        }
96    }
97
98    pub(crate) fn parse_request_rate(line: &Line, error: ParseIntError) -> ParseWarning {
99        ParseWarning {
100            line_no: line.get_line_number(),
101            line: line.get_line_text().into(),
102            reason: WarningReason::ParseRequestRate(error),
103        }
104    }
105
106    pub(crate) fn parse_url(line: &Line, error: ParseUrlError) -> ParseWarning {
107        ParseWarning {
108            line_no: line.get_line_number(),
109            line: line.get_line_text().into(),
110            reason: WarningReason::ParseUrl(error),
111        }
112    }
113
114    pub(crate) fn wrong_clean_param_format(line: &Line) -> ParseWarning {
115        ParseWarning {
116            line_no: line.get_line_number(),
117            line: line.get_line_text().into(),
118            reason: WarningReason::WrongCleanParamFormat,
119        }
120    }
121
122    pub(crate) fn ignored_clean_params(line: &Line, ignored_clean_params: Vec<String>) -> ParseWarning {
123        ParseWarning {
124            line_no: line.get_line_number(),
125            line: line.get_line_text().into(),
126            reason: WarningReason::IgnoredCleanParams(ignored_clean_params),
127        }
128    }
129}
130
131/// Displays text of warning.
132impl fmt::Display for ParseWarning {
133    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
134        write!(f, "Line: {}. Text: `{}`. {}", self.line_no, self.line, self.reason)
135    }
136}