robotparser_fork/parser/
parse_result.rs1use crate::parser::warning::ParseWarning;
2use std::fmt::Debug;
3
4#[derive(Debug)]
5pub 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 pub(crate) fn new(result: R) -> ParseResult<R> {
20 ParseResult {
21 result,
22 warnings: Vec::new(),
23 }
24 }
25
26 pub(crate) fn new_with_warnings(result: R, warnings: Vec<ParseWarning>) -> ParseResult<R> {
28 ParseResult { result, warnings }
29 }
30
31 pub fn get_result(self) -> R {
33 self.result
34 }
35
36 pub fn get_warnings(&self) -> &[ParseWarning] {
38 self.warnings.as_slice()
39 }
40
41 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 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 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}