use super::ParseError;
use std::borrow::{Borrow, BorrowMut};
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct ParseOutcome<T> {
value: T,
errors: Vec<ParseError>,
}
impl<T> ParseOutcome<T> {
#[inline]
pub fn new<I>(value: T, errors: I) -> Self
where
I: Into<Vec<ParseError>>,
{
ParseOutcome {
value,
errors: errors.into(),
}
}
#[inline]
pub fn value(&self) -> &T {
&self.value
}
#[inline]
pub fn errors(&self) -> &[ParseError] {
&self.errors
}
}
impl<U> ParseOutcome<Vec<U>> {
#[inline]
pub fn push(&mut self, item: U) {
self.value.push(item);
}
}
impl<T> Clone for ParseOutcome<T>
where
T: Clone,
{
#[inline]
fn clone(&self) -> Self {
ParseOutcome {
value: self.value.clone(),
errors: self.errors.clone(),
}
}
}
impl<T> Default for ParseOutcome<T>
where
T: Default,
{
#[inline]
fn default() -> Self {
ParseOutcome {
value: T::default(),
errors: Vec::new(),
}
}
}
impl<T> Borrow<T> for ParseOutcome<T> {
#[inline]
fn borrow(&self) -> &T {
&self.value
}
}
impl<T> BorrowMut<T> for ParseOutcome<T> {
#[inline]
fn borrow_mut(&mut self) -> &mut T {
&mut self.value
}
}
impl<T> From<ParseOutcome<T>> for (T, Vec<ParseError>) {
#[inline]
fn from(outcome: ParseOutcome<T>) -> (T, Vec<ParseError>) {
let ParseOutcome { value, errors } = outcome;
(value, errors)
}
}
#[test]
fn outcome() {
let mut outcome = ParseOutcome::new(vec!['a'], vec![]);
assert_eq!(outcome.value(), &['a']);
assert_eq!(outcome.errors(), &[]);
outcome.push('b');
assert_eq!(outcome.value(), &['a', 'b']);
assert_eq!(outcome.errors(), &[]);
let outcome_2 = outcome.clone();
assert_eq!(outcome, outcome_2);
}
#[test]
fn default() {
let mut outcome: ParseOutcome<Option<i32>> = ParseOutcome::default();
{
let value: &Option<i32> = outcome.borrow();
assert_eq!(value, &None);
}
*outcome.borrow_mut() = Some(10);
}