1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use super::*;

/// Trait to validate whether a type has appropriate values
pub trait Validate where Self: Sized {
    /// Return `Err()` if the values are invalid
    fn validate(self) -> ValueResult<Self>;
}

impl Validate for LabValue {
    fn validate(self) -> ValueResult<Self> {
        if self.l < 0.0    || self.l > 100.0 ||
           self.a < -128.0 || self.a > 128.0 ||
           self.b < -128.0 || self.b > 128.0
        {
            Err(ValueError::OutOfBounds)
        } else {
            Ok(self)
        }
    }
}

impl Validate for LchValue {
    fn validate(self) -> ValueResult<Self> {
        if self.l < 0.0 || self.l > 100.0 ||
           self.c < 0.0 || self.c > (128_f32.powi(2) + 128_f32.powi(2)).sqrt() ||
           self.h < 0.0 || self.h > 360.0
        {
            Err(ValueError::OutOfBounds)
        } else {
            Ok(self)
        }
    }
}

impl Validate for XyzValue {
    fn validate(self) -> ValueResult<Self> {
        if self.x < 0.0 || self.x > 1.0 ||
           self.y < 0.0 || self.y > 1.0 ||
           self.z < 0.0 || self.z > 1.0
        {
            Err(ValueError::OutOfBounds)
        } else {
            Ok(self)
        }
    }
}