xsd-schema 0.1.0

XML Schema (XSD 1.0/1.1) validator with PSVI and a built-in XPath 2.0 engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
//! Extensible function support for XPath 2.0.
//!
//! This module provides traits and types for extending the XPath function library
//! with custom user-defined functions.
//!
//! ## Architecture
//!
//! The extensibility system uses two main traits:
//! - `FunctionCatalog` - Bind-time function lookup by namespace/name/arity
//! - `FunctionEvaluator` - Eval-time function dispatch
//!
//! The `FunctionHandle` type provides an opaque reference to functions that works
//! for both built-in and custom functions.
//!
//! ## Usage
//!
//! For most use cases, the default `BuiltinCatalog` and `BuiltinEvaluator` provide
//! access to all standard XPath 2.0 functions. To add custom functions, use `FunctionSet`:
//!
//! ```text
//! let mut functions = FunctionSet::with_builtins();
//! functions.register(
//!     DynamicFunctionSignature { ... },
//!     |ctx, args| { /* implementation */ }
//! );
//! ```

use std::sync::Arc;

use super::signature::{FunctionArity, FunctionSignature};
use super::{eval_function, FunctionId, XPathValue, FUNCTION_REGISTRY};
use crate::types::sequence::SequenceType;
use crate::xpath::context::DynamicContext;
use crate::xpath::error::XPathError;
use crate::xpath::DomNavigator;

// ============================================================================
// FunctionHandle - Opaque identifier for function dispatch
// ============================================================================

/// Base value for custom function handles (built-in handles use FunctionId values).
const CUSTOM_HANDLE_BASE: u32 = 0x1000_0000;

/// Opaque handle for function dispatch.
///
/// Replaces `FunctionId` in the AST to support both built-in and custom functions.
/// Built-in functions use handles with values matching their `FunctionId` discriminant.
/// Custom functions use handles starting at `CUSTOM_HANDLE_BASE`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FunctionHandle(pub(crate) u32);

impl FunctionHandle {
    /// Check if this handle refers to a built-in function.
    #[inline]
    pub fn is_builtin(&self) -> bool {
        self.0 < CUSTOM_HANDLE_BASE
    }

    /// Check if this handle refers to a custom function.
    #[inline]
    pub fn is_custom(&self) -> bool {
        self.0 >= CUSTOM_HANDLE_BASE
    }

    /// Get the custom function index (only valid for custom handles).
    #[inline]
    pub(crate) fn custom_index(&self) -> Option<usize> {
        if self.is_custom() {
            Some((self.0 - CUSTOM_HANDLE_BASE) as usize)
        } else {
            None
        }
    }
}

impl From<FunctionId> for FunctionHandle {
    fn from(id: FunctionId) -> Self {
        FunctionHandle(id as u32)
    }
}

// ============================================================================
// DynamicFunctionSignature - Owned signature for external registration
// ============================================================================

/// Function signature with owned strings for external registration.
///
/// Unlike `FunctionSignature` which uses `&'static str` for built-in functions,
/// this type owns its strings, allowing dynamic registration of custom functions.
#[derive(Debug, Clone)]
pub struct DynamicFunctionSignature {
    /// The function namespace URI.
    pub namespace: Arc<str>,
    /// The local name of the function.
    pub local_name: Arc<str>,
    /// The arity specification.
    pub arity: FunctionArity,
    /// Parameter types (may be shorter than actual args for variadic functions).
    pub param_types: Vec<SequenceType>,
    /// Return type.
    pub return_type: SequenceType,
}

impl DynamicFunctionSignature {
    /// Create a new dynamic signature with exact arity.
    pub fn new(
        namespace: impl Into<Arc<str>>,
        local_name: impl Into<Arc<str>>,
        param_types: Vec<SequenceType>,
        return_type: SequenceType,
    ) -> Self {
        let arity = FunctionArity::Exact(param_types.len());
        Self {
            namespace: namespace.into(),
            local_name: local_name.into(),
            arity,
            param_types,
            return_type,
        }
    }

    /// Create a variadic function signature.
    pub fn variadic(
        namespace: impl Into<Arc<str>>,
        local_name: impl Into<Arc<str>>,
        min_args: usize,
        param_types: Vec<SequenceType>,
        return_type: SequenceType,
    ) -> Self {
        Self {
            namespace: namespace.into(),
            local_name: local_name.into(),
            arity: FunctionArity::Variadic(min_args),
            param_types,
            return_type,
        }
    }

