robotparser_fork/parser/
parse_result.rs

1use crate::parser::warning::ParseWarning;
2use std::fmt::Debug;
3
4#[derive(Debug)]
5/// The result of the robots.txt parser.
6pub struct ParseResult<R>
7where
8    R: Debug,
9{
10    result: R,
11    warnings: Vec<ParseWarning>,
12}
13
14impl<R> ParseResult<R>
15where
16    R: Debug,
17{
18    /// Creates a new structure for parser results.
19    pub(crate) fn new(result: R) -> ParseResult<R> {
20        ParseResult {
21            result,
22            warnings: Vec::new(),
23        }
24    }
25
26    /// Creates a new structure for parser results with warnings.
27    pub(crate) fn new_with_warnings(result: R, warnings: Vec<ParseWarning>) -> ParseResult<R> {
28        ParseResult { result, warnings }
29    }
30
31    /// Returns the result of the robots.txt parser.
32    pub fn get_result(self) -> R {
33        self.result
34    }
35
36    /// Returns the robots.txt parser warning array.
37    pub fn get_warnings(&self) -> &[ParseWarning] {
38        self.warnings.as_slice()
39    }
40
41    /// Returns reference to result of the robots.txt parser or first warning.
42    pub fn ok_ref(&self) -> Result<&R, &ParseWarning> {
43        if let Some(warning) = self.warnings.first() {
44            return Err(warning);
45        }
46        Ok(&self.result)
47    }
48
49    /// Returns the result of the robots.txt parser or first warning.
50    pub fn ok(mut self) -> Result<R, ParseWarning> {
51        if self.warnings.is_empty() {
52            return Ok(self.result);
53        }
54        let first_warning = self.warnings.remove(0);
55        Err(first_warning)
56    }
57
58    /// Converts this structure into another type of structure.
59    pub(crate) fn map<T>(self, callback: impl Fn(R) -> T) -> ParseResult<T>
60    where
61        T: Debug,
62    {
63        ParseResult {
64            result: (callback)(self.result),
65            warnings: self.warnings,
66        }
67    }
68}