@apollohg/react-native-rich-text-editor-code-highlighting 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/LICENSE +160 -0
  2. package/NativeEditorCodeHighlighting.podspec +24 -0
  3. package/README.md +47 -0
  4. package/RUST-STANDARD-LIBRARY-NOTICES.html +8266 -0
  5. package/THIRD_PARTY_NOTICES.md +18451 -0
  6. package/android/build.gradle +25 -0
  7. package/android/consumer-rules.pro +1 -0
  8. package/android/src/main/AndroidManifest.xml +1 -0
  9. package/android/src/main/java/com/apollohg/editor/highlighting/NativeCodeHighlightingModule.kt +17 -0
  10. package/android/src/main/java/com/apollohg/editor/highlighting/SyntectHighlightingProvider.kt +23 -0
  11. package/android/src/main/java/uniffi/native_editor_highlighting/native_editor_highlighting.kt +1259 -0
  12. package/android/src/main/jniLibs/arm64-v8a/libnative_editor_highlighting.so +0 -0
  13. package/android/src/main/jniLibs/armeabi-v7a/libnative_editor_highlighting.so +0 -0
  14. package/android/src/main/jniLibs/x86/libnative_editor_highlighting.so +0 -0
  15. package/android/src/main/jniLibs/x86_64/libnative_editor_highlighting.so +0 -0
  16. package/dist/index.d.ts +7 -0
  17. package/dist/index.js +25 -0
  18. package/expo-module.config.json +10 -0
  19. package/ios/Generated_highlighting.swift +728 -0
  20. package/ios/NativeCodeHighlightingModule.swift +14 -0
  21. package/ios/NativeEditorHighlighting.xcframework/Info.plist +44 -0
  22. package/ios/NativeEditorHighlighting.xcframework/ios-arm64/libnative_editor_highlighting.a +0 -0
  23. package/ios/NativeEditorHighlighting.xcframework/ios-arm64_x86_64-simulator/libnative_editor_highlighting.a +0 -0
  24. package/ios/SyntectHighlightingProvider.swift +12 -0
  25. package/ios/native_editor_highlightingFFI/module.modulemap +7 -0
  26. package/ios/native_editor_highlightingFFI/native_editor_highlightingFFI.h +551 -0
  27. package/package.json +49 -0
  28. package/scripts/verify-package.mjs +19 -0
  29. package/src/index.ts +41 -0