    /// Create a function signature with range arity.
    pub fn range(
        namespace: impl Into<Arc<str>>,
        local_name: impl Into<Arc<str>>,
        min_args: usize,
        max_args: usize,
        param_types: Vec<SequenceType>,
        return_type: SequenceType,
    ) -> Self {
        Self {
            namespace: namespace.into(),
            local_name: local_name.into(),
            arity: FunctionArity::Range(min_args, max_args),
            param_types,
            return_type,
        }
    }

    /// Check if this signature matches the given arity.
    pub fn matches_arity(&self, count: usize) -> bool {
        self.arity.matches(count)
    }
}

impl From<&FunctionSignature> for DynamicFunctionSignature {
    fn from(sig: &FunctionSignature) -> Self {
        Self {
            namespace: Arc::from(sig.namespace),
            local_name: Arc::from(sig.local_name),
            arity: sig.arity,
            param_types: sig.param_types.clone(),
            return_type: sig.return_type.clone(),
        }
    }
}

// ============================================================================
// FunctionCatalog - Bind-time function lookup trait
// ============================================================================

/// Trait for bind-time function lookup.
///
/// Implementors provide function resolution by namespace, local name, and arity.
/// The returned `FunctionHandle` is stored in the AST for later evaluation.
pub trait FunctionCatalog: std::fmt::Debug {
    /// Look up a function by namespace URI, local name, and arity.
    ///
    /// Returns `Some(handle)` if a matching function is found, `None` otherwise.
    fn lookup(&self, namespace: &str, local_name: &str, arity: usize) -> Option<FunctionHandle>;

    /// Get the signature for a function handle.
    ///
    /// Returns `None` if the handle is invalid.
    fn get_signature(&self, handle: FunctionHandle) -> Option<DynamicFunctionSignature>;
}

// ============================================================================
// FunctionEvaluator - Eval-time function dispatch trait
// ============================================================================

/// Trait for eval-time function dispatch.
///
/// Implementors execute functions identified by `FunctionHandle`.
pub trait FunctionEvaluator<N: DomNavigator> {
    /// Evaluate a function with the given arguments.
    ///
    /// # Arguments
    /// * `handle` - The function handle from bind-time lookup
    /// * `ctx` - The dynamic evaluation context
    /// * `args` - The evaluated argument values
    ///
    /// # Errors
    /// Returns an error if the function execution fails.
    fn eval(
        &self,
        handle: FunctionHandle,
        ctx: &mut DynamicContext<'_, N>,
        args: Vec<XPathValue<N>>,
    ) -> Result<XPathValue<N>, XPathError>;
}

// ============================================================================
// BuiltinCatalog - Static wrapper for FUNCTION_REGISTRY
// ============================================================================

/// Catalog wrapper for the static `FUNCTION_REGISTRY`.
///
/// Provides `FunctionCatalog` implementation using only built-in XPath functions.
/// This is the default catalog when no custom functions are registered.
#[derive(Debug, Clone, Copy, Default)]
pub struct BuiltinCatalog;

impl FunctionCatalog for BuiltinCatalog {
    fn lookup(&self, namespace: &str, local_name: &str, arity: usize) -> Option<FunctionHandle> {
        FUNCTION_REGISTRY
            .lookup(namespace, local_name, arity)
            .map(|entry| FunctionHandle::from(entry.id))
    }

    fn get_signature(&self, handle: FunctionHandle) -> Option<DynamicFunctionSignature> {
        if !handle.is_builtin() {
            return None;
        }
        // Convert handle back to FunctionId and look up
        FUNCTION_REGISTRY
            .by_id(handle_to_function_id(handle).ok()?)
            .map(|entry| DynamicFunctionSignature::from(&entry.signature))
    }
}

// ============================================================================
// BuiltinEvaluator - Static wrapper for eval_function
// ============================================================================

/// Evaluator wrapper for the static `eval_function` dispatch.
///
/// Provides `FunctionEvaluator` implementation using only built-in XPath functions.
/// This is the default evaluator when no custom functions are registered.
#[derive(Debug, Clone, Copy, Default)]
pub struct BuiltinEvaluator;

impl<N: DomNavigator> FunctionEvaluator<N> for BuiltinEvaluator {
    fn eval(
        &self,
        handle: FunctionHandle,
        ctx: &mut DynamicContext<'_, N>,
        args: Vec<XPathValue<N>>,
    ) -> Result<XPathValue<N>, XPathError> {
        let id = handle_to_function_id(handle)?;
        eval_function(id, ctx, args)
    }
}

