Move get_property into util.

This commit is contained in:
Tom Alexander 2023-08-29 22:07:23 -04:00
parent 6f2d90162b
commit 58aca53144
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 23 additions and 20 deletions

View File

@ -2,6 +2,7 @@ use std::collections::HashSet;
use super::util::assert_bounds;
use super::util::assert_name;
use super::util::get_property;
use crate::parser::sexp::unquote;
use crate::parser::sexp::Token;
use crate::parser::AngleLink;
@ -397,6 +398,8 @@ fn compare_heading<'s>(
Ok(_) => {}
};
// Compare level
// Compare tags
let emacs_tags = get_tags_from_heading(emacs)?;
let emacs_tags: HashSet<_> = emacs_tags.iter().map(|val| val.as_str()).collect();
@ -483,26 +486,6 @@ fn get_tags_from_heading<'s>(
Ok(tags)
}
fn get_property<'s, 'x>(
emacs: &'s Token<'s>,
key: &'x str,
) -> Result<Option<&'s Token<'s>>, Box<dyn std::error::Error>> {
let children = emacs.as_list()?;
let attributes_child = children
.iter()
.nth(1)
.ok_or("Should have an attributes child.")?;
let attributes_map = attributes_child.as_map()?;
let prop = attributes_map
.get(key)
.ok_or(format!("Missing {} attribute.", key))?;
match prop.as_atom() {
Ok("nil") => return Ok(None),
_ => {}
};
Ok(Some(*prop))
}
fn compare_paragraph<'s>(
source: &'s str,
emacs: &'s Token<'s>,

View File

@ -137,3 +137,23 @@ fn maybe_token_to_usize(
.flatten() // Outer option is whether or not the param exists, inner option is whether or not it is nil
.map_or(Ok(None), |r| r.map(Some))?)
}
pub fn get_property<'s, 'x>(
emacs: &'s Token<'s>,
key: &'x str,
) -> Result<Option<&'s Token<'s>>, Box<dyn std::error::Error>> {
let children = emacs.as_list()?;
let attributes_child = children
.iter()
.nth(1)
.ok_or("Should have an attributes child.")?;
let attributes_map = attributes_child.as_map()?;
let prop = attributes_map
.get(key)
.ok_or(format!("Missing {} attribute.", key))?;
match prop.as_atom() {
Ok("nil") => return Ok(None),
_ => {}
};
Ok(Some(*prop))
}