forma_sif 0.1.0

SIF serialization and deserialization for forma_core.
Documentation
use core::fmt;
use forma_core::error::{DeError, SerError};

#[derive(Debug)]
pub enum Error {
    /// Generic message error.
    Message(String),
    /// Parse error from the sif-parser crate.
    Parse(sif::Error),
    /// No schema found in section.
    NoSchema,
    /// Field not found in schema.
    UnknownField(String),
    /// Unexpected SIF value type.
    TypeMismatch { field: String, expected: String, got: String },
    /// Wrong number of fields in record.
    FieldCount { expected: usize, got: usize },
    /// No more records.
    Eof,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::Message(msg) => f.write_str(msg),
            Error::Parse(e) => write!(f, "SIF parse error: {e}"),
            Error::NoSchema => f.write_str("no schema found in SIF section"),
            Error::UnknownField(name) => write!(f, "unknown field `{name}` in SIF schema"),
            Error::TypeMismatch { field, expected, got } => {
                write!(f, "field `{field}`: expected {expected}, got {got}")
            }
            Error::FieldCount { expected, got } => {
                write!(f, "expected {expected} fields, got {got}")
            }
            Error::Eof => f.write_str("no more records"),
        }
    }
}

impl std::error::Error for Error {}

impl SerError for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Error::Message(msg.to_string())
    }
}

impl DeError for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Error::Message(msg.to_string())
    }
}

impl From<sif::Error> for Error {
    fn from(e: sif::Error) -> Self {
        Error::Parse(e)
    }
}

impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Error::Message(e.to_string())
    }
}