use crate::{
ion::{
attr_meta::CV_REF_ATTR,
decoder::decode::{Metadatum, MetadatumValue},
utilities::{
common::{is_cv_prefix, unit_cv_ref, value_to_opt_string},
cv_table,
},
},
mzml::{
schema::TagId,
structs::{CvParam, UserParam},
},
};
#[inline]
pub(crate) fn parse_cv_and_user_params(metadata: &[&Metadatum]) -> (Vec<CvParam>, Vec<UserParam>) {
let mut cv_params = Vec::with_capacity(metadata.len());
let mut user_params = Vec::new();
for entry in metadata {
if entry.tag_id == TagId::UserParam {
user_params.push(parse_user_param(entry));
continue;
}
let Some(accession) = entry.accession.as_deref() else {
continue;
};
let Some((prefix, _)) = accession.split_once(':') else {
continue;
};
if prefix == CV_REF_ATTR || !is_cv_prefix(prefix) {
continue;
}
cv_params.push(parse_cv_param(entry, accession, prefix));
}
(cv_params, user_params)
}
#[inline]
fn parse_user_param(entry: &Metadatum) -> UserParam {
let (name, value, type_) = match &entry.value {
MetadatumValue::Text(s) => {
let mut parts = s.splitn(3, '\0');
let name_part = parts.next().unwrap_or("").to_string();
let value_part = parts
.next()
.map(|v| {
if v.is_empty() {
None
} else {
Some(v.to_string())
}
})
.unwrap_or(None);
let type_part = parts
.next()
.map(|t| {
if t.is_empty() {
None
} else {
Some(t.to_string())
}
})
.unwrap_or(None);
(name_part, value_part, type_part)
}
MetadatumValue::Number(n) => (n.to_string(), None, None),
MetadatumValue::Empty => (String::new(), None, None),
};
UserParam {
name,
value,
r#type: type_,
unit_accession: entry.unit_accession.clone(),
unit_cv_ref: unit_cv_ref(entry.unit_accession.as_deref()),
unit_name: None,
}
}
#[inline]
fn parse_cv_param(entry: &Metadatum, accession: &str, prefix: &str) -> CvParam {
let unit_accession_str = entry.unit_accession.as_deref();
let name = cv_table::get(accession)
.and_then(|v| v.as_str())
.unwrap_or(accession)
.to_owned();
let unit_name = unit_accession_str
.and_then(|unit_acc| cv_table::get(unit_acc).and_then(|v| v.as_str()))
.map(str::to_owned);
CvParam {
cv_ref: Some(prefix.to_owned()),
accession: Some(accession.to_owned()),
name,
value: value_to_opt_string(&entry.value),
unit_cv_ref: unit_cv_ref(unit_accession_str),
unit_name,
unit_accession: entry.unit_accession.clone(),
}
}