From 54c66fb4d6eb71b576da7ed9a0af4845e10c36be Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Mon, 2 Oct 2023 19:07:12 -0400 Subject: [PATCH] Change get_property to allow absent values. We're returning an Option<> anyway so might as well handle absent values. --- src/compare/util.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/compare/util.rs b/src/compare/util.rs index afaedf35..4c2578d6 100644 --- a/src/compare/util.rs +++ b/src/compare/util.rs @@ -172,9 +172,7 @@ fn maybe_token_to_usize( /// Get a named property from the emacs token. /// -/// Returns Ok(None) if value is nil. -/// -/// Returns error if the attribute is not specified on the token at all. +/// Returns Ok(None) if value is nil or absent. pub(crate) fn get_property<'b, 's, 'x>( emacs: &'b Token<'s>, key: &'x str, @@ -185,14 +183,15 @@ pub(crate) fn get_property<'b, 's, 'x>( .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), + let prop = attributes_map.get(key).map(|token| *token); + match prop + .map(|token| token.as_atom()) + .map_or(Ok(None), |r| r.map(Some))? + { + Some("nil") => return Ok(None), _ => {} }; - Ok(Some(*prop)) + Ok(prop) } /// Get a named property containing an unquoted atom from the emacs token.