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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//! VCF record position.

use std::{cmp::Ordering, fmt, num, str::FromStr};

use noodles_core as core;

/// A VCF record position.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Position(usize);

impl fmt::Display for Position {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// An error returned when a raw VCF record position fails to parse.
pub type ParseError = num::ParseIntError;

impl FromStr for Position {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        s.parse().map(Self)
    }
}

impl From<usize> for Position {
    fn from(n: usize) -> Self {
        Self(n)
    }
}

impl From<core::Position> for Position {
    fn from(position: core::Position) -> Self {
        Self::from(usize::from(position))
    }
}

impl From<Position> for usize {
    fn from(position: Position) -> Self {
        position.0
    }
}

impl PartialEq<core::Position> for Position {
    fn eq(&self, other: &core::Position) -> bool {
        self.0.eq(&usize::from(*other))
    }
}

impl PartialOrd<core::Position> for Position {
    fn partial_cmp(&self, other: &core::Position) -> Option<Ordering> {
        if self.0 == 0 {
            None
        } else {
            self.0.partial_cmp(&usize::from(*other))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_fmt() {
        assert_eq!(Position(0).to_string(), "0");
        assert_eq!(Position(8).to_string(), "8");
        assert_eq!(Position(13).to_string(), "13");
    }

    #[test]
    fn test_from_str() {
        use std::num::IntErrorKind;

        assert_eq!("0".parse(), Ok(Position(0)));
        assert_eq!("8".parse(), Ok(Position(8)));
        assert_eq!("13".parse(), Ok(Position(13)));

        assert!(matches!("".parse::<Position>(), Err(e) if e.kind() == &IntErrorKind::Empty));
        assert!(
            matches!("ndls".parse::<Position>(), Err(e) if e.kind() == &IntErrorKind::InvalidDigit)
        );
        assert!(
            matches!("-1".parse::<Position>(), Err(e) if e.kind() == &IntErrorKind::InvalidDigit)
        );
    }

    #[test]
    fn test_from_usize_for_position() {
        assert_eq!(Position::from(0), Position(0));
        assert_eq!(Position::from(8), Position(8));
        assert_eq!(Position::from(13), Position(13));
    }

    #[test]
    fn test_from_position_for_usize() {
        assert_eq!(usize::from(Position::from(0)), 0);
        assert_eq!(usize::from(Position::from(8)), 8);
        assert_eq!(usize::from(Position::from(13)), 13);
    }

    #[test]
    fn test_partial_eq_core_position_for_position() {
        let q = core::Position::MIN;

        let p = Position::from(1);
        assert_eq!(p, q);

        let p = Position::from(0);
        assert_ne!(p, q);
    }

    #[allow(clippy::nonminimal_bool)]
    #[test]
    fn test_partial_ord_core_position_for_position() -> Result<(), core::position::TryFromIntError>
    {
        let q = core::Position::try_from(8)?;

        let p = Position::from(7);
        assert!(p < q);
        assert!(p <= q);
        assert!(!(p >= q));
        assert!(!(p > q));

        let p = Position::from(8);
        assert!(!(p < q));
        assert!(p <= q);
        assert!(p >= q);
        assert!(!(p > q));

        let p = Position::from(9);
        assert!(!(p < q));
        assert!(!(p <= q));
        assert!(p >= q);
        assert!(p > q);

        let p = Position::from(0);
        assert!(!(p < q));
        assert!(!(p <= q));
        assert!(!(p >= q));
        assert!(!(p > q));

        Ok(())
    }
}