maciejhirsz/logos

Error constraints

segeljakt opened this issue · 2 comments

Hi, I wonder about these constraints on the error type:

Errors

By default, Logos uses () as the error type, which means that it doesn't store any information about the error. This can be changed by using #[logos(error = T)] attribute on the enum. The type T can be any type that implements Clone, PartialEq, Default, Debug and From<E> for each callback's error type E.

Right now I'm representing my error type as:

#[derive(Debug, Clone, PartialEq)]
enum Error {
    ParseBoolError(std::str::ParseBoolError),
    ParseCharError(std::char::ParseCharError),
    ParseFloatError(std::num::ParseFloatError),
    ParseIntError(std::num::ParseIntError),
    NoError
}

impl Default for Error {
    fn default() -> Self {
        Self::NoError
    }
}

impl From<std::str::ParseBoolError> for Error {
    fn from(err: std::str::ParseBoolError) -> Self {
        Self::ParseBoolError(err)
    }
}

// ...

Is this an idiomatic representation? In general I think std::error::Error only requires Debug and Display. It seems strange to me to require Default, but maybe it is never called? Are the constraints here some Logos-internal reason?

Ohh, wait, maybe Default is equal to "Invalid token"

Yes the default is called when the error is invalid token :)