Make on_print/on_debug Option.

This commit is contained in:
Stephen Chung 2021-06-29 17:42:03 +08:00
parent 8f4a582f88
commit 27c126d2f0
3 changed files with 36 additions and 29 deletions

View File

@ -850,9 +850,9 @@ pub struct Engine {
pub(crate) resolve_var: Option<OnVarCallback>,
/// Callback closure for implementing the `print` command.
pub(crate) print: OnPrintCallback,
pub(crate) print: Option<OnPrintCallback>,
/// Callback closure for implementing the `debug` command.
pub(crate) debug: OnDebugCallback,
pub(crate) debug: Option<OnDebugCallback>,
/// Callback closure for progress reporting.
#[cfg(not(feature = "unchecked"))]
pub(crate) progress: Option<crate::fn_native::OnProgressCallback>,
@ -954,8 +954,8 @@ impl Engine {
resolve_var: None,
// default print/debug implementations
print: Box::new(default_print),
debug: Box::new(default_debug),
print: Some(Box::new(default_print)),
debug: Some(Box::new(default_debug)),
// progress callback
#[cfg(not(feature = "unchecked"))]
@ -1010,8 +1010,8 @@ impl Engine {
resolve_var: None,
print: Box::new(|_| {}),
debug: Box::new(|_, _, _| {}),
print: None,
debug: None,
#[cfg(not(feature = "unchecked"))]
progress: None,
@ -2755,10 +2755,9 @@ impl Engine {
err_map.insert("message".into(), err.to_string().into());
state
.source
.as_ref()
.map(|source| err_map.insert("source".into(), source.into()));
if let Some(ref source) = state.source {
err_map.insert("source".into(), source.as_str().into());
}
if err_pos.is_none() {
// No position info

View File

@ -2219,7 +2219,7 @@ impl Engine {
/// ```
#[inline(always)]
pub fn on_print(&mut self, callback: impl Fn(&str) + SendSync + 'static) -> &mut Self {
self.print = Box::new(callback);
self.print = Some(Box::new(callback));
self
}
/// Override default action of `debug` (print to stdout using [`println!`])
@ -2258,7 +2258,7 @@ impl Engine {
&mut self,
callback: impl Fn(&str, Option<&str>, Position) + SendSync + 'static,
) -> &mut Self {
self.debug = Box::new(callback);
self.debug = Some(Box::new(callback));
self
}
}

View File

@ -319,6 +319,7 @@ impl Engine {
// See if the function match print/debug (which requires special processing)
return Ok(match name {
KEYWORD_PRINT => {
if let Some(ref print) = self.print {
let text = result.as_immutable_string().map_err(|typ| {
EvalAltResult::ErrorMismatchOutputType(
self.map_type_name(type_name::<ImmutableString>()).into(),
@ -326,9 +327,13 @@ impl Engine {
pos,
)
})?;
((self.print)(&text).into(), false)
(print(&text).into(), false)
} else {
(Dynamic::UNIT, false)
}
}
KEYWORD_DEBUG => {
if let Some(ref debug) = self.debug {
let text = result.as_immutable_string().map_err(|typ| {
EvalAltResult::ErrorMismatchOutputType(
self.map_type_name(type_name::<ImmutableString>()).into(),
@ -337,7 +342,10 @@ impl Engine {
)
})?;
let source = state.source.as_ref().map(|s| s.as_str());
((self.debug)(&text, source, pos).into(), false)
(debug(&text, source, pos).into(), false)
} else {
(Dynamic::UNIT, false)
}
}
_ => (result, func.is_method()),
});