use crate::dataset::VariableRole;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub(crate) struct VariableMetadata {
pub domain_code: String,
pub variable_name: String,
pub xpt_type: Option<XptVarType>,
pub length: Option<usize>,
pub label: Option<String>,
pub format: Option<String>,
pub order: Option<i32>,
pub role: Option<VariableRole>,
}
#[allow(dead_code)]
impl VariableMetadata {
#[must_use]
pub(crate) fn new(domain_code: impl Into<String>, variable_name: impl Into<String>) -> Self {
Self {
domain_code: domain_code.into(),
variable_name: variable_name.into(),
..Default::default()
}
}
#[must_use]
pub(crate) fn with_xpt_type(mut self, xpt_type: XptVarType) -> Self {
self.xpt_type = Some(xpt_type);
self
}
#[must_use]
pub(crate) fn with_length(mut self, length: usize) -> Self {
self.length = Some(length);
self
}
#[must_use]
pub(crate) fn with_label(mut self, label: impl Into<String>) -> Self {
self.label = Some(label.into());
self
}
#[must_use]
pub(crate) fn with_format(mut self, format: impl Into<String>) -> Self {
self.format = Some(format.into());
self
}
#[must_use]
pub(crate) fn with_order(mut self, order: i32) -> Self {
self.order = Some(order);
self
}
#[must_use]
pub(crate) fn with_role(mut self, role: VariableRole) -> Self {
self.role = Some(role);
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum XptVarType {
#[default]
Numeric,
Character,
}
impl XptVarType {
#[must_use]
pub const fn is_numeric(&self) -> bool {
matches!(self, Self::Numeric)
}
#[must_use]
pub const fn is_character(&self) -> bool {
matches!(self, Self::Character)
}
}
impl std::fmt::Display for XptVarType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Numeric => write!(f, "Numeric"),
Self::Character => write!(f, "Character"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_variable_metadata_builder() {
let meta = VariableMetadata::new("AE", "AESER")
.with_xpt_type(XptVarType::Character)
.with_length(1)
.with_label("Serious Event")
.with_order(5);
assert_eq!(meta.domain_code, "AE");
assert_eq!(meta.variable_name, "AESER");
assert_eq!(meta.xpt_type, Some(XptVarType::Character));
assert_eq!(meta.length, Some(1));
assert_eq!(meta.label.as_deref(), Some("Serious Event"));
assert_eq!(meta.order, Some(5));
}
}