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
//! SAM header record map value.

pub mod builder;
pub mod header;
pub mod program;
pub mod read_group;
pub mod reference_sequence;
mod tag;

pub use self::{
    builder::Builder, header::Header, program::Program, read_group::ReadGroup,
    reference_sequence::ReferenceSequence, tag::Tag,
};

use std::fmt;

use indexmap::IndexMap;

type Fields = Vec<(String, String)>;
type OtherFields<S> = IndexMap<tag::Other<S>, String>;

/// An inner SAM header record map value.
pub trait Inner: Sized {
    /// The standard tag type.
    type StandardTag: tag::Standard;

    /// The builder type.
    type Builder: builder::Inner<Self>;
}

/// A SAM header record map value.
// TODO: I'm not actually sure what's causing this lint warning.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Map<I>
where
    I: Inner,
{
    inner: I,
    other_fields: OtherFields<I::StandardTag>,
}

impl<I> Map<I>
where
    I: Inner,
{
    /// Creates a SAM header record map value.
    pub fn builder() -> Builder<I> {
        Builder::default()
    }

    /// Returns the nonstandard fields in the map.
    pub fn other_fields(&self) -> &OtherFields<I::StandardTag> {
        &self.other_fields
    }
}

impl<I> Default for Map<I>
where
    I: Inner + Default,
{
    fn default() -> Self {
        Self {
            inner: I::default(),
            other_fields: OtherFields::new(),
        }
    }
}

fn fmt_display_other_fields<S>(
    f: &mut fmt::Formatter<'_>,
    other_fields: &OtherFields<S>,
) -> fmt::Result {
    for (key, value) in other_fields {
        write!(f, "\t{key}:{value}")?;
    }

    Ok(())
}