From 58aca53144493747d80be80b1fa76a6bd993f016 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 29 Aug 2023 22:07:23 -0400 Subject: [PATCH] Move get_property into util. --- src/compare/diff.rs | 23 +++-------------------- src/compare/util.rs | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/compare/diff.rs b/src/compare/diff.rs index f4f9450..f7b631e 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -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>, Box> { - 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>, diff --git a/src/compare/util.rs b/src/compare/util.rs index 03c8e9b..7ef432a 100644 --- a/src/compare/util.rs +++ b/src/compare/util.rs @@ -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>, Box> { + 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)) +}