
This is to support returning lists of child results for properties that contain lists of ast nodes.
253 lines
8.9 KiB
Rust
253 lines
8.9 KiB
Rust
use std::fmt::Debug;
|
|
|
|
use super::diff::artificial_diff_scope;
|
|
use super::diff::compare_ast_node;
|
|
use super::diff::DiffEntry;
|
|
use super::diff::DiffStatus;
|
|
use super::sexp::unquote;
|
|
use super::sexp::Token;
|
|
use super::util::get_property;
|
|
use super::util::get_property_quoted_string;
|
|
use super::util::get_property_unquoted_atom;
|
|
use crate::types::AstNode;
|
|
|
|
#[derive(Debug)]
|
|
pub(crate) enum EmacsField<'s> {
|
|
Required(&'s str),
|
|
#[allow(dead_code)]
|
|
Optional(&'s str),
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub(crate) enum ComparePropertiesResult<'b, 's> {
|
|
NoChange,
|
|
/// Return when you want the status for "this" node to change (as opposed to collecting child status).
|
|
SelfChange(DiffStatus, Option<String>),
|
|
DiffEntry(DiffEntry<'b, 's>),
|
|
}
|
|
|
|
/// Do no comparison.
|
|
///
|
|
/// This is for when you want to acknowledge that a field exists in the emacs token, but you do not have any validation for it when using the compare_properties!() macro. Ideally, this should be kept to a minimum since this represents untested values.
|
|
pub(crate) fn compare_noop<'b, 's, 'x, R, RG>(
|
|
_source: &'s str,
|
|
_emacs: &'b Token<'s>,
|
|
_rust_node: R,
|
|
_emacs_field: &'x str,
|
|
_rust_value_getter: RG,
|
|
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error>> {
|
|
Ok(ComparePropertiesResult::NoChange)
|
|
}
|
|
|
|
/// Do no comparison.
|
|
///
|
|
/// This is for when you want to acknowledge that a field exists in the emacs token, but you do not have any validation for it when using the compare_properties!() macro. Ideally, this should be kept to a minimum since this represents untested values.
|
|
pub(crate) fn compare_identity() -> () {
|
|
()
|
|
}
|
|
|
|
/// Assert that the emacs value is always nil or absent.
|
|
///
|
|
/// This is usually used for fields which, in my testing, are always nil. Using this compare function instead of simply doing a compare_noop will enable us to be alerted when we finally come across an org-mode document that has a value other than nil for the property.
|
|
pub(crate) fn compare_property_always_nil<'b, 's, 'x, R, RG>(
|
|
_source: &'s str,
|
|
emacs: &'b Token<'s>,
|
|
_rust_node: R,
|
|
emacs_field: &'x str,
|
|
_rust_value_getter: RG,
|
|
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error>> {
|
|
let value = get_property(emacs, emacs_field)?;
|
|
if value.is_some() {
|
|
let this_status = DiffStatus::Bad;
|
|
let message = Some(format!(
|
|
"{} was expected to always be nil: {:?}",
|
|
emacs_field, value
|
|
));
|
|
Ok(ComparePropertiesResult::SelfChange(this_status, message))
|
|
} else {
|
|
Ok(ComparePropertiesResult::NoChange)
|
|
}
|
|
}
|
|
|
|
pub(crate) fn compare_property_quoted_string<
|
|
'b,
|
|
's,
|
|
'x,
|
|
R,
|
|
RV: AsRef<str> + std::fmt::Debug,
|
|
RG: Fn(R) -> Option<RV>,
|
|
>(
|
|
_source: &'s str,
|
|
emacs: &'b Token<'s>,
|
|
rust_node: R,
|
|
emacs_field: &'x str,
|
|
rust_value_getter: RG,
|
|
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error>> {
|
|
let value = get_property_quoted_string(emacs, emacs_field)?;
|
|
let rust_value = rust_value_getter(rust_node);
|
|
if rust_value.as_ref().map(|s| s.as_ref()) != value.as_ref().map(String::as_str) {
|
|
let this_status = DiffStatus::Bad;
|
|
let message = Some(format!(
|
|
"{} mismatch (emacs != rust) {:?} != {:?}",
|
|
emacs_field, value, rust_value
|
|
));
|
|
Ok(ComparePropertiesResult::SelfChange(this_status, message))
|
|
} else {
|
|
Ok(ComparePropertiesResult::NoChange)
|
|
}
|
|
}
|
|
|
|
pub(crate) fn compare_property_unquoted_atom<'b, 's, 'x, R, RG: Fn(R) -> Option<&'s str>>(
|
|
_source: &'s str,
|
|
emacs: &'b Token<'s>,
|
|
rust_node: R,
|
|
emacs_field: &'x str,
|
|
rust_value_getter: RG,
|
|
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error>> {
|
|
let value = get_property_unquoted_atom(emacs, emacs_field)?;
|
|
let rust_value = rust_value_getter(rust_node);
|
|
if rust_value != value {
|
|
let this_status = DiffStatus::Bad;
|
|
let message = Some(format!(
|
|
"{} mismatch (emacs != rust) {:?} != {:?}",
|
|
emacs_field, value, rust_value
|
|
));
|
|
Ok(ComparePropertiesResult::SelfChange(this_status, message))
|
|
} else {
|
|
Ok(ComparePropertiesResult::NoChange)
|
|
}
|
|
}
|
|
|
|
pub(crate) fn compare_property_list_of_quoted_string<
|
|
'b,
|
|
's,
|
|
'x,
|
|
R,
|
|
RV: AsRef<str> + std::fmt::Debug,
|
|
RI: Iterator<Item = RV>,
|
|
RG: Fn(R) -> Option<RI>,
|
|
>(
|
|
_source: &'s str,
|
|
emacs: &'b Token<'s>,
|
|
rust_node: R,
|
|
emacs_field: &'x str,
|
|
rust_value_getter: RG,
|
|
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error>> {
|
|
let value = get_property(emacs, emacs_field)?
|
|
.map(Token::as_list)
|
|
.map_or(Ok(None), |r| r.map(Some))?;
|
|
let rust_value = rust_value_getter(rust_node);
|
|
// TODO: Seems we are needlessly coverting to a vec here.
|
|
let rust_value: Option<Vec<RV>> = rust_value.map(|it| it.collect());
|
|
match (value, &rust_value) {
|
|
(None, None) => {}
|
|
(None, Some(_)) | (Some(_), None) => {
|
|
let this_status = DiffStatus::Bad;
|
|
let message = Some(format!(
|
|
"{} mismatch (emacs != rust) {:?} != {:?}",
|
|
emacs_field, value, rust_value
|
|
));
|
|
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
|
|
}
|
|
(Some(el), Some(rl)) if el.len() != rl.len() => {
|
|
let this_status = DiffStatus::Bad;
|
|
let message = Some(format!(
|
|
"{} mismatch (emacs != rust) {:?} != {:?}",
|
|
emacs_field, value, rust_value
|
|
));
|
|
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
|
|
}
|
|
(Some(el), Some(rl)) => {
|
|
for (e, r) in el.iter().zip(rl) {
|
|
let e = unquote(e.as_atom()?)?;
|
|
let r = r.as_ref();
|
|
if e != r {
|
|
let this_status = DiffStatus::Bad;
|
|
let message = Some(format!(
|
|
"{} mismatch (emacs != rust) {:?} != {:?}. Full list: {:?} != {:?}",
|
|
emacs_field, e, r, value, rust_value
|
|
));
|
|
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Ok(ComparePropertiesResult::NoChange)
|
|
}
|
|
|
|
pub(crate) fn compare_property_boolean<'b, 's, 'x, R, RG: Fn(R) -> bool>(
|
|
_source: &'s str,
|
|
emacs: &'b Token<'s>,
|
|
rust_node: R,
|
|
emacs_field: &'x str,
|
|
rust_value_getter: RG,
|
|
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error>> {
|
|
// get_property already converts nil to None.
|
|
let value = get_property(emacs, emacs_field)?.is_some();
|
|
let rust_value = rust_value_getter(rust_node);
|
|
if rust_value != value {
|
|
let this_status = DiffStatus::Bad;
|
|
let message = Some(format!(
|
|
"{} mismatch (emacs != rust) {:?} != {:?}",
|
|
emacs_field, value, rust_value
|
|
));
|
|
Ok(ComparePropertiesResult::SelfChange(this_status, message))
|
|
} else {
|
|
Ok(ComparePropertiesResult::NoChange)
|
|
}
|
|
}
|
|
|
|
pub(crate) fn compare_property_list_of_ast_nodes<
|
|
'b,
|
|
's,
|
|
'x: 'b + 's,
|
|
R,
|
|
RV: std::fmt::Debug,
|
|
RI: Iterator<Item = RV>,
|
|
RG: Fn(R) -> Option<RI>,
|
|
>(
|
|
source: &'s str,
|
|
emacs: &'b Token<'s>,
|
|
rust_node: R,
|
|
emacs_field: &'x str,
|
|
rust_value_getter: RG,
|
|
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error>>
|
|
where
|
|
AstNode<'b, 's>: From<RV>,
|
|
{
|
|
let value = get_property(emacs, emacs_field)?
|
|
.map(Token::as_list)
|
|
.map_or(Ok(None), |r| r.map(Some))?;
|
|
let rust_value = rust_value_getter(rust_node);
|
|
// TODO: Seems we are needlessly coverting to a vec here.
|
|
let rust_value: Option<Vec<RV>> = rust_value.map(|it| it.collect());
|
|
match (value, rust_value) {
|
|
(None, None) => {}
|
|
(None, rv @ Some(_)) | (Some(_), rv @ None) => {
|
|
let this_status = DiffStatus::Bad;
|
|
let message = Some(format!(
|
|
"{} mismatch (emacs != rust) {:?} != {:?}",
|
|
emacs_field, value, rv
|
|
));
|
|
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
|
|
}
|
|
(Some(el), Some(rl)) if el.len() != rl.len() => {
|
|
let this_status = DiffStatus::Bad;
|
|
let message = Some(format!(
|
|
"{} mismatch (emacs != rust) {:?} != {:?}",
|
|
emacs_field, el, rl
|
|
));
|
|
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
|
|
}
|
|
(Some(el), Some(rl)) => {
|
|
let mut child_status: Vec<DiffEntry<'b, 's>> = Vec::with_capacity(rl.len());
|
|
for (e, r) in el.iter().zip(rl) {
|
|
child_status.push(compare_ast_node(source, e, r.into())?);
|
|
}
|
|
let diff_scope = artificial_diff_scope(emacs_field, child_status)?;
|
|
return Ok(ComparePropertiesResult::DiffEntry(diff_scope));
|
|
}
|
|
}
|
|
Ok(ComparePropertiesResult::NoChange)
|
|
}
|