corewars_parser/
result.rs

1//! Error handling for the corewars parser.
2//! [`Result`](Result) matches the `std::result::Result` type, except that it
3//! may also contain warnings alongside either an `Ok` or `Err` type.
4
5use super::error::{Error, Warning};
6
7use std::result::Result as StdResult;
8
9/// `Result` mimics the `std::result::Result` type, but each variant also carries
10/// zero or more [`Warning`](Warning)s with it.
11#[must_use = "this `Result` may be an `Err` variant, which should be handled"]
12pub enum Result<T> {
13    /// Contains the success value and zero or more warnings
14    Ok(T, Vec<Warning>),
15
16    /// Contains the error value and zero or more warnings
17    Err(Error, Vec<Warning>),
18}
19
20impl<T> Result<T> {
21    /// Create an `Ok` variant from a value.
22    pub fn ok(value: T) -> Self {
23        Self::Ok(value, Vec::new())
24    }
25
26    /// Create an `Err` variant from an error.
27    pub fn err(err: Error) -> Self {
28        Self::Err(err, Vec::new())
29    }
30}
31
32impl<T> From<StdResult<T, Error>> for Result<T> {
33    fn from(result: StdResult<T, Error>) -> Self {
34        match result {
35            Ok(value) => Self::Ok(value, Vec::new()),
36            Err(err) => Self::Err(err, Vec::new()),
37        }
38    }
39}
40
41impl<T> From<Error> for Result<T> {
42    fn from(err: Error) -> Self {
43        Self::Err(err, Vec::new())
44    }
45}
46
47// TODO some more impls. Probably some kind of deref coercion to make it more ergo