use crate::prelude::String;
use core::fmt;
pub type AcornResult<T> = core::result::Result<T, AcornError>;
#[cfg(feature = "std")]
pub type ApiResult<T> = core::result::Result<T, color_eyre::eyre::Report>;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AcornError {
message: String,
}
impl AcornError {
pub fn new(message: impl Into<String>) -> Self {
Self { message: message.into() }
}
pub fn message(&self) -> &str {
&self.message
}
}
impl fmt::Display for AcornError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.message)
}
}
impl core::error::Error for AcornError {}
impl From<&str> for AcornError {
fn from(message: &str) -> Self {
Self::new(message)
}
}
impl From<String> for AcornError {
fn from(message: String) -> Self {
Self::new(message)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_preserves_message() {
let error = AcornError::new("portable failure");
assert_eq!(error.message(), "portable failure");
assert_eq!(error.to_string(), "portable failure");
}
}