From 33f4614d2884765c018bba11e8487e1342efa373 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 31 Oct 2023 21:59:58 -0400 Subject: [PATCH] Make get_rust_byte_offsets more generic so it can be used for contents. --- src/compare/util.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/compare/util.rs b/src/compare/util.rs index 61eb97f..91ccaea 100644 --- a/src/compare/util.rs +++ b/src/compare/util.rs @@ -29,14 +29,10 @@ fn is_slice_of(parent: &str, child: &str) -> bool { /// Get the byte offset into source that the rust object exists at. /// /// These offsets are zero-based unlike the elisp ones. -fn get_rust_byte_offsets<'b, 's, S: StandardProperties<'s> + ?Sized>( - original_document: &'s str, - rust_ast_node: &'b S, -) -> (usize, usize) { - let rust_object_source = rust_ast_node.get_source(); - debug_assert!(is_slice_of(original_document, rust_object_source)); - let offset = rust_object_source.as_ptr() as usize - original_document.as_ptr() as usize; - let end = offset + rust_object_source.len(); +fn get_rust_byte_offsets<'b, 's>(original_document: &'s str, subset: &'b str) -> (usize, usize) { + debug_assert!(is_slice_of(original_document, subset)); + let offset = subset.as_ptr() as usize - original_document.as_ptr() as usize; + let end = offset + subset.len(); (offset, end) } @@ -54,7 +50,7 @@ pub(crate) fn compare_standard_properties< Ok(()) } -pub(crate) fn assert_name>( +fn assert_name>( emacs: &Token<'_>, name: S, ) -> Result<(), Box> { @@ -77,7 +73,7 @@ pub(crate) fn assert_name>( /// Assert that the character ranges defined by upstream org-mode's :standard-properties match the slices in Organic's StandardProperties. /// /// This does **not** handle plain text because plain text is a special case. -pub(crate) fn assert_bounds<'b, 's, S: StandardProperties<'s> + ?Sized>( +fn assert_bounds<'b, 's, S: StandardProperties<'s> + ?Sized>( original_document: &'s str, emacs: &'b Token<'s>, rust: &'b S, @@ -89,7 +85,7 @@ pub(crate) fn assert_bounds<'b, 's, S: StandardProperties<'s> + ?Sized>( .ok_or("Token should have a begin.")?, standard_properties.end.ok_or("Token should have an end.")?, ); - let (rust_begin, rust_end) = get_rust_byte_offsets(original_document, rust); // 0-based + let (rust_begin, rust_end) = get_rust_byte_offsets(original_document, rust.get_source()); // 0-based let rust_begin_char_offset = original_document[..rust_begin].chars().count() + 1; // 1-based let rust_end_char_offset = rust_begin_char_offset + original_document[rust_begin..rust_end].chars().count(); // 1-based