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

View File

@ -172,9 +172,7 @@ fn maybe_token_to_usize(
/// Get a named property from the emacs token. /// Get a named property from the emacs token.
/// ///
/// Returns Ok(None) if value is nil. /// Returns Ok(None) if value is nil or absent.
///
/// Returns error if the attribute is not specified on the token at all.
pub(crate) fn get_property<'b, 's, 'x>( pub(crate) fn get_property<'b, 's, 'x>(
emacs: &'b Token<'s>, emacs: &'b Token<'s>,
key: &'x str, key: &'x str,
@ -185,14 +183,15 @@ pub(crate) fn get_property<'b, 's, 'x>(
.nth(1) .nth(1)
.ok_or("Should have an attributes child.")?; .ok_or("Should have an attributes child.")?;
let attributes_map = attributes_child.as_map()?; let attributes_map = attributes_child.as_map()?;
let prop = attributes_map let prop = attributes_map.get(key).map(|token| *token);
.get(key) match prop
.ok_or(format!("Missing {} attribute.", key))?; .map(|token| token.as_atom())
match prop.as_atom() { .map_or(Ok(None), |r| r.map(Some))?
Ok("nil") => return Ok(None), {
Some("nil") => return Ok(None),
_ => {} _ => {}
}; };
Ok(Some(*prop)) Ok(prop)
} }
/// Get a named property containing an unquoted atom from the emacs token. /// Get a named property containing an unquoted atom from the emacs token.