Add a utility function to check the first element in an sexp.

This commit is contained in:
Tom Alexander
2023-04-19 15:19:21 -04:00
parent 821eacf161
commit a4c6e899ac
2 changed files with 49 additions and 83 deletions

View File

@@ -1,5 +1,7 @@
use crate::parser::Source;
use super::sexp::Token;
/// Check if the child string slice is a slice of the parent string slice.
fn is_slice_of(parent: &str, child: &str) -> bool {
let parent_start = parent.as_ptr() as usize;
@@ -19,3 +21,19 @@ pub fn get_offsets<'s, S: Source<'s>>(source: &'s str, rust_object: &'s S) -> (u
let end = offset + rust_object_source.len();
(offset, end)
}
pub fn assert_name<'s>(emacs: &'s Token<'s>, name: &str) -> Result<(), Box<dyn std::error::Error>> {
let children = emacs.as_list()?;
let first_child = children
.first()
.ok_or("Should have at least one child.")?
.as_atom()?;
if first_child != name {
Err(format!(
"Expected a {expected} cell, but found a {found} cell.",
expected = name,
found = first_child
))?;
}
Ok(())
}