noodles_vcf/header/record/value/map/
builder.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use std::{error, fmt};

use super::{tag, Map, OtherFields};

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BuildError {
    MissingField(&'static str),
}

impl error::Error for BuildError {}

impl fmt::Display for BuildError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::MissingField(tag) => write!(f, "missing field: {tag}"),
        }
    }
}

pub trait Inner<I>: Default
where
    I: super::Inner,
{
    fn build(self) -> Result<I, BuildError>;
}

pub trait Typed<I>
where
    I: super::Typed,
{
    fn set_number(self, number: I::Number) -> Self;
    fn set_type(self, ty: I::Type) -> Self;
}

pub trait Described<I>
where
    I: super::Described,
{
    fn set_description<D>(self, description: D) -> Self
    where
        D: Into<String>;
}

pub trait Indexed<I>
where
    I: super::Indexed,
{
    fn set_idx(self, idx: usize) -> Self;
}

/// A VCF header record map value builder.
pub struct Builder<I>
where
    I: super::Inner,
{
    pub(super) inner: I::Builder,
    other_fields: OtherFields<I::StandardTag>,
}

impl<I> Builder<I>
where
    I: super::Inner,
{
    /// Inserts a key-value pair into the other fields.
    pub fn insert<V>(mut self, key: tag::Other<I::StandardTag>, value: V) -> Self
    where
        V: Into<String>,
    {
        self.other_fields.insert(key, value.into());
        self
    }

    /// Builds a VCF header record map value.
    pub fn build(self) -> Result<Map<I>, BuildError> {
        let inner = self.inner.build()?;

        Ok(Map {
            inner,
            other_fields: self.other_fields,
        })
    }
}

impl<I> Builder<I>
where
    I: super::Typed,
    I::Builder: Typed<I>,
{
    /// Sets the number.
    pub fn set_number(mut self, number: I::Number) -> Self {
        self.inner = self.inner.set_number(number);
        self
    }

    /// Sets the type.
    pub fn set_type(mut self, ty: I::Type) -> Self {
        self.inner = self.inner.set_type(ty);
        self
    }
}

impl<I> Builder<I>
where
    I: super::Described,
    I::Builder: Described<I>,
{
    /// Sets the description.
    pub fn set_description<D>(mut self, description: D) -> Self
    where
        D: Into<String>,
    {
        self.inner = self.inner.set_description(description);
        self
    }
}

impl<I> Builder<I>
where
    I: super::Indexed,
    I::Builder: Indexed<I>,
{
    /// Sets the index.
    pub fn set_idx(mut self, idx: usize) -> Self {
        self.inner = self.inner.set_idx(idx);
        self
    }
}

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

#[derive(Default)]
pub struct Identity;

impl<I> Inner<I> for Identity
where
    I: Default + super::Inner,
{
    fn build(self) -> Result<I, BuildError> {
        Ok(I::default())
    }
}

pub struct TypedDescribedIndexed<I>
where
    I: super::Typed + super::Described + super::Indexed,
{
    pub(super) number: Option<I::Number>,
    pub(super) ty: Option<I::Type>,
    pub(super) description: Option<String>,
    pub(super) idx: Option<usize>,
}

impl<I> Typed<I> for TypedDescribedIndexed<I>
where
    I: super::Typed + super::Described + super::Indexed,
{
    fn set_number(mut self, number: I::Number) -> Self {
        self.number = Some(number);
        self
    }

    fn set_type(mut self, ty: I::Type) -> Self {
        self.ty = Some(ty);
        self
    }
}

impl<I> Described<I> for TypedDescribedIndexed<I>
where
    I: super::Typed + super::Described + super::Indexed,
{
    fn set_description<D>(mut self, description: D) -> Self
    where
        D: Into<String>,
    {
        self.description = Some(description.into());
        self
    }
}

impl<I> Indexed<I> for TypedDescribedIndexed<I>
where
    I: super::Typed + super::Described + super::Indexed,
{
    fn set_idx(mut self, idx: usize) -> Self {
        self.idx = Some(idx);
        self
    }
}

impl<I> Default for TypedDescribedIndexed<I>
where
    I: super::Typed + super::Described + super::Indexed,
{
    fn default() -> Self {
        Self {
            number: None,
            ty: None,
            description: None,
            idx: None,
        }
    }
}

#[derive(Default)]
pub struct DescribedIndexed {
    pub(super) description: Option<String>,
    pub(super) idx: Option<usize>,
}

impl<I> Described<I> for DescribedIndexed
where
    I: super::Described,
{
    fn set_description<D>(mut self, description: D) -> Self
    where
        D: Into<String>,
    {
        self.description = Some(description.into());
        self
    }
}

impl<I> Indexed<I> for DescribedIndexed
where
    I: super::Indexed,
{
    fn set_idx(mut self, idx: usize) -> Self {
        self.idx = Some(idx);
        self
    }
}