pub mod alternative_allele;
mod builder;
pub mod contig;
pub mod filter;
pub mod format;
pub mod info;
pub mod other;
pub(crate) mod tag;
pub use self::{
alternative_allele::AlternativeAllele, builder::Builder, contig::Contig, filter::Filter,
format::Format, info::Info, other::Other,
};
use std::fmt::{self, Display};
use indexmap::IndexMap;
use crate::header::Number;
pub(crate) type OtherFields<S> = IndexMap<tag::Other<S>, String>;
pub trait Inner: Sized {
type StandardTag: tag::Standard;
type Builder: builder::Inner<Self>;
}
pub trait Typed: Inner {
type Type: Display;
fn number(&self) -> Number;
fn number_mut(&mut self) -> &mut Number;
fn ty(&self) -> Self::Type;
fn type_mut(&mut self) -> &mut Self::Type;
}
pub trait Described: Inner {
fn description(&self) -> &str;
fn description_mut(&mut self) -> &mut String;
}
pub trait Indexed: Inner {
fn idx(&self) -> Option<usize>;
fn idx_mut(&mut self) -> &mut Option<usize>;
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Map<I>
where
I: Inner,
{
pub(crate) inner: I,
pub(crate) other_fields: OtherFields<I::StandardTag>,
}
impl<I> Map<I>
where
I: Inner,
{
pub fn builder() -> Builder<I> {
Builder::default()
}
pub fn other_fields(&self) -> &OtherFields<I::StandardTag> {
&self.other_fields
}
}
impl<I> Default for Map<I>
where
I: Inner + Default,
{
fn default() -> Self {
Self {
inner: I::default(),
other_fields: OtherFields::new(),
}
}
}
impl<I> Map<I>
where
I: Typed,
{
pub fn number(&self) -> Number {
self.inner.number()
}
pub fn number_mut(&mut self) -> &mut Number {
self.inner.number_mut()
}
pub fn ty(&self) -> I::Type {
self.inner.ty()
}
pub fn type_mut(&mut self) -> &mut I::Type {
self.inner.type_mut()
}
}
impl<I> Map<I>
where
I: Described,
{
pub fn description(&self) -> &str {
self.inner.description()
}
pub fn description_mut(&mut self) -> &mut String {
self.inner.description_mut()
}
}
impl<I> Map<I>
where
I: Indexed,
{
pub fn idx(&self) -> Option<usize> {
self.inner.idx()
}
pub fn idx_mut(&mut self) -> &mut Option<usize> {
self.inner.idx_mut()
}
}
fn fmt_display_type_fields<T>(f: &mut fmt::Formatter<'_>, number: Number, ty: T) -> fmt::Result
where
T: Display,
{
write!(f, ",{}={}", tag::NUMBER, number)?;
write!(f, ",{}={}", tag::TYPE, ty)?;
Ok(())
}
fn fmt_display_description_field(f: &mut fmt::Formatter<'_>, description: &str) -> fmt::Result {
use crate::header::fmt::write_escaped_string;
write!(f, ",{}=", tag::DESCRIPTION)?;
write_escaped_string(f, description)?;
Ok(())
}
fn fmt_display_other_fields<S>(
f: &mut fmt::Formatter<'_>,
other_fields: &OtherFields<S>,
) -> fmt::Result {
use crate::header::fmt::write_escaped_string;
for (key, value) in other_fields {
write!(f, ",{key}=")?;
write_escaped_string(f, value)?;
}
Ok(())
}
fn fmt_display_idx_field(f: &mut fmt::Formatter<'_>, idx: usize) -> fmt::Result {
write!(f, ",{}={}", tag::IDX, idx)
}