kira-mmcif 0.2.0

Low-level, streaming mmCIF/BinaryCIF parser focused on protein coordinates.
Documentation
use std::fmt;
use std::io;
use std::num::{ParseFloatError, ParseIntError};

#[derive(Debug)]
pub enum MmCifError {
    Io(io::Error),
    Parse(String),
    MissingField(&'static str),
    InvalidChainId(String),
    InvalidModelCount(usize),
    UnsupportedFormat(String),
    UnsupportedEncoding(String),
    ResourceLimit(&'static str),
}

impl fmt::Display for MmCifError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Io(err) => write!(f, "io error: {err}"),
            Self::Parse(msg) => write!(f, "parse error: {msg}"),
            Self::MissingField(name) => write!(f, "missing required field: {name}"),
            Self::InvalidChainId(id) => write!(f, "invalid chain id: {id}"),
            Self::InvalidModelCount(count) => write!(f, "expected 1 model, got {count}"),
            Self::UnsupportedFormat(name) => write!(f, "unsupported format: {name}"),
            Self::UnsupportedEncoding(name) => write!(f, "unsupported encoding: {name}"),
            Self::ResourceLimit(name) => write!(f, "resource limit exceeded: {name}"),
        }
    }
}

impl std::error::Error for MmCifError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io(err) => Some(err),
            _ => None,
        }
    }
}

impl From<io::Error> for MmCifError {
    fn from(err: io::Error) -> Self {
        Self::Io(err)
    }
}

impl From<ParseIntError> for MmCifError {
    fn from(err: ParseIntError) -> Self {
        Self::Parse(format!("invalid integer: {err}"))
    }
}

impl From<ParseFloatError> for MmCifError {
    fn from(err: ParseFloatError) -> Self {
        Self::Parse(format!("invalid float: {err}"))
    }
}