Move the Date struct into types and implement a get_property_numeric.

This commit is contained in:
Tom Alexander
2023-10-02 15:49:51 -04:00
parent 10ae36a419
commit e7ec23af3d
7 changed files with 203 additions and 112 deletions

View File

@@ -10,6 +10,7 @@ use super::sexp::Token;
use super::util::compare_standard_properties;
use super::util::get_property;
use super::util::get_property_boolean;
use super::util::get_property_numeric;
use super::util::get_property_quoted_string;
use super::util::get_property_unquoted_atom;
use crate::types::AngleLink;
@@ -2163,6 +2164,21 @@ fn compare_timestamp<'b, 's>(
));
}
// Compare start
let year_start: Option<u16> = get_property_unquoted_atom(emacs, ":year-start")?
.map(|val| val.parse())
.map_or(Ok(None), |r| r.map(Some))?;
let month_start: Option<u8> = get_property_unquoted_atom(emacs, ":month-start")?
.map(|val| val.parse())
.map_or(Ok(None), |r| r.map(Some))?;
let day_of_month_start: Option<u8> = get_property_unquoted_atom(emacs, ":day-start")?
.map(|val| val.parse())
.map_or(Ok(None), |r| r.map(Some))?;
let year_end = get_property_numeric::<u16>(emacs, ":year-end")?;
// Compare end
// TODO: Compare :year-start :month-start :day-start :hour-start :minute-start :year-end :month-end :day-end :hour-end :minute-end :repeater-type :repeater-value :repeater-unit :warning-type :warning-value :warning-unit
//
// :type unquoted atom either diary, active, inactive, active-range, or inactive-range.

View File

@@ -1,3 +1,5 @@
use std::str::FromStr;
use super::elisp_fact::GetElispFact;
use super::sexp::Token;
use crate::compare::sexp::unquote;
@@ -234,3 +236,23 @@ pub(crate) fn get_property_boolean<'b, 's, 'x>(
.unwrap_or("nil")
!= "nil")
}
/// Get a named property containing an unquoted numeric value.
///
/// Returns None if key is not found.
pub(crate) fn get_property_numeric<'b, 's, 'x, N: FromStr>(
emacs: &'b Token<'s>,
key: &'x str,
) -> Result<Option<N>, Box<dyn std::error::Error>>
where
<N as FromStr>::Err: std::error::Error,
<N as FromStr>::Err: 'static,
{
let foo = get_property(emacs, key)?
.map(Token::as_atom)
.map_or(Ok(None), |r| r.map(Some))?;
let bar = foo
.map(|val| val.parse::<N>())
.map_or(Ok(None), |r| r.map(Some))?;
Ok(bar)
}