@@ -0,0 +1,728 @@
1
+ // This file was autogenerated by some hot garbage in the `uniffi` crate.
2
+ // Trust me, you don't want to mess with it!
3
+
4
+ // swiftlint:disable all
5
+ import Foundation
6
+
7
+ // Depending on the consumer's build setup, the low-level FFI code
8
+ // might be in a separate module, or it might be compiled inline into
9
+ // this module. This is a bit of light hackery to work with both.
10
+ #if canImport(native_editor_highlightingFFI)
11
+ import native_editor_highlightingFFI
12
+ #endif
13
+
14
+ fileprivate extension RustBuffer {
15
+ // Allocate a new buffer, copying the contents of a `UInt8` array.
16
+ init(bytes: [UInt8]) {
17
+ let rbuf = bytes.withUnsafeBufferPointer { ptr in
18
+ RustBuffer.from(ptr)
19
+ }
20
+ self.init(capacity: rbuf.capacity, len: rbuf.len, data: rbuf.data)
21
+ }
22
+
23
+ static func empty() -> RustBuffer {
24
+ RustBuffer(capacity: 0, len:0, data: nil)
25
+ }
26
+
27
+ static func from(_ ptr: UnsafeBufferPointer<UInt8>) -> RustBuffer {
28
+ try! rustCall { ffi_native_editor_highlighting_rustbuffer_from_bytes(ForeignBytes(bufferPointer: ptr), $0) }
29
+ }
30
+
31
+ // Frees the buffer in place.
32
+ // The buffer must not be used after this is called.
33
+ func deallocate() {
34
+ try! rustCall { ffi_native_editor_highlighting_rustbuffer_free(self, $0) }
35
+ }
36
+ }
37
+
38
+ fileprivate extension ForeignBytes {
39
+ init(bufferPointer: UnsafeBufferPointer<UInt8>) {
40
+ self.init(len: Int32(bufferPointer.count), data: bufferPointer.baseAddress)
41
+ }
42
+ }
43
+
44
+ // For every type used in the interface, we provide helper methods for conveniently
45
+ // lifting and lowering that type from C-compatible data, and for reading and writing
46
+ // values of that type in a buffer.
47
+
48
+ // Helper classes/extensions that don't change.
49
+ // Someday, this will be in a library of its own.
50
+
51
+ fileprivate extension Data {
52
+ init(rustBuffer: RustBuffer) {
53
+ self.init(
54
+ bytesNoCopy: rustBuffer.data!,
55
+ count: Int(rustBuffer.len),
56
+ deallocator: .none
57
+ )
58
+ }
59
+ }
60
+
61
+ // Define reader functionality. Normally this would be defined in a class or
62
+ // struct, but we use standalone functions instead in order to make external
63
+ // types work.
64
+ //
65
+ // With external types, one swift source file needs to be able to call the read
66
+ // method on another source file's FfiConverter, but then what visibility
67
+ // should Reader have?
68
+ // - If Reader is fileprivate, then this means the read() must also
69
+ // be fileprivate, which doesn't work with external types.
70
+ // - If Reader is internal/public, we'll get compile errors since both source
71
+ // files will try define the same type.
72
+ //
73
+ // Instead, the read() method and these helper functions input a tuple of data
74
+
75
+ fileprivate func createReader(data: Data) -> (data: Data, offset: Data.Index) {
76
+ (data: data, offset: 0)
77
+ }
78
+
79
+ // Reads an integer at the current offset, in big-endian order, and advances
80
+ // the offset on success. Throws if reading the integer would move the
81
+ // offset past the end of the buffer.
82
+ fileprivate func readInt<T: FixedWidthInteger>(_ reader: inout (data: Data, offset: Data.Index)) throws -> T {
83
+ let range = reader.offset..<reader.offset + MemoryLayout<T>.size
84
+ guard reader.data.count >= range.upperBound else {
85
+ throw UniffiInternalError.bufferOverflow
86
+ }
87
+ if T.self == UInt8.self {
88
+ let value = reader.data[reader.offset]
89
+ reader.offset += 1
90
+ return value as! T
91
+ }
92
+ var value: T = 0
93
+ let _ = withUnsafeMutableBytes(of: &value, { reader.data.copyBytes(to: $0, from: range)})
94
+ reader.offset = range.upperBound
95
+ return value.bigEndian
96
+ }
97
+
98
+ // Reads an arbitrary number of bytes, to be used to read
99
+ // raw bytes, this is useful when lifting strings
100
+ fileprivate func readBytes(_ reader: inout (data: Data, offset: Data.Index), count: Int) throws -> Array<UInt8> {
101
+ let range = reader.offset..<(reader.offset+count)
102
+ guard reader.data.count >= range.upperBound else {
103
+ throw UniffiInternalError.bufferOverflow
104
+ }
105
+ var value = [UInt8](repeating: 0, count: count)
106
+ value.withUnsafeMutableBufferPointer({ buffer in
107
+ reader.data.copyBytes(to: buffer, from: range)
108
+ })
109
+ reader.offset = range.upperBound
110
+ return value
111
+ }
112
+
113
+ // Reads a float at the current offset.
114
+ fileprivate func readFloat(_ reader: inout (data: Data, offset: Data.Index)) throws -> Float {
115
+ return Float(bitPattern: try readInt(&reader))
116
+ }
117
+
118
+ // Reads a float at the current offset.
119
+ fileprivate func readDouble(_ reader: inout (data: Data, offset: Data.Index)) throws -> Double {
120
+ return Double(bitPattern: try readInt(&reader))
121
+ }
122
+
123
+ // Indicates if the offset has reached the end of the buffer.
124
+ fileprivate func hasRemaining(_ reader: (data: Data, offset: Data.Index)) -> Bool {
125
+ return reader.offset < reader.data.count
126
+ }
127
+
128
+ // Define writer functionality. Normally this would be defined in a class or
129
+ // struct, but we use standalone functions instead in order to make external
130
+ // types work. See the above discussion on Readers for details.
131
+
132
+ fileprivate func createWriter() -> [UInt8] {
133
+ return []
134
+ }
135
+
136
+ fileprivate func writeBytes<S>(_ writer: inout [UInt8], _ byteArr: S) where S: Sequence, S.Element == UInt8 {
137
+ writer.append(contentsOf: byteArr)
138
+ }
139
+
140
+ // Writes an integer in big-endian order.
141
+ //
142
+ // Warning: make sure what you are trying to write
143
+ // is in the correct type!
144
+ fileprivate func writeInt<T: FixedWidthInteger>(_ writer: inout [UInt8], _ value: T) {
145
+ var value = value.bigEndian
146
+ withUnsafeBytes(of: &value) { writer.append(contentsOf: $0) }
147
+ }
148
+
149
+ fileprivate func writeFloat(_ writer: inout [UInt8], _ value: Float) {
150
+ writeInt(&writer, value.bitPattern)
151
+ }
152
+
153
+ fileprivate func writeDouble(_ writer: inout [UInt8], _ value: Double) {
154
+ writeInt(&writer, value.bitPattern)
155
+ }
156
+
157
+ // Protocol for types that transfer other types across the FFI. This is
158
+ // analogous to the Rust trait of the same name.
159
+ fileprivate protocol FfiConverter {
160
+ associatedtype FfiType
161
+ associatedtype SwiftType
162
+
163
+ static func lift(_ value: FfiType) throws -> SwiftType
164
+ static func lower(_ value: SwiftType) -> FfiType
165
+ static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType
166
+ static func write(_ value: SwiftType, into buf: inout [UInt8])
167
+ }
168
+
169
+ // Types conforming to `Primitive` pass themselves directly over the FFI.
170
+ fileprivate protocol FfiConverterPrimitive: FfiConverter where FfiType == SwiftType { }
171
+
172
+ extension FfiConverterPrimitive {
173
+ #if swift(>=5.8)
174
+ @_documentation(visibility: private)
175
+ #endif
176
+ public static func lift(_ value: FfiType) throws -> SwiftType {
177
+ return value
178
+ }
179
+
180
+ #if swift(>=5.8)
181
+ @_documentation(visibility: private)
182
+ #endif
183
+ public static func lower(_ value: SwiftType) -> FfiType {
184
+ return value
185
+ }
186
+ }
187
+
188
+ // Types conforming to `FfiConverterRustBuffer` lift and lower into a `RustBuffer`.
189
+ // Used for complex types where it's hard to write a custom lift/lower.
190
+ fileprivate protocol FfiConverterRustBuffer: FfiConverter where FfiType == RustBuffer {}
191
+
192
+ extension FfiConverterRustBuffer {
193
+ #if swift(>=5.8)
194
+ @_documentation(visibility: private)
195
+ #endif
196
+ public static func lift(_ buf: RustBuffer) throws -> SwiftType {
197
+ var reader = createReader(data: Data(rustBuffer: buf))
198
+ let value = try read(from: &reader)
199
+ if hasRemaining(reader) {
200
+ throw UniffiInternalError.incompleteData
201
+ }
202
+ buf.deallocate()
203
+ return value
204
+ }
205
+
206
+ #if swift(>=5.8)
207
+ @_documentation(visibility: private)
208
+ #endif
209
+ public static func lower(_ value: SwiftType) -> RustBuffer {
210
+ var writer = createWriter()
211
+ write(value, into: &writer)
212
+ return RustBuffer(bytes: writer)
213
+ }
214
+ }
215
+ // An error type for FFI errors. These errors occur at the UniFFI level, not
216
+ // the library level.
217
+ fileprivate enum UniffiInternalError: LocalizedError {
218
+ case bufferOverflow
219
+ case incompleteData
220
+ case unexpectedOptionalTag
221
+ case unexpectedEnumCase
222
+ case unexpectedNullPointer
223
+ case unexpectedRustCallStatusCode
224
+ case unexpectedRustCallError
225
+ case unexpectedStaleHandle
226
+ case rustPanic(_ message: String)
227
+
228
+ public var errorDescription: String? {
229
+ switch self {
230
+ case .bufferOverflow: return "Reading the requested value would read past the end of the buffer"
231
+ case .incompleteData: return "The buffer still has data after lifting its containing value"
232
+ case .unexpectedOptionalTag: return "Unexpected optional tag; should be 0 or 1"
233
+ case .unexpectedEnumCase: return "Raw enum value doesn't match any cases"
234
+ case .unexpectedNullPointer: return "Raw pointer value was null"
235
+ case .unexpectedRustCallStatusCode: return "Unexpected RustCallStatus code"
236
+ case .unexpectedRustCallError: return "CALL_ERROR but no errorClass specified"
237
+ case .unexpectedStaleHandle: return "The object in the handle map has been dropped already"
238
+ case let .rustPanic(message): return message
239
+ }
240
+ }
241
+ }
242
+
243
+ fileprivate extension NSLock {
244
+ func withLock<T>(f: () throws -> T) rethrows -> T {
245
+ self.lock()
246
+ defer { self.unlock() }
247
+ return try f()
248
+ }
249
+ }
250
+
251
+ fileprivate let CALL_SUCCESS: Int8 = 0
252
+ fileprivate let CALL_ERROR: Int8 = 1
253
+ fileprivate let CALL_UNEXPECTED_ERROR: Int8 = 2
254
+ fileprivate let CALL_CANCELLED: Int8 = 3
255
+
256
+ fileprivate extension RustCallStatus {
257
+ init() {
258
+ self.init(
259
+ code: CALL_SUCCESS,
260
+ errorBuf: RustBuffer.init(
261
+ capacity: 0,
262
+ len: 0,
263
+ data: nil
264
+ )
265
+ )
266
+ }
267
+ }
268
+
269
+ private func rustCall<T>(_ callback: (UnsafeMutablePointer<RustCallStatus>) -> T) throws -> T {
270
+ let neverThrow: ((RustBuffer) throws -> Never)? = nil
271
+ return try makeRustCall(callback, errorHandler: neverThrow)
272
+ }
273
+
274
+ private func rustCallWithError<T, E: Swift.Error>(
275
+ _ errorHandler: @escaping (RustBuffer) throws -> E,
276
+ _ callback: (UnsafeMutablePointer<RustCallStatus>) -> T) throws -> T {
277
+ try makeRustCall(callback, errorHandler: errorHandler)
278
+ }
279
+
280
+ private func makeRustCall<T, E: Swift.Error>(
281
+ _ callback: (UnsafeMutablePointer<RustCallStatus>) -> T,
282
+ errorHandler: ((RustBuffer) throws -> E)?
283
+ ) throws -> T {
284
+ uniffiEnsureNativeEditorHighlightingInitialized()
285
+ var callStatus = RustCallStatus.init()
286
+ let returnedVal = callback(&callStatus)
287
+ try uniffiCheckCallStatus(callStatus: callStatus, errorHandler: errorHandler)
288
+ return returnedVal
289
+ }
290
+
291
+ private func uniffiCheckCallStatus<E: Swift.Error>(
292
+ callStatus: RustCallStatus,
293
+ errorHandler: ((RustBuffer) throws -> E)?
294
+ ) throws {
295
+ switch callStatus.code {
296
+ case CALL_SUCCESS:
297
+ return
298
+
299
+ case CALL_ERROR:
300
+ if let errorHandler = errorHandler {
301
+ throw try errorHandler(callStatus.errorBuf)
302
+ } else {
303
+ callStatus.errorBuf.deallocate()
304
+ throw UniffiInternalError.unexpectedRustCallError
305
+ }
306
+
307
+ case CALL_UNEXPECTED_ERROR:
308
+ // When the rust code sees a panic, it tries to construct a RustBuffer
309
+ // with the message. But if that code panics, then it just sends back
310
+ // an empty buffer.
311
+ if callStatus.errorBuf.len > 0 {
312
+ throw UniffiInternalError.rustPanic(try FfiConverterString.lift(callStatus.errorBuf))
313
+ } else {
314
+ callStatus.errorBuf.deallocate()
315
+ throw UniffiInternalError.rustPanic("Rust panic")
316
+ }
317
+
318
+ case CALL_CANCELLED:
319
+ fatalError("Cancellation not supported yet")
320
+
321
+ default:
322
+ throw UniffiInternalError.unexpectedRustCallStatusCode
323
+ }
324
+ }
325
+
326
+ private func uniffiTraitInterfaceCall<T>(
327
+ callStatus: UnsafeMutablePointer<RustCallStatus>,
328
+ makeCall: () throws -> T,
329
+ writeReturn: (T) -> ()
330
+ ) {
331
+ do {
332
+ try writeReturn(makeCall())
333
+ } catch let error {
334
+ callStatus.pointee.code = CALL_UNEXPECTED_ERROR
335
+ callStatus.pointee.errorBuf = FfiConverterString.lower(String(describing: error))
336
+ }
337
+ }
338
+
339
+ private func uniffiTraitInterfaceCallWithError<T, E>(
340
+ callStatus: UnsafeMutablePointer<RustCallStatus>,
341
+ makeCall: () throws -> T,
342
+ writeReturn: (T) -> (),
343
+ lowerError: (E) -> RustBuffer
344
+ ) {
345
+ do {
346
+ try writeReturn(makeCall())
347
+ } catch let error as E {
348
+ callStatus.pointee.code = CALL_ERROR
349
+ callStatus.pointee.errorBuf = lowerError(error)
350
+ } catch {
351
+ callStatus.pointee.code = CALL_UNEXPECTED_ERROR
352
+ callStatus.pointee.errorBuf = FfiConverterString.lower(String(describing: error))
353
+ }
354
+ }
355
+ fileprivate final class UniffiHandleMap<T>: @unchecked Sendable {
356
+ // All mutation happens with this lock held, which is why we implement @unchecked Sendable.
357
+ private let lock = NSLock()
358
+ private var map: [UInt64: T] = [:]
359
+ private var currentHandle: UInt64 = 1
360
+
361
+ func insert(obj: T) -> UInt64 {
362
+ lock.withLock {
363
+ let handle = currentHandle
364
+ currentHandle += 1
365
+ map[handle] = obj
366
+ return handle
367
+ }
368
+ }
369
+
370
+ func get(handle: UInt64) throws -> T {
371
+ try lock.withLock {
372
+ guard let obj = map[handle] else {
373
+ throw UniffiInternalError.unexpectedStaleHandle
374
+ }
375
+ return obj
376
+ }
377
+ }
378
+
379
+ @discardableResult
380
+ func remove(handle: UInt64) throws -> T {
381
+ try lock.withLock {
382
+ guard let obj = map.removeValue(forKey: handle) else {
383
+ throw UniffiInternalError.unexpectedStaleHandle
384
+ }
385
+ return obj
386
+ }
387
+ }
388
+
389
+ var count: Int {
390
+ get {
391
+ map.count
392
+ }
393
+ }
394
+ }
395
+
396
+
397
+ // Public interface members begin here.
398
+
399
+
400
+ #if swift(>=5.8)
401
+ @_documentation(visibility: private)
402
+ #endif
403
+ fileprivate struct FfiConverterUInt8: FfiConverterPrimitive {
404
+ typealias FfiType = UInt8
405
+ typealias SwiftType = UInt8
406
+
407
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> UInt8 {
408
+ return try lift(readInt(&buf))
409
+ }
410
+
411
+ public static func write(_ value: UInt8, into buf: inout [UInt8]) {
412
+ writeInt(&buf, lower(value))
413
+ }
414
+ }
415
+
416
+ #if swift(>=5.8)
417
+ @_documentation(visibility: private)
418
+ #endif
419
+ fileprivate struct FfiConverterUInt32: FfiConverterPrimitive {
420
+ typealias FfiType = UInt32
421
+ typealias SwiftType = UInt32
422
+
423
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> UInt32 {
424
+ return try lift(readInt(&buf))
425
+ }
426
+
427
+ public static func write(_ value: SwiftType, into buf: inout [UInt8]) {
428
+ writeInt(&buf, lower(value))
429
+ }
430
+ }
431
+
432
+ #if swift(>=5.8)
433
+ @_documentation(visibility: private)
434
+ #endif
435
+ fileprivate struct FfiConverterString: FfiConverter {
436
+ typealias SwiftType = String
437
+ typealias FfiType = RustBuffer
438
+
439
+ public static func lift(_ value: RustBuffer) throws -> String {
440
+ defer {
441
+ value.deallocate()
442
+ }
443
+ if value.data == nil {
444
+ return String()
445
+ }
446
+ let bytes = UnsafeBufferPointer<UInt8>(start: value.data!, count: Int(value.len))
447
+ return String(bytes: bytes, encoding: String.Encoding.utf8)!
448
+ }
449
+
450
+ public static func lower(_ value: String) -> RustBuffer {
451
+ return value.utf8CString.withUnsafeBufferPointer { ptr in
452
+ // The swift string gives us int8_t, we want uint8_t.
453
+ ptr.withMemoryRebound(to: UInt8.self) { ptr in
454
+ // The swift string gives us a trailing null byte, we don't want it.
455
+ let buf = UnsafeBufferPointer(rebasing: ptr.prefix(upTo: ptr.count - 1))
456
+ return RustBuffer.from(buf)
457
+ }
458
+ }
459
+ }
460
+
461
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> String {
462
+ let len: Int32 = try readInt(&buf)
463
+ return String(bytes: try readBytes(&buf, count: Int(len)), encoding: String.Encoding.utf8)!
464
+ }
465
+
466
+ public static func write(_ value: String, into buf: inout [UInt8]) {
467
+ let len = Int32(value.utf8.count)
468
+ writeInt(&buf, len)
469
+ writeBytes(&buf, value.utf8)
470
+ }
471
+ }
472
+
473
+
474
+ public struct HighlightRange {
475
+ public var start: UInt32
476
+ public var length: UInt32
477
+ public var color: UInt32
478
+ public var fontStyle: UInt8
479
+
480
+ // Default memberwise initializers are never public by default, so we
481
+ // declare one manually.
482
+ public init(start: UInt32, length: UInt32, color: UInt32, fontStyle: UInt8) {
483
+ self.start = start
484
+ self.length = length
485
+ self.color = color
486
+ self.fontStyle = fontStyle
487
+ }
488
+ }
489
+
490
+ #if compiler(>=6)
491
+ extension HighlightRange: Sendable {}
492
+ #endif
493
+
494
+
495
+ extension HighlightRange: Equatable, Hashable {
496
+ public static func ==(lhs: HighlightRange, rhs: HighlightRange) -> Bool {
497
+ if lhs.start != rhs.start {
498
+ return false
499
+ }
500
+ if lhs.length != rhs.length {
501
+ return false
502
+ }
503
+ if lhs.color != rhs.color {
504
+ return false
505
+ }
506
+ if lhs.fontStyle != rhs.fontStyle {
507
+ return false
508
+ }
509
+ return true
510
+ }
511
+
512
+ public func hash(into hasher: inout Hasher) {
513
+ hasher.combine(start)
514
+ hasher.combine(length)
515
+ hasher.combine(color)
516
+ hasher.combine(fontStyle)
517
+ }
518
+ }
519
+
520
+
521
+
522
+ #if swift(>=5.8)
523
+ @_documentation(visibility: private)
524
+ #endif
525
+ public struct FfiConverterTypeHighlightRange: FfiConverterRustBuffer {
526
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> HighlightRange {
527
+ return
528
+ try HighlightRange(
529
+ start: FfiConverterUInt32.read(from: &buf),
530
+ length: FfiConverterUInt32.read(from: &buf),
531
+ color: FfiConverterUInt32.read(from: &buf),
532
+ fontStyle: FfiConverterUInt8.read(from: &buf)
533
+ )
534
+ }
535
+
536
+ public static func write(_ value: HighlightRange, into buf: inout [UInt8]) {
537
+ FfiConverterUInt32.write(value.start, into: &buf)
538
+ FfiConverterUInt32.write(value.length, into: &buf)
539
+ FfiConverterUInt32.write(value.color, into: &buf)
540
+ FfiConverterUInt8.write(value.fontStyle, into: &buf)
541
+ }
542
+ }
543
+
544
+
545
+ #if swift(>=5.8)
546
+ @_documentation(visibility: private)
547
+ #endif
548
+ public func FfiConverterTypeHighlightRange_lift(_ buf: RustBuffer) throws -> HighlightRange {
549
+ return try FfiConverterTypeHighlightRange.lift(buf)
550
+ }
551
+
552
+ #if swift(>=5.8)
553
+ @_documentation(visibility: private)
554
+ #endif
555
+ public func FfiConverterTypeHighlightRange_lower(_ value: HighlightRange) -> RustBuffer {
556
+ return FfiConverterTypeHighlightRange.lower(value)
557
+ }
558
+
559
+
560
+ public enum HighlightError: Swift.Error {
561
+
562
+
563
+
564
+ case UnknownTheme(theme: String
565
+ )
566
+ }
567
+
568
+
569
+ #if swift(>=5.8)
570
+ @_documentation(visibility: private)
571
+ #endif
572
+ public struct FfiConverterTypeHighlightError: FfiConverterRustBuffer {
573
+ typealias SwiftType = HighlightError
574
+
575
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> HighlightError {
576
+ let variant: Int32 = try readInt(&buf)
577
+ switch variant {
578
+
579
+
580
+
581
+
582
+ case 1: return .UnknownTheme(
583
+ theme: try FfiConverterString.read(from: &buf)
584
+ )
585
+
586
+ default: throw UniffiInternalError.unexpectedEnumCase
587
+ }
588
+ }
589
+
590
+ public static func write(_ value: HighlightError, into buf: inout [UInt8]) {
591
+ switch value {
592
+
593
+
594
+
595
+
596
+
597
+ case let .UnknownTheme(theme):
598
+ writeInt(&buf, Int32(1))
599
+ FfiConverterString.write(theme, into: &buf)
600
+
601
+ }
602
+ }
603
+ }
604
+
605
+
606
+ #if swift(>=5.8)
607
+ @_documentation(visibility: private)
608
+ #endif
609
+ public func FfiConverterTypeHighlightError_lift(_ buf: RustBuffer) throws -> HighlightError {
610
+ return try FfiConverterTypeHighlightError.lift(buf)
611
+ }
612
+
613
+ #if swift(>=5.8)
614
+ @_documentation(visibility: private)
615
+ #endif
616
+ public func FfiConverterTypeHighlightError_lower(_ value: HighlightError) -> RustBuffer {
617
+ return FfiConverterTypeHighlightError.lower(value)
618
+ }
619
+
620
+
621
+ extension HighlightError: Equatable, Hashable {}
622
+
623
+
624
+
625
+
626
+ extension HighlightError: Foundation.LocalizedError {
627
+ public var errorDescription: String? {
628
+ String(reflecting: self)
629
+ }
630
+ }
631
+
632
+
633
+
634
+
635
+ #if swift(>=5.8)
636
+ @_documentation(visibility: private)
637
+ #endif
638
+ fileprivate struct FfiConverterOptionString: FfiConverterRustBuffer {
639
+ typealias SwiftType = String?
640
+
641
+ public static func write(_ value: SwiftType, into buf: inout [UInt8]) {
642
+ guard let value = value else {
643
+ writeInt(&buf, Int8(0))
644
+ return
645
+ }
646
+ writeInt(&buf, Int8(1))
647
+ FfiConverterString.write(value, into: &buf)
648
+ }
649
+
650
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType {
651
+ switch try readInt(&buf) as Int8 {
652
+ case 0: return nil
653
+ case 1: return try FfiConverterString.read(from: &buf)
654
+ default: throw UniffiInternalError.unexpectedOptionalTag
655
+ }
656
+ }
657
+ }
658
+
659
+ #if swift(>=5.8)
660
+ @_documentation(visibility: private)
661
+ #endif
662
+ fileprivate struct FfiConverterSequenceTypeHighlightRange: FfiConverterRustBuffer {
663
+ typealias SwiftType = [HighlightRange]
664
+
665
+ public static func write(_ value: [HighlightRange], into buf: inout [UInt8]) {
666
+ let len = Int32(value.count)
667
+ writeInt(&buf, len)
668
+ for item in value {
669
+ FfiConverterTypeHighlightRange.write(item, into: &buf)
670
+ }
671
+ }
672
+
673
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> [HighlightRange] {
674
+ let len: Int32 = try readInt(&buf)
675
+ var seq = [HighlightRange]()
676
+ seq.reserveCapacity(Int(len))
677
+ for _ in 0 ..< len {
678
+ seq.append(try FfiConverterTypeHighlightRange.read(from: &buf))
679
+ }
680
+ return seq
681
+ }
682
+ }
683
+ public func highlightCode(text: String, language: String?, theme: String)throws -> [HighlightRange] {
684
+ return try FfiConverterSequenceTypeHighlightRange.lift(try rustCallWithError(FfiConverterTypeHighlightError_lift) {
685
+ uniffi_native_editor_highlighting_fn_func_highlight_code(
686
+ FfiConverterString.lower(text),
687
+ FfiConverterOptionString.lower(language),
688
+ FfiConverterString.lower(theme),$0
689
+ )
690
+ })
691
+ }
692
+
693
+ private enum InitializationResult {
694
+ case ok
695
+ case contractVersionMismatch
696
+ case apiChecksumMismatch
697
+ }
698
+ // Use a global variable to perform the versioning checks. Swift ensures that
699
+ // the code inside is only computed once.
700
+ private let initializationResult: InitializationResult = {
701
+ // Get the bindings contract version from our ComponentInterface
702
+ let bindings_contract_version = 29
703
+ // Get the scaffolding contract version by calling the into the dylib
704
+ let scaffolding_contract_version = ffi_native_editor_highlighting_uniffi_contract_version()
705
+ if bindings_contract_version != scaffolding_contract_version {
706
+ return InitializationResult.contractVersionMismatch
707
+ }
708
+ if (uniffi_native_editor_highlighting_checksum_func_highlight_code() != 17678) {
709
+ return InitializationResult.apiChecksumMismatch
710
+ }
711
+
712
+ return InitializationResult.ok
713
+ }()
714
+
715
+ // Make the ensure init function public so that other modules which have external type references to
716
+ // our types can call it.
717
+ public func uniffiEnsureNativeEditorHighlightingInitialized() {
718
+ switch initializationResult {
719
+ case .ok:
720
+ break
721
+ case .contractVersionMismatch:
722
+ fatalError("UniFFI contract version mismatch: try cleaning and rebuilding your project")
723
+ case .apiChecksumMismatch:
724
+ fatalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
725
+ }
726
+ }
727
+
728
+ // swiftlint:enable all