use crate::ErrorKind;
use crate::item::{Node, Sequence, SequenceTrait};
use crate::transform::Transform;
use crate::transform::context::{Context, StaticContext};
use crate::xdmerror::Error;
use qualname::{NamespaceUri, NcName, QName};
use url::Url;
pub fn current<N: Node>(ctxt: &Context<N>) -> Result<Sequence<N>, Error> {
ctxt.current_item.as_ref().map_or_else(
|| {
ctxt.context_item.as_ref().map_or(
Err(Error::new(
ErrorKind::DynamicAbsent,
String::from("current item missing"),
)),
|c| Ok(vec![c.clone()]),
)
},
|c| Ok(vec![c.clone()]),
)
}
pub(crate) fn message<
N: Node,
F: FnMut(&str) -> Result<(), Error>,
G: FnMut(&str) -> Result<N, Error>,
H: FnMut(&Url) -> Result<String, Error>,
>(
ctxt: &Context<N>,
stctxt: &mut StaticContext<N, F, G, H>,
body: &Transform<N>,
_sel: &Option<Box<Transform<N>>>, _e: &Transform<N>, t: &Transform<N>, ) -> Result<Sequence<N>, Error> {
let msg = ctxt.dispatch(stctxt, body)?.to_string();
if let Some(f) = &mut stctxt.message {
f(msg.as_str())?
}
match ctxt.dispatch(stctxt, t)?.to_string().trim() {
"yes" => {
Err(Error {
kind: ErrorKind::Terminated,
message: msg,
code: Some(QName::new_from_parts(
NcName::try_from("XTMM9000").unwrap(),
Some(NamespaceUri::try_from("http://www.w3.org/2005/xqt-errors").unwrap()),
)),
})
}
_ => Ok(vec![]),
}
}