// ============================================================================
// Helper functions
// ============================================================================

/// Convert a FunctionHandle back to a FunctionId.
///
/// Returns an error if the handle is not a valid built-in function handle.
pub(crate) fn handle_to_function_id(handle: FunctionHandle) -> Result<FunctionId, XPathError> {
    if !handle.is_builtin() {
        return Err(XPathError::Internal(format!(
            "Cannot convert custom handle {:?} to FunctionId",
            handle
        )));
    }

    // The handle value is the FunctionId discriminant
    // We need to find the matching FunctionId
    let value = handle.0 as u16;

    // Use the registry to find the function with matching ID
    // This is safe because we only create builtin handles from FunctionIds
    let entry = FUNCTION_REGISTRY
        .by_id_value(value)
        .ok_or_else(|| XPathError::Internal(format!("Invalid function handle: {}", value)))?;

    Ok(entry.id)
}

// ============================================================================
// FunctionSet - Combined catalog and evaluator with custom function support
// ============================================================================

use std::collections::HashMap;

/// Type alias for custom function implementation.
///
/// Custom functions receive the dynamic context and evaluated arguments,
/// and return an XPath value or error.
pub type CustomFn<N> = Arc<
    dyn Fn(&mut DynamicContext<'_, N>, Vec<XPathValue<N>>) -> Result<XPathValue<N>, XPathError>
        + Send
        + Sync,
>;

/// Entry for a custom function in FunctionSet.
struct CustomFunctionEntry<N: DomNavigator> {
    signature: DynamicFunctionSignature,
    implementation: CustomFn<N>,
}

/// 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
///
/// ```text
/// 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"))
/// });
/// ```
pub struct FunctionSet<N: DomNavigator> {
    /// Custom function entries (indexed by custom handle offset)
    custom_functions: Vec<CustomFunctionEntry<N>>,
    /// Lookup map: (namespace, local_name, arity) -> handle
    lookup: HashMap<(Arc<str>, Arc<str>, usize), FunctionHandle>,
    /// Variadic lookup: (namespace, local_name) -> (handle, min_arity)
    variadic_lookup: HashMap<(Arc<str>, Arc<str>), (FunctionHandle, usize)>,
}

impl<N: DomNavigator> FunctionSet<N> {
    /// Create an empty function set with no built-in functions.
    ///
    /// Use `with_builtins()` to include standard XPath 2.0 functions.
    pub fn new() -> Self {
        Self {
            custom_functions: Vec::new(),
            lookup: HashMap::new(),
            variadic_lookup: HashMap::new(),
        }
    }

    /// 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.
    pub fn with_builtins() -> Self {
        // We don't need to populate the lookup maps with builtins;
        // we fall back to FUNCTION_REGISTRY for those.
        Self::new()
    }

    /// 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
    ///
    /// ```text
    /// 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))
    /// });
    /// ```
    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,
    {
        let index = self.custom_functions.len();
        let handle = FunctionHandle(CUSTOM_HANDLE_BASE + index as u32);

        // Register in lookup maps based on arity
        let ns = signature.namespace.clone();
        let local = signature.local_name.clone();

        match signature.arity {
            FunctionArity::Exact(n) => {
                self.lookup.insert((ns.clone(), local.clone(), n), handle);
            }
            FunctionArity::Range(min, max) => {
                for arity in min..=max {
                    self.lookup
                        .insert((ns.clone(), local.clone(), arity), handle);
                }
            }
            FunctionArity::Variadic(min) => {
                self.variadic_lookup
                    .insert((ns.clone(), local.clone()), (handle, min));
            }
        }

        // Store the entry
        self.custom_functions.push(CustomFunctionEntry {
            signature,
            implementation: Arc::new(implementation),
        });

        handle
    }

    /// Get the number of custom functions registered.
    pub fn custom_count(&self) -> usize {
        self.custom_functions.len()
    }

    /// Check if this set has any custom functions.
    pub fn has_custom_functions(&self) -> bool {
        !self.custom_functions.is_empty()
    }
}

impl<N: DomNavigator> Default for FunctionSet<N> {
    fn default() -> Self {
        Self::with_builtins()
    }
}

impl<N: DomNavigator> std::fmt::Debug for FunctionSet<N> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FunctionSet")
            .field("custom_count", &self.custom_functions.len())
            .field("lookup_count", &self.lookup.len())
            .finish()
    }
}

