Backup commit

My backspace key started ghosting. Nothing works atm.
This commit is contained in:
2024-01-27 14:50:33 +00:00
parent f77e4fd90a
commit a8887227e5
236 changed files with 10946 additions and 8977 deletions

68
src/foreign/error.rs Normal file
View File

@@ -0,0 +1,68 @@
use std::error::Error;
use std::fmt::{Debug, Display};
use std::sync::Arc;
use dyn_clone::DynClone;
use crate::location::CodeLocation;
/// Errors produced by external code
pub trait ExternError: Display + Send + Sync + DynClone {
/// Convert into trait object
#[must_use]
fn rc(self) -> Arc<dyn ExternError>
where Self: 'static + Sized {
Arc::new(self)
}
}
impl Debug for dyn ExternError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ExternError({self})")
}
}
impl Error for dyn ExternError {}
/// An error produced by Rust code called form Orchid. The error is type-erased.
pub type ExternResult<T> = Result<T, Arc<dyn ExternError>>;
/// Some expectation (usually about the argument types of a function) did not
/// hold.
#[derive(Clone)]
pub struct AssertionError {
location: CodeLocation,
message: &'static str,
details: String,
}
impl AssertionError {
/// Construct, upcast and wrap in a Result that never succeeds for easy
/// short-circuiting
pub fn fail<T>(
location: CodeLocation,
message: &'static str,
details: String,
) -> ExternResult<T> {
Err(Self::ext(location, message, details))
}
/// Construct and upcast to [ExternError]
pub fn ext(
location: CodeLocation,
message: &'static str,
details: String,
) -> Arc<dyn ExternError> {
Self { location, message, details }.rc()
}
}
impl Display for AssertionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Error: expected {}", self.message)?;
write!(f, " at {}", self.location)?;
write!(f, " details: {}", self.details)
}
}
impl ExternError for AssertionError {}