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
use std::collections::HashMap;

use super::BlockContentEncoderMap;
use crate::{
    codecs::Encoder,
    container::block,
    data_container::compression_header::{
        data_series_encoding_map::DataSeries, preservation_map::tag_ids_dictionary,
    },
};

/// A CRAM data container block content-encoder map builder.
#[derive(Debug)]
pub struct Builder {
    core_data_encoder: Option<Encoder>,
    data_series_encoders: Vec<Option<Encoder>>,
    tag_values_encoders: HashMap<block::ContentId, Option<Encoder>>,
}

impl Builder {
    /// Sets the core data encoder.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_cram::data_container::BlockContentEncoderMap;
    /// let builder = BlockContentEncoderMap::builder().set_core_data_encoder(None);
    /// ```
    pub fn set_core_data_encoder(mut self, encoder: Option<Encoder>) -> Self {
        self.core_data_encoder = encoder;
        self
    }

    /// Sets a data series encoder.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_cram::data_container::{
    ///     compression_header::data_series_encoding_map::DataSeries,
    ///     BlockContentEncoderMap,
    /// };
    ///
    /// let builder = BlockContentEncoderMap::builder()
    ///     .set_data_series_encoder(DataSeries::BamBitFlags, None);
    /// ```
    pub fn set_data_series_encoder(
        mut self,
        data_series: DataSeries,
        encoder: Option<Encoder>,
    ) -> Self {
        let i = (i32::from(block::ContentId::from(data_series)) as usize) - 1;
        self.data_series_encoders[i] = encoder;
        self
    }

    /// Sets a tag values encoder.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_cram::data_container::{
    ///     compression_header::preservation_map::tag_ids_dictionary::Key,
    ///     BlockContentEncoderMap,
    /// };
    /// use noodles_sam::alignment::record::data::field::{Tag, Type};
    ///
    /// let key = Key::new(Tag::ALIGNMENT_HIT_COUNT, Type::UInt8);
    /// let builder = BlockContentEncoderMap::builder()
    ///     .set_tag_values_encoder(key, None);
    /// ```
    pub fn set_tag_values_encoder(
        mut self,
        key: tag_ids_dictionary::Key,
        encoder: Option<Encoder>,
    ) -> Self {
        let id = block::ContentId::from(key);
        self.tag_values_encoders.insert(id, encoder);
        self
    }

    /// Builds a block content-encoder map.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_cram::data_container::BlockContentEncoderMap;
    /// let map = BlockContentEncoderMap::builder().build();
    /// ```
    pub fn build(self) -> BlockContentEncoderMap {
        BlockContentEncoderMap {
            core_data_encoder: self.core_data_encoder,
            data_series_encoders: self.data_series_encoders,
            tag_values_encoders: self.tag_values_encoders,
        }
    }
}

impl Default for Builder {
    fn default() -> Self {
        use flate2::Compression;

        use crate::data_container::compression_header::data_series_encoding_map::data_series::STANDARD_DATA_SERIES;

        let compression_level = Compression::default();

        Self {
            core_data_encoder: Some(Encoder::Gzip(compression_level)),
            data_series_encoders: vec![
                Some(Encoder::Gzip(compression_level));
                STANDARD_DATA_SERIES.len()
            ],
            tag_values_encoders: HashMap::new(),
        }
    }
}