white_balancer/auto/
methods.rs1use std::fmt;
2
3#[derive(PartialEq, Debug)]
4pub enum AutoWhiteBalanceMethod {
5 GrayWorld,
6 Retinex,
7 GrayRetinex,
8}
9
10impl fmt::Display for AutoWhiteBalanceMethod {
11 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
12 let name = match *self {
13 AutoWhiteBalanceMethod::GrayWorld => "gray-world",
14 AutoWhiteBalanceMethod::Retinex => "retinex",
15 AutoWhiteBalanceMethod::GrayRetinex => "gray-retinex",
16 };
17 write!(f, "{}", name)
18 }
19}
20
21impl AutoWhiteBalanceMethod {
22 pub fn iter() -> AutoWhiteBalanceMethodIterator {
23 AutoWhiteBalanceMethodIterator::new()
24 }
25
26 pub fn try_from(val: &str) -> Result<Self, String> {
27 for method in AutoWhiteBalanceMethod::iter() {
28 if method.to_string() == val {
29 return Ok(method)
30 }
31 }
32 Err(format!("Auto white balance method '{}' not recognized", val))
33 }
34}
35
36pub struct AutoWhiteBalanceMethodIterator {
37 idx: u8,
38}
39
40impl AutoWhiteBalanceMethodIterator {
41 fn new() -> AutoWhiteBalanceMethodIterator {
42 AutoWhiteBalanceMethodIterator{idx: 0}
43 }
44}
45
46impl Iterator for AutoWhiteBalanceMethodIterator {
47 type Item = AutoWhiteBalanceMethod;
48
49 fn next(&mut self) -> Option<Self::Item> {
50 let res = match self.idx {
51 0 => Some(AutoWhiteBalanceMethod::GrayWorld),
52 1 => Some(AutoWhiteBalanceMethod::Retinex),
53 2 => Some(AutoWhiteBalanceMethod::GrayRetinex),
54 _ => None
55 };
56 self.idx += 1;
57 res
58 }
59}
60
61#[cfg(test)]
62mod methods_test {
63 use super::*;
64
65 #[test]
66 fn test_to_string() {
67 assert_eq!(AutoWhiteBalanceMethod::GrayWorld.to_string(), "gray-world");
68 }
69
70 #[test]
71 fn test_try_from() {
72 let res = AutoWhiteBalanceMethod::try_from("gray-world");
73
74 assert_eq!(res.is_err(), false);
75 assert_eq!(res.unwrap(), AutoWhiteBalanceMethod::GrayWorld);
76
77 let res = AutoWhiteBalanceMethod::try_from("retinex");
78 assert_eq!(res.unwrap(), AutoWhiteBalanceMethod::Retinex);
79
80 let res = AutoWhiteBalanceMethod::try_from("wrong");
81 assert_eq!(res.is_err(), true);
82 }
83}