2023-10-06 14:07:25 -04:00
use std ::fmt ::Debug ;
2023-10-06 13:40:11 -04:00
use super ::diff ::DiffStatus ;
use super ::sexp ::Token ;
use super ::util ::get_property_quoted_string ;
#[ derive(Debug) ]
pub ( crate ) enum EmacsField < ' s > {
Required ( & ' s str ) ,
2023-10-06 14:07:25 -04:00
#[ allow(dead_code) ]
2023-10-06 13:40:11 -04:00
Optional ( & ' s str ) ,
}
2023-10-06 14:07:25 -04:00
/// 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.
#[ allow(dead_code) ]
pub ( crate ) fn compare_noop < ' b , ' s , ' x > (
_emacs : & ' b Token < ' s > ,
_emacs_field : & ' x str ,
_rust_value : ( ) ,
) -> Result < Option < ( DiffStatus , Option < String > ) > , Box < dyn std ::error ::Error > > {
Ok ( None )
}
2023-10-06 13:40:11 -04:00
pub ( crate ) fn compare_property_quoted_string_required_value < ' b , ' s , ' x > (
emacs : & ' b Token < ' s > ,
emacs_field : & ' x str ,
rust_value : & ' s str ,
) -> Result < Option < ( DiffStatus , Option < String > ) > , Box < dyn std ::error ::Error > > {
let value = get_property_quoted_string ( emacs , emacs_field ) ? ;
if value . as_ref ( ) . map ( String ::as_str ) ! = Some ( rust_value ) {
let this_status = DiffStatus ::Bad ;
let message = Some ( format! (
" {} mismatch (emacs != rust) {:?} != {:?} " ,
emacs_field , value , rust_value
) ) ;
Ok ( Some ( ( this_status , message ) ) )
} else {
Ok ( None )
}
}
2023-10-06 14:07:25 -04:00
pub ( crate ) fn compare_property_quoted_string <
' b ,
' s ,
' x ,
R ,
RV : Debug + for < ' a > PartialEq < Option < & ' a str > > ,
RG : Fn ( R ) -> RV ,
> (
emacs : & ' b Token < ' s > ,
emacs_field : & ' x str ,
rust_node : R ,
rust_value_getter : RG ,
) -> Result < Option < ( DiffStatus , Option < String > ) > , 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 . eq ( & 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 ( Some ( ( this_status , message ) ) )
} else {
Ok ( None )
}
}