luaur_analysis/records/
internal_compiler_error.rs1use luaur_ast::records::location::Location;
2
3#[derive(Debug, Clone)]
4pub struct InternalCompilerError {
5 pub message: alloc::string::String,
6 pub module_name: Option<alloc::string::String>,
7 pub location: Option<Location>,
8 pub(crate) c_message: alloc::ffi::CString,
14}
15
16impl InternalCompilerError {
17 pub fn new(
20 message: alloc::string::String,
21 module_name: Option<alloc::string::String>,
22 location: Option<Location>,
23 ) -> Self {
24 let c_message = nul_terminated(&message);
25 Self {
26 message,
27 module_name,
28 location,
29 c_message,
30 }
31 }
32}
33
34pub(crate) fn nul_terminated(s: &str) -> alloc::ffi::CString {
37 match alloc::ffi::CString::new(s) {
38 Ok(c) => c,
39 Err(_) => alloc::ffi::CString::new(s.replace('\0', "")).unwrap_or_default(),
40 }
41}
42
43unsafe impl Send for InternalCompilerError {}
44unsafe impl Sync for InternalCompilerError {}
45
46#[cfg(feature = "std")]
47impl std::error::Error for InternalCompilerError {}
48
49impl core::fmt::Display for InternalCompilerError {
50 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
51 write!(f, "{}", self.message)
52 }
53}