-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition.rs
102 lines (91 loc) · 2.4 KB
/
position.rs
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
use std::fmt::{self, Display, Formatter};
use serde::Deserialize;
/// Absolute or relative position within a chromosome or read (in base pairs).
///
/// This is just a struct containing a [`usize`].
#[derive(Debug, Clone, Copy, Deserialize, Hash, PartialEq, Eq)]
pub struct Position(usize);
impl Display for Position {
/// Return a formatted [`String`] representation of a [`Position`].
///
/// # Example
/// ```
/// use pmd_mask::genome::Position;
/// let position: Position = 42.into();
/// assert_eq!(&format!("{position: ^6}"), " 42 ");
/// ```
///
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl Position {
/// Create a new [`Position`] from a usize
///
/// # Example
/// ```
/// use pmd_mask::genome::Position;
///
/// let position = Position::new(42usize);
/// assert_eq!(position, 42.into());
/// ```
///
pub fn new(value: usize) -> Self {
Self(value)
}
//// Get the inner `usize` contained within a [`Position`] back
///
/// # Example
/// ```
/// use pmd_mask::genome::Position;
///
/// assert_eq!(Position::from(42).inner(), 42);
/// ```
///
pub fn inner(&self) -> usize {
self.0
}
}
impl From<usize> for Position {
/// Convert a [`usize`] from and into a [`Position`]
/// ```
/// use pmd_mask::genome::Position;
///
/// let pos1: Position = 55.into();
/// let pos2: Position = Position::from(55);
/// assert_eq!(pos1, pos2);
/// ```
fn from(value: usize) -> Self {
Position(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_test::Token;
use serde_test::assert_de_tokens;
#[test]
fn display() {
let chr = Position::new(10_000);
assert_eq!("10000", format!("{chr}"));
assert_eq!("10000----", format!("{chr:-<9}"))
}
#[test]
fn get_inner() {
let chr = Position::new(15_654);
assert_eq!(chr.inner(), 15_654)
}
#[test]
fn deserialize() {
assert_de_tokens(&Position::new(120_000_000), &[
Token::TupleStruct{name: "Position", len: 1},
Token::U32(120_000_000),
Token::TupleStructEnd
])
}
#[test]
fn equality() {
assert!(Position::new(10) == Position::new(10));
assert!(Position::new(10) != Position::new(11));
}
}