PureXML/Sources/XPath/CoreFunctions.swift
Mihaela Mihaljevic 380160cc2b refactor(validation): decompose XSD instance validation into composable rules
The two monolithic XSD content/identity rules only satisfied the OpenAPIKit
validation idiom at the wrong granularity: no per-constraint removability,
no isolable failing/near-miss tests. Split them into 32 named rules, each
finding tagged at its origin and cached per XSDContext so the walk still
runs once per document (the #101 decision against duplicating XSD's
recursive type resolution still holds). Wired the streaming path's ID/IDREF
and identity checks through the same framework instead of calling the
validators imperatively, and added the unwrapAndLookup combinator the rule
already named as required.

Also includes the mandated repo-wide swiftformat/swiftlint pass.
2026-07-12 14:12:17 +02:00

38 lines
1.8 KiB
Swift

extension PureXML.XPath {
/// The core XPath functions tied to the four-type model and predicates:
/// `last`, `position`, `count`, `not`, `true`, `false`, `boolean`, `number`,
/// and `string`. The broader library (string, node, and number functions)
/// extends this table.
enum CoreFunctions {
// nonisolated(unsafe): an immutable, build-once table never mutated after
// initialization, so concurrent reads are safe; `FunctionTable` is only
// un-`Sendable` because it stores closures.
nonisolated(unsafe) static let table = FunctionTable([
"last": { _, context in .number(Double(context.size)) },
"position": { _, context in .number(Double(context.position)) },
"count": { arguments, _ in
guard let nodes = arguments.first?.nodes else {
throw PureXML.XPath.QueryError.invalidArguments("count() requires a node-set")
}
return .number(Double(nodes.count))
},
"not": { arguments, _ in .boolean(!(arguments.first?.boolean ?? false)) },
"true": { _, _ in .boolean(true) },
"false": { _, _ in .boolean(false) },
"boolean": { arguments, _ in .boolean(arguments.first?.boolean ?? false) },
"number": { arguments, context in
if let first = arguments.first {
return .number(first.number)
}
return .number(PureXML.XPath.Value.parseNumber(context.node.stringValue))
},
"string": { arguments, context in
if let first = arguments.first {
return .string(first.string)
}
return .string(context.node.stringValue)
},
])
}
}