73 lines
2.4 KiB
Swift
73 lines
2.4 KiB
Swift
|
|
// Generated by alef. Do not edit by hand.
|
||
|
|
// swift-format-ignore-file
|
||
|
|
// This file contains generated FFI glue for trait bridge registration.
|
||
|
|
|
||
|
|
import Foundation
|
||
|
|
import RustBridge
|
||
|
|
|
||
|
|
/// Protocol for outbound `DocumentExtractor` implementations.
|
||
|
|
/// Conform your Swift class or struct to this protocol to implement
|
||
|
|
/// a Rust trait from the host side.
|
||
|
|
public protocol SwiftDocumentExtractorBridge: AnyObject {
|
||
|
|
func extractBytes(content: Data, mime_type: String, config: ExtractionConfig) throws -> String
|
||
|
|
func supportedMimeTypes() -> [String]
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Internal adapter wrapping a `SwiftDocumentExtractorBridge` conformer.
|
||
|
|
/// Marshals Swift types and trait calls to/from the C boundary.
|
||
|
|
/// Excluded/internal types are serialised to/from JSON strings.
|
||
|
|
final class SwiftDocumentExtractorAdapter {
|
||
|
|
private let bridge: any SwiftDocumentExtractorBridge
|
||
|
|
|
||
|
|
init(bridge: any SwiftDocumentExtractorBridge) {
|
||
|
|
self.bridge = bridge
|
||
|
|
}
|
||
|
|
|
||
|
|
func extractBytesCall(content: Data, mime_type: String, config: ExtractionConfig) throws -> String {
|
||
|
|
do {
|
||
|
|
let result = try self.bridge.extractBytes(content: content, mime_type: mime_type, config: config)
|
||
|
|
let encodedData = try marshal_encode_excluded(result)
|
||
|
|
if let jsonString = String(data: encodedData, encoding: .utf8) {
|
||
|
|
return "{\"ok\": \(jsonString)}"
|
||
|
|
}
|
||
|
|
return "{\"ok\": null}"
|
||
|
|
} catch {
|
||
|
|
return marshal_error_result(error)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func supportedMimeTypesCall() -> [String] {
|
||
|
|
let result = self.bridge.supportedMimeTypes()
|
||
|
|
return result
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
// MARK: - Marshalling helpers
|
||
|
|
|
||
|
|
private struct Empty: Codable {}
|
||
|
|
|
||
|
|
private func marshal_ok_result<T: Encodable>(_ value: T) -> String {
|
||
|
|
let encoder = JSONEncoder()
|
||
|
|
if let data = try? encoder.encode(value),
|
||
|
|
let jsonString = String(data: data, encoding: .utf8) {
|
||
|
|
return "{\"ok\": \(jsonString)}"
|
||
|
|
}
|
||
|
|
return "{\"ok\": null}"
|
||
|
|
}
|
||
|
|
|
||
|
|
private func marshal_encode_excluded<T: Encodable>(_ value: T) throws -> Data {
|
||
|
|
let encoder = JSONEncoder()
|
||
|
|
return try encoder.encode(value)
|
||
|
|
}
|
||
|
|
|
||
|
|
private func marshal_error_result(_ error: any Error) -> String {
|
||
|
|
let errorString = String(describing: error)
|
||
|
|
let encoder = JSONEncoder()
|
||
|
|
if let data = try? encoder.encode(errorString),
|
||
|
|
let jsonString = String(data: data, encoding: .utf8) {
|
||
|
|
return "{\"err\": \(jsonString)}"
|
||
|
|
}
|
||
|
|
return "{\"err\": \"unknown error\"}"
|
||
|
|
}
|