noodles_vcf/header/record/value/
map.rs

1//! VCF header map value.
2
3pub mod alternative_allele;
4mod builder;
5pub mod contig;
6pub mod filter;
7pub mod format;
8pub mod info;
9pub mod other;
10pub(crate) mod tag;
11
12pub use self::{
13    alternative_allele::AlternativeAllele, builder::Builder, contig::Contig, filter::Filter,
14    format::Format, info::Info, other::Other,
15};
16
17use std::fmt;
18
19use indexmap::IndexMap;
20
21pub(crate) type OtherFields<S> = IndexMap<tag::Other<S>, String>;
22
23/// An inner VCF header map value.
24pub trait Inner: Sized {
25    /// The standard tag type.
26    type StandardTag: tag::Standard;
27
28    /// The builder type.
29    type Builder: builder::Inner<Self>;
30}
31
32/// An inner VCF header map value with number and type fields.
33pub trait Typed: Inner {
34    /// The number type.
35    type Number;
36
37    /// The type type.
38    type Type: fmt::Display;
39
40    /// Returns the cardinality of the field value.
41    fn number(&self) -> Self::Number;
42
43    /// Returns a mutable reference to the number.
44    fn number_mut(&mut self) -> &mut Self::Number;
45
46    /// Returns the type of the field value.
47    fn ty(&self) -> Self::Type;
48
49    /// Returns a mutable reference to the type.
50    fn type_mut(&mut self) -> &mut Self::Type;
51}
52
53/// An inner VCF header map value with a description field.
54pub trait Described: Inner {
55    /// Returns the description.
56    fn description(&self) -> &str;
57
58    /// Returns a mutable reference to the description.
59    fn description_mut(&mut self) -> &mut String;
60}
61
62/// An inner VCF header map value with an IDX field.
63pub trait Indexed: Inner {
64    /// Returns the index of the ID in the dictionary of strings.
65    fn idx(&self) -> Option<usize>;
66
67    /// Returns a mutable reference to the index.
68    fn idx_mut(&mut self) -> &mut Option<usize>;
69}
70
71/// A VCF header map value.
72#[derive(Clone, Debug, Eq, PartialEq)]
73pub struct Map<I>
74where
75    I: Inner,
76{
77    pub(crate) inner: I,
78    pub(crate) other_fields: OtherFields<I::StandardTag>,
79}
80
81impl<I> Map<I>
82where
83    I: Inner,
84{
85    /// Creates a VCF header map value builder.
86    pub fn builder() -> Builder<I> {
87        Builder::default()
88    }
89
90    /// Returns the nonstandard fields in the map.
91    pub fn other_fields(&self) -> &OtherFields<I::StandardTag> {
92        &self.other_fields
93    }
94
95    /// Returns a mutable reference to the nonstandard fields in the map.
96    ///
97    /// # Examples
98    ///
99    /// ```
100    /// use noodles_vcf::header::record::value::{map::Filter, Map};
101    ///
102    /// let tag = match "noodles".parse() {
103    ///     Ok(tag) => tag,
104    ///     Err(_) => unreachable!(),
105    /// };
106    ///
107    /// let mut map = Map::<Filter>::pass();
108    /// map.other_fields_mut().insert(tag, String::from("vcf"));
109    /// ```
110    pub fn other_fields_mut(&mut self) -> &mut OtherFields<I::StandardTag> {
111        &mut self.other_fields
112    }
113}
114
115impl<I> Default for Map<I>
116where
117    I: Inner + Default,
118{
119    fn default() -> Self {
120        Self {
121            inner: I::default(),
122            other_fields: OtherFields::new(),
123        }
124    }
125}
126
127impl<I> Map<I>
128where
129    I: Typed,
130{
131    /// Returns the cardinality of the field value.
132    pub fn number(&self) -> I::Number {
133        self.inner.number()
134    }
135
136    /// Returns a mutable reference to the number.
137    pub fn number_mut(&mut self) -> &mut I::Number {
138        self.inner.number_mut()
139    }
140
141    /// Returns the type of the field value.
142    pub fn ty(&self) -> I::Type {
143        self.inner.ty()
144    }
145
146    /// Returns a mutable reference to the type.
147    pub fn type_mut(&mut self) -> &mut I::Type {
148        self.inner.type_mut()
149    }
150}
151
152impl<I> Map<I>
153where
154    I: Described,
155{
156    /// Returns the description.
157    pub fn description(&self) -> &str {
158        self.inner.description()
159    }
160
161    /// Returns a mutable reference to the description.
162    pub fn description_mut(&mut self) -> &mut String {
163        self.inner.description_mut()
164    }
165}
166
167impl<I> Map<I>
168where
169    I: Indexed,
170{
171    /// Returns the index of the ID in the dictionary of strings.
172    pub fn idx(&self) -> Option<usize> {
173        self.inner.idx()
174    }
175
176    /// Returns a mutable reference to the index.
177    pub fn idx_mut(&mut self) -> &mut Option<usize> {
178        self.inner.idx_mut()
179    }
180}