Skip to content

Commit e7b1d84

Browse files
committed
binding_rust: implement Display and Error traits for TS errors to be convertible
1 parent ca6e8d9 commit e7b1d84

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

lib/binding_rust/lib.rs

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::mem::MaybeUninit;
1313
use std::os::raw::{c_char, c_void};
1414
use std::ptr::NonNull;
1515
use std::sync::atomic::AtomicUsize;
16-
use std::{char, fmt, hash, iter, ptr, slice, str, u16};
16+
use std::{char, error, fmt, hash, iter, ptr, slice, str, u16};
1717

1818
/// The latest ABI version that is supported by the current version of the
1919
/// library.
@@ -271,16 +271,6 @@ impl Language {
271271
}
272272
}
273273

274-
impl fmt::Display for LanguageError {
275-
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
276-
write!(
277-
f,
278-
"Incompatible language version {}. Expected minimum {}, maximum {}",
279-
self.version, MIN_COMPATIBLE_LANGUAGE_VERSION, LANGUAGE_VERSION,
280-
)
281-
}
282-
}
283-
284274
impl Parser {
285275
/// Create a new parser.
286276
pub fn new() -> Parser {
@@ -573,7 +563,8 @@ impl Parser {
573563
/// ```text
574564
/// ranges[i].end_byte <= ranges[i + 1].start_byte
575565
/// ```
576-
/// If this requirement is not satisfied, method will panic.
566+
/// If this requirement is not satisfied, method will return IncludedRangesError
567+
/// error with an offset in the passed ranges slice pointing to a first incorrect range.
577568
pub fn set_included_ranges<'a>(
578569
&mut self,
579570
ranges: &'a [Range],
@@ -1908,6 +1899,46 @@ fn predicate_error(row: usize, message: String) -> QueryError {
19081899
}
19091900
}
19101901

1902+
impl fmt::Display for IncludedRangesError {
1903+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1904+
write!(f, "Incorrect range by index: {}", self.0)
1905+
}
1906+
}
1907+
1908+
impl fmt::Display for LanguageError {
1909+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1910+
write!(
1911+
f,
1912+
"Incompatible language version {}. Expected minimum {}, maximum {}",
1913+
self.version, MIN_COMPATIBLE_LANGUAGE_VERSION, LANGUAGE_VERSION,
1914+
)
1915+
}
1916+
}
1917+
1918+
impl fmt::Display for QueryError {
1919+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1920+
let msg = match self.kind {
1921+
QueryErrorKind::Capture => format!("Invalid capture name {}", self.message),
1922+
QueryErrorKind::Field => format!("Invalid field name {}", self.message),
1923+
QueryErrorKind::NodeType => format!("Invalid node type {}", self.message),
1924+
QueryErrorKind::Syntax => format!("Invalid syntax:\n{}", self.message),
1925+
QueryErrorKind::Structure => format!("Impossible pattern:\n{}", self.message),
1926+
QueryErrorKind::Predicate => format!("Invalid predicate: {}", self.message),
1927+
};
1928+
write!(
1929+
f,
1930+
"Query error at {}:{}. {}",
1931+
self.row + 1,
1932+
self.column + 1,
1933+
msg
1934+
)
1935+
}
1936+
}
1937+
1938+
impl error::Error for IncludedRangesError {}
1939+
impl error::Error for LanguageError {}
1940+
impl error::Error for QueryError {}
1941+
19111942
unsafe impl Send for Language {}
19121943
unsafe impl Send for Parser {}
19131944
unsafe impl Send for Query {}

0 commit comments

Comments
 (0)