use crate::parser::warning::ParseWarning;
use std::fmt::Debug;
#[derive(Debug)]
pub struct ParseResult<R>
where
R: Debug,
{
result: R,
warnings: Vec<ParseWarning>,
}
impl<R> ParseResult<R>
where
R: Debug,
{
pub(crate) fn new(result: R) -> ParseResult<R> {
ParseResult {
result,
warnings: Vec::new(),
}
}
pub(crate) fn new_with_warnings(result: R, warnings: Vec<ParseWarning>) -> ParseResult<R> {
ParseResult { result, warnings }
}
pub fn get_result(self) -> R {
self.result
}
pub fn get_warnings(&self) -> &[ParseWarning] {
self.warnings.as_slice()
}
pub fn ok_ref(&self) -> Result<&R, &ParseWarning> {
if let Some(warning) = self.warnings.first() {
return Err(warning);
}
Ok(&self.result)
}
pub fn ok(mut self) -> Result<R, ParseWarning> {
if self.warnings.is_empty() {
return Ok(self.result);
}
let first_warning = self.warnings.remove(0);
Err(first_warning)
}
pub(crate) fn map<T>(self, callback: impl Fn(R) -> T) -> ParseResult<T>
where
T: Debug,
{
ParseResult {
result: (callback)(self.result),
warnings: self.warnings,
}
}
}