Skip to main content

FunctionSet

Struct FunctionSet 

Source
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>

Source

pub fn new() -> Self

Create an empty function set with no built-in functions.

Use with_builtins() to include standard XPath 2.0 functions.

Source

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.

Source

pub fn register<F>( &mut self, signature: DynamicFunctionSignature, implementation: F, ) -> FunctionHandle
where 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))
});
Source

pub fn custom_count(&self) -> usize

Get the number of custom functions registered.

Source

pub fn has_custom_functions(&self) -> bool

Check if this set has any custom functions.

Trait Implementations§

Source§

impl<N: DomNavigator> Debug for FunctionSet<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<N: DomNavigator> Default for FunctionSet<N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<N: DomNavigator> FunctionCatalog for FunctionSet<N>

Source§

fn lookup( &self, namespace: &str, local_name: &str, arity: usize, ) -> Option<FunctionHandle>

Look up a function by namespace URI, local name, and arity. Read more
Source§

fn get_signature( &self, handle: FunctionHandle, ) -> Option<DynamicFunctionSignature>

Get the signature for a function handle. Read more
Source§

impl<N: DomNavigator> FunctionEvaluator<N> for FunctionSet<N>

Source§

fn eval( &self, handle: FunctionHandle, ctx: &mut DynamicContext<'_, N>, args: Vec<XPathValue<N>>, ) -> Result<XPathValue<N>, XPathError>

Evaluate a function with the given arguments. Read more

Auto Trait Implementations§

§

impl<N> !RefUnwindSafe for FunctionSet<N>

§

impl<N> !UnwindSafe for FunctionSet<N>

§

impl<N> Freeze for FunctionSet<N>

§

impl<N> Send for FunctionSet<N>

§

impl<N> Sync for FunctionSet<N>

§

impl<N> Unpin for FunctionSet<N>

§

impl<N> UnsafeUnpin for FunctionSet<N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> MaybeSendSync for T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.