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
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
use std::convert::{From, TryFrom};

use crate::error::ParseError;
use crate::interval::Interval;

#[derive(Debug, PartialEq, Copy, Clone, Eq, Ord, PartialOrd)]
pub struct CDS {
    pub range: Interval,
    pub phase: Phase,
}

impl CDS {
    pub fn new(range: Interval, phase: Phase) -> Self {
        Self { range, phase }
    }
}

#[derive(Debug, PartialEq, Eq, Copy, Clone, Ord, PartialOrd)]
pub enum Phase {
    Zero,
    One,
    Two,
}

impl From<Phase> for u8 {
    fn from(phase: Phase) -> u8 {
        match phase {
            Phase::Zero => 0,
            Phase::One => 1,
            Phase::Two => 2,
        }
    }
}

impl From<Phase> for char {
    fn from(phase: Phase) -> Self {
        match phase {
            Phase::Zero => '0',
            Phase::One => '1',
            Phase::Two => '2',
        }
    }
}

const PHASES_STR: &str = "0,1 or 2";

impl TryFrom<&str> for Phase {
    type Error = ParseError;
    fn try_from(phase: &str) -> Result<Self, ParseError> {
        if phase.len() != 1 {
            Err(ParseError::somewhere(PHASES_STR, phase.to_string()))
        } else {
            let c = phase
                .chars()
                .next()
                .expect("string is exactly 1 chars long");
            match c {
                '0' => Ok(Phase::Zero),
                '1' => Ok(Phase::One),
                '2' => Ok(Phase::Two),
                _ => Err(ParseError::somewhere(PHASES_STR, c.to_string())),
            }
        }
    }
}

impl TryFrom<u8> for Phase {
    type Error = ParseError;
    fn try_from(phase: u8) -> Result<Self, ParseError> {
        match phase {
            0 => Ok(Phase::Zero),
            1 => Ok(Phase::One),
            2 => Ok(Phase::Two),
            _ => Err(ParseError::somewhere(PHASES_STR, phase.to_string())),
        }
    }
}

impl TryFrom<char> for Phase {
    type Error = ParseError;
    fn try_from(phase: char) -> Result<Self, ParseError> {
        match phase {
            '0' => Ok(Phase::Zero),
            '1' => Ok(Phase::One),
            '2' => Ok(Phase::Two),
            _ => Err(ParseError::somewhere(PHASES_STR, phase.to_string())),
        }
    }
}

impl std::fmt::Display for Phase {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Zero => "0",
                Self::One => "1",
                Self::Two => "2",
            }
        )
    }
}