impl<N: DomNavigator> FunctionCatalog for FunctionSet<N> {
    fn lookup(&self, namespace: &str, local_name: &str, arity: usize) -> Option<FunctionHandle> {
        // First check custom functions (exact arity)
        let ns: Arc<str> = Arc::from(namespace);
        let local: Arc<str> = Arc::from(local_name);

        if let Some(&handle) = self.lookup.get(&(ns.clone(), local.clone(), arity)) {
            return Some(handle);
        }

        // Check variadic custom functions
        if let Some(&(handle, min_arity)) = self.variadic_lookup.get(&(ns.clone(), local.clone())) {
            if arity >= min_arity {
                return Some(handle);
            }
        }

        // Fall back to built-in registry
        FUNCTION_REGISTRY
            .lookup(namespace, local_name, arity)
            .map(|entry| FunctionHandle::from(entry.id))
    }

    fn get_signature(&self, handle: FunctionHandle) -> Option<DynamicFunctionSignature> {
        if let Some(index) = handle.custom_index() {
            // Custom function
            self.custom_functions
                .get(index)
                .map(|e| e.signature.clone())
        } else {
            // Built-in function
            FUNCTION_REGISTRY
                .by_id(handle_to_function_id(handle).ok()?)
                .map(|entry| DynamicFunctionSignature::from(&entry.signature))
        }
    }
}

impl<N: DomNavigator> FunctionEvaluator<N> for FunctionSet<N> {
    fn eval(
        &self,
        handle: FunctionHandle,
        ctx: &mut DynamicContext<'_, N>,
        args: Vec<XPathValue<N>>,
    ) -> Result<XPathValue<N>, XPathError> {
        if let Some(index) = handle.custom_index() {
            // Custom function
            let entry = self.custom_functions.get(index).ok_or_else(|| {
                XPathError::Internal(format!("Invalid custom function handle: {:?}", handle))
            })?;
            (entry.implementation)(ctx, args)
        } else {
            // Built-in function
            let id = handle_to_function_id(handle)?;
            eval_function(id, ctx, args)
        }
    }
}

// ============================================================================
// XPath10Catalog - Function catalog restricting to XPath 1.0 core functions
// ============================================================================

/// Static list of XPath 1.0 core function names (27 functions).
const XPATH10_FUNCTIONS: &[&str] = &[
    "last",
    "position",
    "count",
    "id",
    "name",
    "local-name",
    "namespace-uri",
    "lang",
    "string",
    "concat",
    "starts-with",
    "contains",
    "substring-before",
    "substring-after",
    "substring",
    "string-length",
    "normalize-space",
    "translate",
    "boolean",
    "not",
    "true",
    "false",
    "number",
    "sum",
    "floor",
    "ceiling",
    "round",
];

/// Catalog that restricts available functions to the XPath 1.0 core set.
///
/// For empty-namespace lookups (XPath 1.0 mode), only the 27 core functions
/// are allowed, and they are resolved via `FUNCTION_REGISTRY` using `FN_NAMESPACE`.
/// For non-empty namespace lookups, delegates to `BuiltinCatalog` unchanged.
#[derive(Debug, Clone, Copy, Default)]
pub struct XPath10Catalog;

impl FunctionCatalog for XPath10Catalog {
    fn lookup(&self, namespace: &str, local_name: &str, arity: usize) -> Option<FunctionHandle> {
        if namespace.is_empty() {
            // XPath 1.0 mode: only allow core 1.0 functions, resolve via FN_NAMESPACE
            if XPATH10_FUNCTIONS.contains(&local_name) {
                FUNCTION_REGISTRY
                    .lookup(super::signature::FN_NAMESPACE, local_name, arity)
                    .map(|entry| FunctionHandle::from(entry.id))
            } else {
                None
            }
        } else {
            // Non-empty namespace: delegate to builtin catalog
            BuiltinCatalog.lookup(namespace, local_name, arity)
        }
    }

    fn get_signature(&self, handle: FunctionHandle) -> Option<DynamicFunctionSignature> {
        BuiltinCatalog.get_signature(handle)
    }
}

// ============================================================================
// XPath10Evaluator - Evaluator applying XPath 1.0 semantics
// ============================================================================

/// Convert an XPathValue result to double if it's an integer.
///
/// XPath 1.0 has no integer type — all numeric results are doubles.
fn wrap_as_double<N: DomNavigator>(result: XPathValue<N>) -> XPathValue<N> {
    // Check integer first — as_f64() also succeeds for integers via conversion
    if let Some(i) = result.as_integer() {
        return XPathValue::double(i.to_string().parse::<f64>().unwrap_or(f64::NAN));
    }
    result
}

