use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct MaveContext {
pub protein_accession: Option<String>,
pub coding_accession: Option<String>,
pub noncoding_accession: Option<String>,
pub genomic_accession: Option<String>,
pub gene_symbol: Option<String>,
pub score_set_urn: Option<String>,
pub target_name: Option<String>,
}
impl MaveContext {
pub fn new() -> Self {
Self::default()
}
pub fn with_protein_accession(mut self, accession: impl Into<String>) -> Self {
self.protein_accession = Some(accession.into());
self
}
pub fn with_coding_accession(mut self, accession: impl Into<String>) -> Self {
self.coding_accession = Some(accession.into());
self
}
pub fn with_noncoding_accession(mut self, accession: impl Into<String>) -> Self {
self.noncoding_accession = Some(accession.into());
self
}
pub fn with_genomic_accession(mut self, accession: impl Into<String>) -> Self {
self.genomic_accession = Some(accession.into());
self
}
pub fn with_gene_symbol(mut self, symbol: impl Into<String>) -> Self {
self.gene_symbol = Some(symbol.into());
self
}
pub fn with_score_set_urn(mut self, urn: impl Into<String>) -> Self {
self.score_set_urn = Some(urn.into());
self
}
pub fn with_target_name(mut self, name: impl Into<String>) -> Self {
self.target_name = Some(name.into());
self
}
pub fn accession_for_coordinate_type(&self, coord_type: char) -> Option<&str> {
match coord_type {
'p' => self.protein_accession.as_deref(),
'c' => self.coding_accession.as_deref(),
'n' => self.noncoding_accession.as_deref(),
'g' => self.genomic_accession.as_deref(),
'r' => self.coding_accession.as_deref(), 'm' | 'o' => self.genomic_accession.as_deref(), _ => None,
}
}
pub fn has_accessions(&self) -> bool {
self.protein_accession.is_some()
|| self.coding_accession.is_some()
|| self.noncoding_accession.is_some()
|| self.genomic_accession.is_some()
}
pub fn supports_coordinate_type(&self, coord_type: char) -> bool {
self.accession_for_coordinate_type(coord_type).is_some()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_context() {
let ctx = MaveContext::new();
assert!(ctx.protein_accession.is_none());
assert!(ctx.coding_accession.is_none());
assert!(!ctx.has_accessions());
}
#[test]
fn test_with_protein_accession() {
let ctx = MaveContext::new().with_protein_accession("NP_000509.1");
assert_eq!(ctx.protein_accession, Some("NP_000509.1".to_string()));
assert!(ctx.has_accessions());
}
#[test]
fn test_with_coding_accession() {
let ctx = MaveContext::new().with_coding_accession("NM_000518.5");
assert_eq!(ctx.coding_accession, Some("NM_000518.5".to_string()));
assert!(ctx.has_accessions());
}
#[test]
fn test_full_context() {
let ctx = MaveContext::new()
.with_protein_accession("NP_000509.1")
.with_coding_accession("NM_000518.5")
.with_genomic_accession("NC_000011.10")
.with_gene_symbol("HBB")
.with_score_set_urn("urn:mavedb:00000001-a-1");
assert_eq!(ctx.protein_accession, Some("NP_000509.1".to_string()));
assert_eq!(ctx.coding_accession, Some("NM_000518.5".to_string()));
assert_eq!(ctx.genomic_accession, Some("NC_000011.10".to_string()));
assert_eq!(ctx.gene_symbol, Some("HBB".to_string()));
assert_eq!(
ctx.score_set_urn,
Some("urn:mavedb:00000001-a-1".to_string())
);
}
#[test]
fn test_accession_for_coordinate_type() {
let ctx = MaveContext::new()
.with_protein_accession("NP_000509.1")
.with_coding_accession("NM_000518.5")
.with_genomic_accession("NC_000011.10");
assert_eq!(ctx.accession_for_coordinate_type('p'), Some("NP_000509.1"));
assert_eq!(ctx.accession_for_coordinate_type('c'), Some("NM_000518.5"));
assert_eq!(ctx.accession_for_coordinate_type('g'), Some("NC_000011.10"));
assert_eq!(ctx.accession_for_coordinate_type('n'), None);
assert_eq!(ctx.accession_for_coordinate_type('r'), Some("NM_000518.5"));
}
#[test]
fn test_supports_coordinate_type() {
let ctx = MaveContext::new()
.with_protein_accession("NP_000509.1")
.with_coding_accession("NM_000518.5");
assert!(ctx.supports_coordinate_type('p'));
assert!(ctx.supports_coordinate_type('c'));
assert!(!ctx.supports_coordinate_type('g'));
assert!(!ctx.supports_coordinate_type('n'));
}
#[test]
fn test_serde() {
let ctx = MaveContext::new()
.with_protein_accession("NP_000509.1")
.with_gene_symbol("HBB");
let json = serde_json::to_string(&ctx).unwrap();
let ctx2: MaveContext = serde_json::from_str(&json).unwrap();
assert_eq!(ctx, ctx2);
}
}