use luaur_ast::records::location::Location;
#[derive(Debug, Clone)]
pub struct InternalCompilerError {
pub message: alloc::string::String,
pub module_name: Option<alloc::string::String>,
pub location: Option<Location>,
pub(crate) c_message: alloc::ffi::CString,
}
impl InternalCompilerError {
pub fn new(
message: alloc::string::String,
module_name: Option<alloc::string::String>,
location: Option<Location>,
) -> Self {
let c_message = nul_terminated(&message);
Self {
message,
module_name,
location,
c_message,
}
}
}
pub(crate) fn nul_terminated(s: &str) -> alloc::ffi::CString {
match alloc::ffi::CString::new(s) {
Ok(c) => c,
Err(_) => alloc::ffi::CString::new(s.replace('\0', "")).unwrap_or_default(),
}
}
unsafe impl Send for InternalCompilerError {}
unsafe impl Sync for InternalCompilerError {}
#[cfg(feature = "std")]
impl std::error::Error for InternalCompilerError {}
impl core::fmt::Display for InternalCompilerError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.message)
}
}