Struct range_check::Error [] [src]

pub struct Error<T> {
    pub allowed_range: Bounds<T>,
    pub outside_value: T,
}

The error that gets thrown when a check_range fails.

Fields

The bounds of the range that was searched.

The value that lies outside of the range.

Methods

impl<T> Error<T>
[src]

Creates a new Error using the given value and the bounds of the given range.

This gets used by check_range, but may need to be called yourself if you're implementing, say, your own number-to-enum-variant constructor:

Examples

use range_check::{Error, Result};

#[derive(Debug)]
enum Number { One, Two, Three }

impl Number {
    fn from_u8(num: u8) -> Result<Number, u8> {
        Ok(match num {
            1 => Number::One,
            2 => Number::Two,
            3 => Number::Three,
            n => return Err(Error::new(n, 1..4)),
        })
    }
}

let error = Number::from_u8(4).unwrap_err();
assert_eq!(error.outside_value, 4);

Converts every value present in the error using Into::into. This usually has the effect of making the error more generic: you can use the same type to work with ranges of different types.

For example, you could need to validate one i8 and one i32. Ordinarily you wouldn’t be able to check both types in the same function, as one would return an Error<i8>, and the other an Error<i32>. But because there exists impl From<i8> for i32, we can use that to convert the numbers to a different type.

Examples

The function below checks both an i8 and an i32, but returns a range_check::Error<i32> because of the From implementation.

use range_check::{self, Check};

enum Error {
    OutOfRange(range_check::Error<i64>),
}

impl<E> From<range_check::Error<E>> for Error
where i64: From<E> {
    fn from(original: range_check::Error<E>) -> Error {
        Error::OutOfRange(original.generify())
    }
}

fn tiny_clock(hour: i8, minute: i32) -> Result<(i8, i32), Error> {
    let hour = try!(hour.check_range(0..24));
    let minute = try!(minute.check_range(0..60));
    Ok((hour, minute))
}

assert!(tiny_clock(23, 59).is_ok());
assert!(tiny_clock(24, 00).is_err());

Trait Implementations

impl<T: PartialEq> PartialEq for Error<T>
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<T: Debug> Debug for Error<T>
[src]

Formats the value using the given formatter.

impl<T: Clone> Clone for Error<T>
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<T: Debug + Any> ErrorTrait for Error<T>
[src]

A short description of the error. Read more

The lower-level cause of this error, if any. Read more

impl<T: Debug> Display for Error<T>
[src]

Formats the value using the given formatter. Read more