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
//! SAM header reference sequence tag.

use std::marker::PhantomData;

use crate::header::record::value::map::{
    self,
    tag::{self, Other},
};

pub(crate) type Tag = map::tag::Tag<Standard>;

pub(crate) const NAME: Tag = map::tag::Tag::Standard(Standard::Name);
pub(crate) const LENGTH: Tag = map::tag::Tag::Standard(Standard::Length);

/// Alternate locus (`AH`).
pub const ALTERNATIVE_LOCUS: Other<Standard> = Other([b'A', b'H'], PhantomData);

/// Alternate reference sequence names (`AN`).
pub const ALTERNATIVE_NAMES: Other<Standard> = Other([b'A', b'N'], PhantomData);

/// Genome assembly ID (`AS`).
pub const ASSEMBLY_ID: Other<Standard> = Other([b'A', b'S'], PhantomData);

/// Description (`DS`).
pub const DESCRIPTION: Other<Standard> = Other([b'D', b'S'], PhantomData);

/// MD5 checksum of the reference sequence (`M5`).
pub const MD5_CHECKSUM: Other<Standard> = Other([b'M', b'5'], PhantomData);

/// Species (`SP`).
pub const SPECIES: Other<Standard> = Other([b'S', b'P'], PhantomData);

/// Molecule topology (`TP`).
pub const MOLECULE_TOPOLOGY: Other<Standard> = Other([b'T', b'P'], PhantomData);

/// URI of the reference sequence (`UR`).
pub const URI: Other<Standard> = Other([b'U', b'R'], PhantomData);

const SN: [u8; tag::LENGTH] = [b'S', b'N'];
const LN: [u8; tag::LENGTH] = [b'L', b'N'];

/// A SAM header reference sequence tag.
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub enum Standard {
    /// Reference sequence name (`SN`).
    Name,
    /// Reference sequence length (`LN`).
    Length,
}

impl map::tag::Standard for Standard {}

impl AsRef<[u8; tag::LENGTH]> for Standard {
    fn as_ref(&self) -> &[u8; tag::LENGTH] {
        match self {
            Standard::Name => &SN,
            Standard::Length => &LN,
        }
    }
}

impl TryFrom<[u8; tag::LENGTH]> for Standard {
    type Error = ();

    fn try_from(b: [u8; tag::LENGTH]) -> Result<Self, Self::Error> {
        match b {
            SN => Ok(Self::Name),
            LN => Ok(Self::Length),
            _ => Err(()),
        }
    }
}

impl From<Standard> for [u8; tag::LENGTH] {
    fn from(tag: Standard) -> Self {
        match tag {
            Standard::Name => SN,
            Standard::Length => LN,
        }
    }
}

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

    #[test]
    fn test_as_ref_u8_2_array_for_standard() {
        assert_eq!(Standard::Name.as_ref(), &[b'S', b'N']);
        assert_eq!(Standard::Length.as_ref(), &[b'L', b'N']);
    }

    #[test]
    fn test_from_str() {
        assert_eq!(Standard::try_from([b'S', b'N']), Ok(Standard::Name));
        assert_eq!(Standard::try_from([b'L', b'N']), Ok(Standard::Length));
        assert_eq!(Standard::try_from([b'N', b'D']), Err(()));
    }

    #[test]
    fn test_from_standard_for_u8_2_array() {
        assert_eq!(<[u8; tag::LENGTH]>::from(Standard::Name), [b'S', b'N']);
        assert_eq!(<[u8; tag::LENGTH]>::from(Standard::Length), [b'L', b'N']);
    }
}