noodles_sam/header/record/value/
map.rs

1//! SAM header record map value.
2
3pub mod builder;
4pub mod header;
5pub mod program;
6pub mod read_group;
7pub mod reference_sequence;
8pub mod tag;
9
10pub use self::{
11    builder::Builder, header::Header, program::Program, read_group::ReadGroup,
12    reference_sequence::ReferenceSequence, tag::Tag,
13};
14
15use bstr::BString;
16use indexmap::IndexMap;
17
18pub(crate) type OtherFields<S> = IndexMap<tag::Other<S>, BString>;
19
20/// An inner SAM header record map value.
21pub trait Inner: Sized {
22    /// The standard tag type.
23    type StandardTag: tag::Standard;
24
25    /// The builder type.
26    type Builder: builder::Inner<Self>;
27}
28
29/// A SAM header record map value.
30#[derive(Clone, Debug, Eq, PartialEq)]
31pub struct Map<I>
32where
33    I: Inner,
34{
35    pub(crate) inner: I,
36    pub(crate) other_fields: OtherFields<I::StandardTag>,
37}
38
39impl<I> Map<I>
40where
41    I: Inner,
42{
43    /// Creates a SAM header record map value.
44    pub fn builder() -> Builder<I> {
45        Builder::default()
46    }
47
48    /// Returns the nonstandard fields in the map.
49    pub fn other_fields(&self) -> &OtherFields<I::StandardTag> {
50        &self.other_fields
51    }
52
53    /// Returns a mutable reference to the nonstandard fields in the map.
54    ///
55    /// # Example
56    ///
57    /// ```
58    /// use bstr::BString;
59    /// use noodles_sam::header::record::value::{map::{tag, Header}, Map};
60    /// let mut map = Map::<Header>::new(Default::default());
61    /// let nd = tag::Other::try_from([b'n', b'd'])?;
62    /// map.other_fields_mut().insert(nd, BString::from("noodles"));
63    /// # Ok::<_, tag::ParseError>(())
64    /// ```
65    pub fn other_fields_mut(&mut self) -> &mut OtherFields<I::StandardTag> {
66        &mut self.other_fields
67    }
68}
69
70impl<I> Default for Map<I>
71where
72    I: Inner + Default,
73{
74    fn default() -> Self {
75        Self {
76            inner: I::default(),
77            other_fields: OtherFields::new(),
78        }
79    }
80}