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
//! Inner VCF header contig map value.
mod builder;
pub mod name;
pub(crate) mod tag;
pub use self::{name::Name, tag::Tag};
use std::fmt;
use super::{Indexed, Inner, Map};
/// An inner VCF header contig map value.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Contig {
pub(crate) length: Option<usize>,
pub(crate) md5: Option<String>,
pub(crate) url: Option<String>,
pub(crate) idx: Option<usize>,
}
impl Inner for Contig {
type StandardTag = tag::Standard;
type Builder = builder::Builder;
}
impl Indexed for Contig {
fn idx(&self) -> Option<usize> {
self.idx
}
fn idx_mut(&mut self) -> &mut Option<usize> {
&mut self.idx
}
}
impl Map<Contig> {
/// Creates a VCF header contig map value.
///
/// # Examples
///
/// ```
/// use noodles_vcf::header::record::value::{map::Contig, Map};
/// let map = Map::<Contig>::new();
/// ```
pub fn new() -> Self {
Self::default()
}
/// Returns the length.
///
/// # Examples
///
/// ```
/// use noodles_vcf::header::record::value::{map::Contig, Map};
/// let map = Map::<Contig>::new();
/// assert!(map.length().is_none());
/// ```
pub fn length(&self) -> Option<usize> {
self.inner.length
}
/// Returns a mutable reference to the length.
///
/// # Examples
///
/// ```
/// use noodles_vcf::header::record::value::{map::Contig, Map};
///
/// let mut map = Map::<Contig>::new();
/// assert!(map.length().is_none());
///
/// *map.length_mut() = Some(8);
/// assert_eq!(map.length(), Some(8));
/// ```
pub fn length_mut(&mut self) -> &mut Option<usize> {
&mut self.inner.length
}
/// Returns the MD5 hexdigest.
///
/// # Examples
///
/// ```
/// use noodles_vcf::header::record::value::{map::Contig, Map};
/// let map = Map::<Contig>::new();
/// assert!(map.md5().is_none());
/// ```
pub fn md5(&self) -> Option<&str> {
self.inner.md5.as_deref()
}
/// Returns a mutable reference to the MD5 hexdigest.
///
/// # Examples
///
/// ```
/// use noodles_vcf::header::record::value::{map::Contig, Map};
///
/// let mut map = Map::<Contig>::new();
/// assert!(map.md5().is_none());
///
/// *map.md5_mut() = Some(String::from("d7eba311421bbc9d3ada44709dd61534"));
/// assert_eq!(map.md5(), Some("d7eba311421bbc9d3ada44709dd61534"));
/// ```
pub fn md5_mut(&mut self) -> &mut Option<String> {
&mut self.inner.md5
}
/// Returns the URL.
///
/// # Examples
///
/// ```
/// use noodles_vcf::header::record::value::{map::Contig, Map};
/// let map = Map::<Contig>::new();
/// assert!(map.url().is_none());
/// ```
pub fn url(&self) -> Option<&str> {
self.inner.url.as_deref()
}
/// Returns a mutable reference to the URL.
///
/// # Examples
///
/// ```
/// use noodles_vcf::header::record::value::{map::Contig, Map};
///
/// let mut map = Map::<Contig>::new();
/// assert!(map.url().is_none());
///
/// *map.url_mut() = Some(String::from("https://example.com/reference.fa"));
/// assert_eq!(map.url(), Some("https://example.com/reference.fa"));
/// ```
pub fn url_mut(&mut self) -> &mut Option<String> {
&mut self.inner.url
}
}
impl fmt::Display for Map<Contig> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(length) = self.length() {
write!(f, ",{tag}={length}", tag = tag::LENGTH)?;
}
if let Some(md5) = self.md5() {
write!(f, ",{tag}={md5}", tag = tag::MD5)?;
}
if let Some(url) = self.url() {
write!(f, ",{tag}={url}", tag = tag::URL)?;
}
super::fmt_display_other_fields(f, self.other_fields())?;
if let Some(idx) = self.idx() {
super::fmt_display_idx_field(f, idx)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::header::record::value::map::builder::BuildError;
#[test]
fn test_fmt() -> Result<(), BuildError> {
let map = Map::<Contig>::builder()
.set_length(8)
.set_md5("d7eba311421bbc9d3ada44709dd61534")
.set_url("https://example.com/reference.fa")
.build()?;
let expected = r#",length=8,md5=d7eba311421bbc9d3ada44709dd61534,URL=https://example.com/reference.fa"#;
assert_eq!(map.to_string(), expected);
Ok(())
}
}