use crate::value::Value;
use alloc::string::ToString as _;
use super::common;
use super::evaluator::RbacBuiltinError;
#[cfg(feature = "time")]
use chrono::DateTime;
pub(super) fn datetime_compare(
left: &Value,
right: &Value,
op: &str,
name: &'static str,
) -> Result<bool, RbacBuiltinError> {
let lhs = common::value_as_string(left, name)?;
let rhs = common::value_as_string(right, name)?;
#[cfg(feature = "time")]
{
let left_dt = DateTime::parse_from_rfc3339(&lhs)
.map_err(|err| RbacBuiltinError::new(err.to_string()))?;
let right_dt = DateTime::parse_from_rfc3339(&rhs)
.map_err(|err| RbacBuiltinError::new(err.to_string()))?;
Ok(match op {
"==" => left_dt == right_dt,
"<" => left_dt < right_dt,
"<=" => left_dt <= right_dt,
">" => left_dt > right_dt,
">=" => left_dt >= right_dt,
_ => false,
})
}
#[cfg(not(feature = "time"))]
{
Ok(match op {
"==" => lhs == rhs,
"<" => lhs < rhs,
"<=" => lhs <= rhs,
">" => lhs > rhs,
">=" => lhs >= rhs,
_ => false,
})
}
}