#!/usr/bin/env python3 """Patch UDL-generated Swift file for iOS 26 SDK compatibility and static linking.""" import sys, re f = sys.argv[1] with open(f) as fh: c = fh.read() # Patch 1: Fix Data.init for iOS 26 SDK (bytesNoCopy:count:deallocator: removed) c = c.replace( "fileprivate extension Data {\n init(rustBuffer: RustBuffer) {\n self.init(\n bytesNoCopy: rustBuffer.data!,\n count: Int(rustBuffer.len),\n deallocator: .none\n )\n }\n}", "fileprivate extension Data {\n init(rustBuffer: RustBuffer) {\n let buf = UnsafeRawBufferPointer(start: rustBuffer.data, count: Int(rustBuffer.len))\n self.init(buf)\n }\n}" ) # Patch 2: Fix UnsafeMutableRawPointer cast for iOS 26 SDK c = c.replace( "let bytes = UnsafeBufferPointer(start: value.data!, count: Int(value.len))", "let bytes = UnsafeBufferPointer(start: value.data!.assumingMemoryBound(to: UInt8.self), count: Int(value.len))" ) # Patch 3: Remove checksum initialization # UniFFI 0.28 UDL scaffolding does not export checksum functions as #[no_mangle], # so they cannot be called from Swift. Remove the runtime checksum verification. c = re.sub(r'private enum InitializationResult.*?// swiftlint:enable all', '// swiftlint:enable all', c, flags=re.DOTALL) c = re.sub(r'public func uniffiEnsureZxDocumentFfiInitialized\(\) \{.*?\n \}\n\}', '', c, flags=re.DOTALL) c = re.sub(r'private let initializationResult: InitializationResult.*?InitializationResult\.ok\n\}', '', c, flags=re.DOTALL) # Add a simple stub (called by rustCall helper, body removed above) c = c.replace( 'fileprivate extension RustBuffer {\n // Allocate a new buffer', 'private func uniffiEnsureZxDocumentFfiInitialized() {}\n\nfileprivate extension RustBuffer {\n // Allocate a new buffer' ) # Remove accidental duplicate stub if present c = c.replace( 'private func uniffiEnsureZxDocumentFfiInitialized() {}\n\nprivate func uniffiEnsureZxDocumentFfiInitialized() {}\n', 'private func uniffiEnsureZxDocumentFfiInitialized() {}\n' ) with open(f, 'w') as fh: fh.write(c) print(f" Patched: {len(c.split(chr(10)))} lines")