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

View File

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

View File

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