/// Evaluator that applies XPath 1.0 semantics to function results.
///
/// Intercepts specific functions to:
/// - Use first-node-string-value rule for `fn:string`
/// - Use first-node-to-number rule for `fn:number`
/// - Convert integer results to double for `count`, `string-length`, `last`, `position`
/// - Ensure `sum`, `floor`, `ceiling`, `round` return doubles
///
/// All other functions delegate to `BuiltinEvaluator` unchanged.
#[derive(Debug, Clone, Copy, Default)]
pub struct XPath10Evaluator;

impl<N: DomNavigator> FunctionEvaluator<N> for XPath10Evaluator {
    fn eval(
        &self,
        handle: FunctionHandle,
        ctx: &mut DynamicContext<'_, N>,
        args: Vec<XPathValue<N>>,
    ) -> Result<XPathValue<N>, XPathError> {
        let id = handle_to_function_id(handle)?;
        match id {
            // fn:string — use XPath 1.0 first-node rule
            FunctionId::String => {
                use crate::xpath::atomize;
                match args.len() {
                    0 => {
                        let item = ctx.require_context_item()?.clone();
                        let s = match item {
                            crate::xpath::iterator::XmlItem::Node(nav) => nav.value(),
                            crate::xpath::iterator::XmlItem::Atomic(v) => atomize::string_value(&v),
                        };
                        Ok(XPathValue::string(s))
                    }
                    1 => {
                        let s = atomize::to_string_10(&args[0]);
                        Ok(XPathValue::string(s))
                    }
                    _ => Err(XPathError::wrong_number_of_arguments(
                        "string",
                        1,
                        args.len(),
                    )),
                }
            }

            // fn:number — use XPath 1.0 first-node rule
            FunctionId::Number => {
                use crate::xpath::atomize;
                match args.len() {
                    0 => {
                        let item = ctx.require_context_item()?.clone();
                        let d = match item {
                            crate::xpath::iterator::XmlItem::Node(nav) => {
                                let s = nav.value();
                                s.trim().parse().unwrap_or(f64::NAN)
                            }
                            crate::xpath::iterator::XmlItem::Atomic(v) => atomize::to_number(&v),
                        };
                        Ok(XPathValue::double(d))
                    }
                    1 => {
                        let d = atomize::to_number_10(&args[0]);
                        Ok(XPathValue::double(d))
                    }
                    _ => Err(XPathError::wrong_number_of_arguments(
                        "number",
                        1,
                        args.len(),
                    )),
                }
            }

            // Functions that return integer in 2.0 but should return double in 1.0
            FunctionId::Count
            | FunctionId::StringLength
            | FunctionId::Last
            | FunctionId::Position => {
                let result = eval_function(id, ctx, args)?;
                Ok(wrap_as_double(result))
            }

            // Numeric functions — ensure double result in 1.0
            FunctionId::Sum | FunctionId::Floor | FunctionId::Ceiling | FunctionId::Round => {
                let result = eval_function(id, ctx, args)?;
                Ok(wrap_as_double(result))
            }

            // All other functions: delegate unchanged
            _ => eval_function(id, ctx, args),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::xpath::RoXmlNavigator;

    #[test]
    fn test_function_handle_from_id() {
        let handle = FunctionHandle::from(FunctionId::Count);
        assert!(handle.is_builtin());
        assert!(!handle.is_custom());
    }

    #[test]
    fn test_custom_handle() {
        let handle = FunctionHandle(CUSTOM_HANDLE_BASE);
        assert!(!handle.is_builtin());
        assert!(handle.is_custom());
        assert_eq!(handle.custom_index(), Some(0));

        let handle2 = FunctionHandle(CUSTOM_HANDLE_BASE + 5);
        assert_eq!(handle2.custom_index(), Some(5));
    }

    #[test]
    fn test_builtin_catalog_lookup() {
        let catalog = BuiltinCatalog;
        let handle = catalog.lookup("http://www.w3.org/2005/xpath-functions", "count", 1);
        assert!(handle.is_some());
        assert!(handle.unwrap().is_builtin());
    }

    #[test]
    fn test_builtin_catalog_not_found() {
        let catalog = BuiltinCatalog;
        let handle = catalog.lookup("http://example.com", "my-func", 1);
        assert!(handle.is_none());
    }

    #[test]
    fn test_dynamic_signature_from_static() {
        let catalog = BuiltinCatalog;
        let handle = catalog
            .lookup("http://www.w3.org/2005/xpath-functions", "count", 1)
            .unwrap();
        let sig = catalog.get_signature(handle);
        assert!(sig.is_some());
        let sig = sig.unwrap();
        assert_eq!(&*sig.local_name, "count");
        assert_eq!(sig.arity, FunctionArity::Exact(1));
    }

    // ========================================================================
    // FunctionSet tests
    // ========================================================================

    #[test]
    fn test_function_set_builtins_accessible() {
        let functions: FunctionSet<RoXmlNavigator<'static>> = FunctionSet::with_builtins();

        // Built-in function should be found
        let handle = functions.lookup("http://www.w3.org/2005/xpath-functions", "count", 1);
        assert!(handle.is_some());
        assert!(handle.unwrap().is_builtin());
    }

    #[test]
    fn test_function_set_register_custom() {
        let mut functions: FunctionSet<RoXmlNavigator<'static>> = FunctionSet::with_builtins();

        let sig = DynamicFunctionSignature::new(
            "http://example.com/ext",
            "my-func",
            vec![SequenceType::string()],
            SequenceType::string(),
        );

        let handle = functions.register(sig, |_ctx, _args| Ok(XPathValue::string("custom result")));

        assert!(handle.is_custom());
        assert_eq!(functions.custom_count(), 1);
    }

    #[test]
    fn test_function_set_lookup_custom() {
        let mut functions: FunctionSet<RoXmlNavigator<'static>> = FunctionSet::with_builtins();

        let sig = DynamicFunctionSignature::new(
            "http://example.com/ext",
            "my-upper",
            vec![SequenceType::string()],
            SequenceType::string(),
        );

        let registered_handle = functions.register(sig, |_ctx, mut args| {
            let s = super::super::atomize_to_string(args.remove(0))?;
            Ok(XPathValue::string(s.to_uppercase()))
        });

        // Lookup should find our custom function
        let found_handle = functions.lookup("http://example.com/ext", "my-upper", 1);
        assert!(found_handle.is_some());
        assert_eq!(found_handle.unwrap(), registered_handle);
        assert!(found_handle.unwrap().is_custom());
    }

    #[test]
    fn test_function_set_get_custom_signature() {
        let mut functions: FunctionSet<RoXmlNavigator<'static>> = FunctionSet::with_builtins();

        let sig = DynamicFunctionSignature::new(
            "http://example.com/ext",
            "test-func",
            vec![SequenceType::integer(), SequenceType::integer()],
            SequenceType::integer(),
        );

        let handle = functions.register(sig, |_ctx, _args| Ok(XPathValue::integer(42)));

        let retrieved_sig = functions.get_signature(handle);
        assert!(retrieved_sig.is_some());
        let retrieved_sig = retrieved_sig.unwrap();
        assert_eq!(&*retrieved_sig.namespace, "http://example.com/ext");
        assert_eq!(&*retrieved_sig.local_name, "test-func");
        assert_eq!(retrieved_sig.arity, FunctionArity::Exact(2));
    }

    #[test]
    fn test_function_set_custom_overrides_builtin() {
        let mut functions: FunctionSet<RoXmlNavigator<'static>> = FunctionSet::with_builtins();

        // Register a custom function with the same name as a builtin
        let sig = DynamicFunctionSignature::new(
            "http://www.w3.org/2005/xpath-functions",
            "count",
            vec![SequenceType::any()],
            SequenceType::integer(),
        );

        let custom_handle = functions.register(sig, |_ctx, _args| {
            // Custom count always returns 999
            Ok(XPathValue::integer(999))
        });

        // Lookup should now find the custom function
        let found_handle = functions.lookup("http://www.w3.org/2005/xpath-functions", "count", 1);
        assert!(found_handle.is_some());
        assert_eq!(found_handle.unwrap(), custom_handle);
        assert!(found_handle.unwrap().is_custom());
    }

    #[test]
    fn test_function_set_eval_custom() {
        use crate::namespace::table::NameTable;
        use crate::xpath::context::{DynamicContext, XPathContext};

        let mut functions: FunctionSet<RoXmlNavigator<'static>> = FunctionSet::with_builtins();

        // Register a simple doubling function
        let sig = DynamicFunctionSignature::new(
            "http://example.com/ext",
            "double",
            vec![SequenceType::double()],
            SequenceType::double(),
        );

        let handle = 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))
        });

        // Create a minimal context for evaluation
        let names = NameTable::new();
        let static_ctx = XPathContext::new(&names);
        let mut dyn_ctx: DynamicContext<'_, RoXmlNavigator<'static>> =
            DynamicContext::new(&static_ctx, 0);

        // Evaluate the custom function
        let args = vec![XPathValue::double(21.0)];
        let result = functions.eval(handle, &mut dyn_ctx, args).unwrap();

        assert_eq!(result.as_f64(), Some(42.0));
    }

    #[test]
    fn test_function_set_eval_builtin() {
        use crate::namespace::table::NameTable;
        use crate::types::value::XmlValue;
        use crate::xpath::context::{DynamicContext, XPathContext};
        use crate::xpath::iterator::XmlItem;

        let functions: FunctionSet<RoXmlNavigator<'static>> = FunctionSet::with_builtins();

        // Get handle for builtin count function
        let handle = functions
            .lookup("http://www.w3.org/2005/xpath-functions", "count", 1)
            .unwrap();

        // Create context
        let names = NameTable::new();
        let static_ctx = XPathContext::new(&names);
        let mut dyn_ctx: DynamicContext<'_, RoXmlNavigator<'static>> =
            DynamicContext::new(&static_ctx, 0);

        // Create a sequence of 3 items
        let items = vec![
            XmlItem::Atomic(XmlValue::integer(1.into())),
            XmlItem::Atomic(XmlValue::integer(2.into())),
            XmlItem::Atomic(XmlValue::integer(3.into())),
        ];
        let args = vec![XPathValue::from_sequence(items)];

        // Evaluate
        let result = functions.eval(handle, &mut dyn_ctx, args).unwrap();
        assert_eq!(
            result.as_integer().map(|i| i.to_string()),
            Some("3".to_string())
        );
    }

    #[test]
    fn test_function_set_range_arity() {
        let mut functions: FunctionSet<RoXmlNavigator<'static>> = FunctionSet::with_builtins();

        // Register a function with range arity (1-3 arguments)
        let sig = DynamicFunctionSignature::range(
            "http://example.com/ext",
            "multi",
            1,
            3,
            vec![
                SequenceType::string(),
                SequenceType::string(),
                SequenceType::string(),
            ],
            SequenceType::string(),
        );

        let handle =
            functions.register(sig, |_ctx, args| Ok(XPathValue::integer(args.len() as i64)));

        // Should match arities 1, 2, and 3
        assert_eq!(
            functions.lookup("http://example.com/ext", "multi", 1),
            Some(handle)
        );
        assert_eq!(
            functions.lookup("http://example.com/ext", "multi", 2),
            Some(handle)
        );
        assert_eq!(
            functions.lookup("http://example.com/ext", "multi", 3),
            Some(handle)
        );

        // Should not match arity 0 or 4
        assert!(functions
            .lookup("http://example.com/ext", "multi", 0)
            .is_none());
        assert!(functions
            .lookup("http://example.com/ext", "multi", 4)
            .is_none());
    }

    #[test]
    fn test_function_set_variadic() {
        let mut functions: FunctionSet<RoXmlNavigator<'static>> = FunctionSet::with_builtins();

        // Register a variadic function (min 2 arguments)
        let sig = DynamicFunctionSignature::variadic(
            "http://example.com/ext",
            "varargs",
            2,
            vec![SequenceType::any_atomic()],
            SequenceType::integer(),
        );

        let handle =
            functions.register(sig, |_ctx, args| Ok(XPathValue::integer(args.len() as i64)));

        // Should match arities >= 2
        assert_eq!(
            functions.lookup("http://example.com/ext", "varargs", 2),
            Some(handle)
        );
        assert_eq!(
            functions.lookup("http://example.com/ext", "varargs", 5),
            Some(handle)
        );
        assert_eq!(
            functions.lookup("http://example.com/ext", "varargs", 100),
            Some(handle)
        );

        // Should not match arities < 2
        assert!(functions
            .lookup("http://example.com/ext", "varargs", 0)
            .is_none());
        assert!(functions
            .lookup("http://example.com/ext", "varargs", 1)
            .is_none());
    }

    // ========================================================================
    // XPath10Catalog tests
    // ========================================================================

    #[test]
    fn test_xpath10_catalog_resolves_core_function() {
        let catalog = XPath10Catalog;
        // count is in the 1.0 core set — empty namespace should resolve
        let handle = catalog.lookup("", "count", 1);
        assert!(handle.is_some());
        assert!(handle.unwrap().is_builtin());
    }

    #[test]
    fn test_xpath10_catalog_rejects_non_core_function() {
        let catalog = XPath10Catalog;
        // deep-equal is XPath 2.0 only — should be rejected in empty namespace
        let handle = catalog.lookup("", "deep-equal", 2);
        assert!(handle.is_none());
    }

    #[test]
    fn test_xpath10_catalog_non_empty_ns_delegates() {
        let catalog = XPath10Catalog;
        // Non-empty namespace delegates to BuiltinCatalog unchanged
        let handle = catalog.lookup("http://www.w3.org/2005/xpath-functions", "count", 1);
        assert!(handle.is_some());
    }

    #[test]
    fn test_xpath10_catalog_all_core_functions() {
        let catalog = XPath10Catalog;
        // Verify all 27 core functions resolve
        let functions_with_arity: &[(&str, usize)] = &[
            ("last", 0),
            ("position", 0),
            ("count", 1),
            ("id", 1),
            ("name", 0),
            ("local-name", 0),
            ("namespace-uri", 0),
            ("lang", 1),
            ("string", 0),
            ("concat", 2),
            ("starts-with", 2),
            ("contains", 2),
            ("substring-before", 2),
            ("substring-after", 2),
            ("substring", 2),
            ("string-length", 0),
            ("normalize-space", 0),
            ("translate", 3),
            ("boolean", 1),
            ("not", 1),
            ("true", 0),
            ("false", 0),
            ("number", 0),
            ("sum", 1),
            ("floor", 1),
            ("ceiling", 1),
            ("round", 1),
        ];
        for (name, arity) in functions_with_arity {
            let handle = catalog.lookup("", name, *arity);
            assert!(
                handle.is_some(),
                "XPath 1.0 function '{}' with arity {} not found",
                name,
                arity
            );
        }
    }

    // ========================================================================
    // XPath10Evaluator tests
    // ========================================================================

    #[test]
    fn test_xpath10_evaluator_count_returns_double() {
        use crate::namespace::table::NameTable;
        use crate::types::value::XmlValue;
        use crate::xpath::context::{DynamicContext, XPathContext};
        use crate::xpath::iterator::XmlItem;

        let evaluator = XPath10Evaluator;

        let names = NameTable::new();
        let static_ctx = XPathContext::new(&names);
        let mut dyn_ctx: DynamicContext<'_, RoXmlNavigator<'static>> =
            DynamicContext::new(&static_ctx, 0);

        // Create a sequence of 3 items
        let items = vec![
            XmlItem::Atomic(XmlValue::integer(1.into())),
            XmlItem::Atomic(XmlValue::integer(2.into())),
            XmlItem::Atomic(XmlValue::integer(3.into())),
        ];
        let args = vec![XPathValue::from_sequence(items)];
        let handle = FunctionHandle::from(FunctionId::Count);

        let result = evaluator.eval(handle, &mut dyn_ctx, args).unwrap();
        // In XPath 1.0, count() should return double, not integer
        assert_eq!(result.as_f64(), Some(3.0));
        // Should NOT be an integer
        assert!(result.as_integer().is_none());
    }

    #[test]
    fn test_xpath10_evaluator_string_with_node() {
        let evaluator = XPath10Evaluator;

        let doc = roxmltree::Document::parse("<r><a>first</a><b>second</b></r>").unwrap();
        let mut nav_a = RoXmlNavigator::new(&doc);
        nav_a.move_to_first_child(); // <r>
        nav_a.move_to_first_child(); // <a>
        let mut nav_b = nav_a.clone();
        nav_b.move_to_next_sibling(); // <b>

        use crate::namespace::table::NameTable;
        use crate::xpath::context::{DynamicContext, XPathContext};
        use crate::xpath::iterator::XmlItem;

        let names = NameTable::new();
        let static_ctx = XPathContext::new(&names);
        let mut dyn_ctx = DynamicContext::new(&static_ctx, 0);

        // Sequence of two nodes — XPath 1.0 string() should use first node
        let node_seq = XPathValue::from_sequence(vec![XmlItem::Node(nav_a), XmlItem::Node(nav_b)]);
        let args = vec![node_seq];
        let handle = FunctionHandle::from(FunctionId::String);

        let result = evaluator.eval(handle, &mut dyn_ctx, args).unwrap();
        assert_eq!(result.as_str(), Some("first".to_string()));
    }
}