pub struct FunctionSet<N: DomNavigator> { /* private fields */ }Expand description
A function set that combines built-in and custom functions.
FunctionSet<N> implements both FunctionCatalog and FunctionEvaluator<N>,
allowing users to register custom XPath functions alongside the built-in ones.
§Example
use xsd_schema::xpath::functions::{FunctionSet, DynamicFunctionSignature, XPathValue};
use xsd_schema::types::sequence::SequenceType;
let mut functions = FunctionSet::with_builtins();
// Register a custom function
let sig = DynamicFunctionSignature::new(
"http://example.com/ext",
"my-upper",
vec![SequenceType::string()],
SequenceType::string(),
);
functions.register(sig, |_ctx, mut args| {
let s = args.remove(0);
// ... implementation
Ok(XPathValue::string("RESULT"))
});Implementations§
Source§impl<N: DomNavigator> FunctionSet<N>
impl<N: DomNavigator> FunctionSet<N>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty function set with no built-in functions.
Use with_builtins() to include standard XPath 2.0 functions.
Sourcepub fn with_builtins() -> Self
pub fn with_builtins() -> Self
Create a function set with all built-in XPath 2.0 functions.
Built-in functions are looked up via the global FUNCTION_REGISTRY.
Custom functions registered with register() will take precedence
over built-in functions with the same signature.
Sourcepub fn register<F>(
&mut self,
signature: DynamicFunctionSignature,
implementation: F,
) -> FunctionHandlewhere
F: Fn(&mut DynamicContext<'_, N>, Vec<XPathValue<N>>) -> Result<XPathValue<N>, XPathError> + Send + Sync + 'static,
pub fn register<F>(
&mut self,
signature: DynamicFunctionSignature,
implementation: F,
) -> FunctionHandlewhere
F: Fn(&mut DynamicContext<'_, N>, Vec<XPathValue<N>>) -> Result<XPathValue<N>, XPathError> + Send + Sync + 'static,
Register a custom function.
The function will be available for lookup by its namespace, local name, and arity. If a function with the same signature already exists (either built-in or previously registered), the new function takes precedence.
Returns the FunctionHandle for the registered function.
§Example
let sig = DynamicFunctionSignature::new(
"http://example.com/ext",
"double",
vec![SequenceType::double()],
SequenceType::double(),
);
functions.register(sig, |_ctx, mut args| {
let val = args.remove(0);
let d = val.as_f64().unwrap_or(0.0);
Ok(XPathValue::double(d * 2.0))
});Sourcepub fn custom_count(&self) -> usize
pub fn custom_count(&self) -> usize
Get the number of custom functions registered.
Sourcepub fn has_custom_functions(&self) -> bool
pub fn has_custom_functions(&self) -> bool
Check if this set has any custom functions.