Change get_property to allow absent values.

We're returning an Option<> anyway so might as well handle absent values.
This commit is contained in:
Tom Alexander 2023-10-02 19:07:12 -04:00
parent 6a8ae9d838
commit 54c66fb4d6
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
1 changed files with 8 additions and 9 deletions

View File

@ -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.