I am using the thiserror crate to create a custom error type
use thiserror::Error;
/// ParseTreeError enumerates all possible errors returned by this library.
#[derive(Error, Debug)]
pub enum ParseTreeError {
/// Represents an empty source. For example, an empty text file being given
/// as input to `count_words()`.
#[error("Source contains no data")]
EmptySource,
/// Represents a failure to read from input.
#[error("Read error")]
ReadError,
}
pub struct Parser <'a, T>
where
T: FromStr + PartialEq
{
token_stream: &'a str,
marker: (usize, usize),
}
impl<'a, T> Parser<'a, T>
where
T: FromStr + PartialEq
{
fn test_parse() -> Result<(), ParseTreeError> {
let mut val = T::from_str("5").unwrap();
Ok(())
}
}
This throws the following error:
error[E0599]: the method `unwrap` exists for enum `std::result::Result<T, <T as FromStr>::Err>`, but its trait bounds were not satisfied
--> src/tree/parser.rs:65:40
|
65 | let mut val = T::from_str("5").unwrap();
| ^^^^^^ method cannot be called on `std::result::Result<T, <T as FromStr>::Err>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`<T as FromStr>::Err: Debug`
If I map the error like this
let mut val2 = T::from_str("5").map_err(|_| ParseTreeError::ReadError).unwrap();
Then no error is thrown.
If i try to use the ? operator i get the error
error[E0277]: `?` couldn't convert the error to `ParseTreeError`
--> src/tree/parser.rs:65:40
|
65 | let mut val1 = T::from_str("5")?;
| ^ the trait `From<<T as FromStr>::Err>` is not implemented for `ParseTreeError`
|
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= note: required by `from`
What do I need to do so that the ? operator can automatically convert the error to the correct error type without having to use map_err ?