hail_core 0.3.0

a library for implementing a speedrun timer
Documentation
/*
  Copyright 2024 periwinkle

  This Source Code Form is subject to the terms of the Mozilla Public
  License, v. 2.0. If a copy of the MPL was not distributed with this
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

use crate::run::InvalidReason;

use std::io::Error as IoErr;

#[cfg(feature = "serde")]
use ron::error::{Error as RonErr, SpannedError};
#[cfg(feature = "serde")]
use roxmltree::Error as XmlError;
use thiserror::Error;

pub type HailResult<T> = Result<T, HailError>;

/// Errors that this crate can produce.
///
/// Mostly from serializing and deserializing a [`Run`](crate::Run).
#[derive(Error, Debug)]
pub enum HailError {
    #[cfg(feature = "serde")]
    #[error("Could not serialize: {0}")]
    Ser(#[from] RonErr),
    #[cfg(feature = "serde")]
    #[error("Could not deserialize: {0}")]
    De(#[from] DeError),
    #[error("I/O Error: {0}")]
    Io(#[from] IoErr),
    #[error("{0}")]
    Str(String),
    #[error("Invalid run: {0}")]
    InvalidRun(#[from] InvalidReason),
}

/// Failure to deserialize a Run.
#[cfg(feature = "serde")]
#[derive(Error, Debug)]
pub enum DeError {
    /// RON syntax error.
    #[error("{0}")]
    Ron(SpannedError),
    /// XML syntax error.
    #[error("{0}")]
    Xml(XmlError),
    /// Version identifier is missing from file.
    #[error("Invalid or missing version!")]
    InvalidVersion,
}

impl From<String> for HailError {
    fn from(s: String) -> Self {
        Self::Str(s)
    }
}

impl From<&str> for HailError {
    fn from(s: &str) -> Self {
        Self::Str(s.into())
    }
}