onelib 0.1.0

Rust implementation of the ONEcode file format
Documentation
/// The eight ONEcode field types.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum FieldType {
    Int = 1,
    Real = 2,
    Char = 3,
    String = 4,
    IntList = 5,
    RealList = 6,
    StringList = 7,
    Dna = 8,
}

impl FieldType {
    /// Parse a field type from its string name as used in ONEcode schemas.
    pub fn from_name(name: &str) -> Option<Self> {
        match name {
            "INT" => Some(Self::Int),
            "REAL" => Some(Self::Real),
            "CHAR" => Some(Self::Char),
            "STRING" => Some(Self::String),
            "INT_LIST" => Some(Self::IntList),
            "REAL_LIST" => Some(Self::RealList),
            "STRING_LIST" => Some(Self::StringList),
            "DNA" => Some(Self::Dna),
            _ => None,
        }
    }

    /// The canonical string name for this field type.
    pub fn name(self) -> &'static str {
        match self {
            Self::Int => "INT",
            Self::Real => "REAL",
            Self::Char => "CHAR",
            Self::String => "STRING",
            Self::IntList => "INT_LIST",
            Self::RealList => "REAL_LIST",
            Self::StringList => "STRING_LIST",
            Self::Dna => "DNA",
        }
    }

    /// Whether this field type is a list (has a length and variable-size data).
    pub fn is_list(self) -> bool {
        matches!(
            self,
            Self::String
                | Self::IntList
                | Self::RealList
                | Self::StringList
                | Self::Dna
        )
    }

    /// Size in bytes of a single element, for list types.
    /// Returns 0 for non-list types.
    pub fn list_element_size(self) -> usize {
        match self {
            Self::String | Self::Dna => 1,
            Self::IntList => 8,
            Self::RealList => 8,
            Self::StringList => 1,
            _ => 0,
        }
    }
}

/// Accumulated count statistics for a line type.
#[derive(Debug, Clone, Default)]
pub struct LineCounts {
    /// Number of lines of this type.
    pub count: i64,
    /// Maximum list length seen.
    pub max: i64,
    /// Sum of all list lengths.
    pub total: i64,
}

/// A provenance record (`!`-line), tracking a processing step.
#[derive(Debug, Clone)]
pub struct Provenance {
    pub program: String,
    pub version: String,
    pub command: String,
    pub date: String,
}

/// A reference to an external file (`<`-line).
#[derive(Debug, Clone)]
pub struct Reference {
    pub filename: String,
    pub count: i64,
}