Make on_print/on_debug Option.
This commit is contained in:
parent
8f4a582f88
commit
27c126d2f0
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -319,25 +319,33 @@ impl Engine {
|
||||
// See if the function match print/debug (which requires special processing)
|
||||
return Ok(match name {
|
||||
KEYWORD_PRINT => {
|
||||
let text = result.as_immutable_string().map_err(|typ| {
|
||||
EvalAltResult::ErrorMismatchOutputType(
|
||||
self.map_type_name(type_name::<ImmutableString>()).into(),
|
||||
typ.into(),
|
||||
pos,
|
||||
)
|
||||
})?;
|
||||
((self.print)(&text).into(), false)
|
||||
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(),
|
||||
typ.into(),
|
||||
pos,
|
||||
)
|
||||
})?;
|
||||
(print(&text).into(), false)
|
||||
} else {
|
||||
(Dynamic::UNIT, false)
|
||||
}
|
||||
}
|
||||
KEYWORD_DEBUG => {
|
||||
let text = result.as_immutable_string().map_err(|typ| {
|
||||
EvalAltResult::ErrorMismatchOutputType(
|
||||
self.map_type_name(type_name::<ImmutableString>()).into(),
|
||||
typ.into(),
|
||||
pos,
|
||||
)
|
||||
})?;
|
||||
let source = state.source.as_ref().map(|s| s.as_str());
|
||||
((self.debug)(&text, source, pos).into(), false)
|
||||
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(),
|
||||
typ.into(),
|
||||
pos,
|
||||
)
|
||||
})?;
|
||||
let source = state.source.as_ref().map(|s| s.as_str());
|
||||
(debug(&text, source, pos).into(), false)
|
||||
} else {
|
||||
(Dynamic::UNIT, false)
|
||||
}
|
||||
}
|
||||
_ => (result, func.is_method()),
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user