1// This file was autogenerated by some hot garbage in the `uniffi` crate.2// Trust me, you don't want to mess with it!34// swiftlint:disable all5import Foundation67// Depending on the consumer's build setup, the low-level FFI code8// might be in a separate module, or it might be compiled inline into9// this module. This is a bit of light hackery to work with both.10#if canImport(sophax_mlsFFI)11import sophax_mlsFFI12#endif1314fileprivate extension RustBuffer {15// Allocate a new buffer, copying the contents of a `UInt8` array.16 init(bytes:[UInt8]){17 let rbuf = bytes.withUnsafeBufferPointer { ptr in18 RustBuffer.from(ptr)19}20 self.init(capacity: rbuf.capacity, len: rbuf.len, data: rbuf.data)21}2223 static func empty()-> RustBuffer {24 RustBuffer(capacity: 0, len:0, data: nil)25}2627 static func from(_ ptr: UnsafeBufferPointer<UInt8>)-> RustBuffer {28 try! rustCall { ffi_sophax_mls_rustbuffer_from_bytes(ForeignBytes(bufferPointer: ptr), $0)}29}3031// Frees the buffer in place.32// The buffer must not be used after this is called.33 func deallocate(){34 try! rustCall { ffi_sophax_mls_rustbuffer_free(self, $0)}35}36}3738fileprivate extension ForeignBytes {39 init(bufferPointer: UnsafeBufferPointer<UInt8>){40 self.init(len: Int32(bufferPointer.count), data: bufferPointer.baseAddress)41}42}4344// For every type used in the interface, we provide helper methods for conveniently45// lifting and lowering that type from C-compatible data, and for reading and writing46// values of that type in a buffer.4748// Helper classes/extensions that don't change.49// Someday, this will be in a library of its own.5051fileprivate extension Data {52 init(rustBuffer: RustBuffer){53 self.init(54 bytesNoCopy: rustBuffer.data!,55 count: Int(rustBuffer.len),56 deallocator:.none57)58}59}6061// Define reader functionality. Normally this would be defined in a class or62// struct, but we use standalone functions instead in order to make external63// types work.64//65// With external types, one swift source file needs to be able to call the read66// method on another source file's FfiConverter, but then what visibility67// should Reader have?68// - If Reader is fileprivate, then this means the read() must also69// be fileprivate, which doesn't work with external types.70// - If Reader is internal/public, we'll get compile errors since both source71// files will try define the same type.72//73// Instead, the read() method and these helper functions input a tuple of data7475fileprivate func createReader(data: Data)->(data: Data, offset: Data.Index){76(data: data, offset: 0)77}7879// Reads an integer at the current offset, in big-endian order, and advances80// the offset on success. Throws if reading the integer would move the81// offset past the end of the buffer.82fileprivate func readInt<T: FixedWidthInteger>(_ reader: inout (data: Data, offset: Data.Index)) throws -> T {83 let range = reader.offset..<reader.offset + MemoryLayout<T>.size84 guard reader.data.count >= range.upperBound else {85 throw UniffiInternalError.bufferOverflow86}87 if T.self == UInt8.self {88 let value = reader.data[reader.offset]89 reader.offset += 190 return value as! T91}92 var value: T = 093 let _ = withUnsafeMutableBytes(of:&value,{ reader.data.copyBytes(to: $0, from: range)})94 reader.offset = range.upperBound95 return value.bigEndian96}9798// Reads an arbitrary number of bytes, to be used to read99// raw bytes, this is useful when lifting strings100fileprivate 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.bufferOverflow104}105 var value =[UInt8](repeating: 0, count: count)106 value.withUnsafeMutableBufferPointer({ buffer in107 reader.data.copyBytes(to: buffer, from: range)108})109 reader.offset = range.upperBound110 return value111}112113// Reads a float at the current offset.114fileprivate func readFloat(_ reader: inout (data: Data, offset: Data.Index)) throws -> Float {115 return Float(bitPattern: try readInt(&reader))116}117118// Reads a float at the current offset.119fileprivate func readDouble(_ reader: inout (data: Data, offset: Data.Index)) throws -> Double {120 return Double(bitPattern: try readInt(&reader))121}122123// Indicates if the offset has reached the end of the buffer.124fileprivate func hasRemaining(_ reader:(data: Data, offset: Data.Index))-> Bool {125 return reader.offset < reader.data.count126}127128// Define writer functionality. Normally this would be defined in a class or129// struct, but we use standalone functions instead in order to make external130// types work. See the above discussion on Readers for details.131132fileprivate func createWriter()->[UInt8]{133 return []134}135136fileprivate func writeBytes<S>(_ writer: inout [UInt8], _ byteArr: S) where S: Sequence, S.Element == UInt8 {137 writer.append(contentsOf: byteArr)138}139140// Writes an integer in big-endian order.141//142// Warning: make sure what you are trying to write143// is in the correct type!144fileprivate func writeInt<T: FixedWidthInteger>(_ writer: inout [UInt8], _ value: T){145 var value = value.bigEndian146 withUnsafeBytes(of:&value){ writer.append(contentsOf: $0)}147}148149fileprivate func writeFloat(_ writer: inout [UInt8], _ value: Float){150 writeInt(&writer, value.bitPattern)151}152153fileprivate func writeDouble(_ writer: inout [UInt8], _ value: Double){154 writeInt(&writer, value.bitPattern)155}156157// Protocol for types that transfer other types across the FFI. This is158// analogous to the Rust trait of the same name.159fileprivate protocol FfiConverter {160 associatedtype FfiType161 associatedtype SwiftType162163 static func lift(_ value: FfiType) throws -> SwiftType164 static func lower(_ value: SwiftType)-> FfiType165 static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType166 static func write(_ value: SwiftType, into buf: inout [UInt8])167}168169// Types conforming to `Primitive` pass themselves directly over the FFI.170fileprivate protocol FfiConverterPrimitive: FfiConverter where FfiType == SwiftType {}171172extension FfiConverterPrimitive {173#if swift(>=5.8)174@_documentation(visibility: private)175#endif176 public static func lift(_ value: FfiType) throws -> SwiftType {177 return value178}179180#if swift(>=5.8)181@_documentation(visibility: private)182#endif183 public static func lower(_ value: SwiftType)-> FfiType {184 return value185}186}187188// 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.190fileprivate protocol FfiConverterRustBuffer: FfiConverter where FfiType == RustBuffer {}191192extension FfiConverterRustBuffer {193#if swift(>=5.8)194@_documentation(visibility: private)195#endif196 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.incompleteData201}202 buf.deallocate()203 return value204}205206#if swift(>=5.8)207@_documentation(visibility: private)208#endif209 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, not216// the library level.217fileprivate enum UniffiInternalError: LocalizedError {218 case bufferOverflow219 case incompleteData220 case unexpectedOptionalTag221 case unexpectedEnumCase222 case unexpectedNullPointer223 case unexpectedRustCallStatusCode224 case unexpectedRustCallError225 case unexpectedStaleHandle226 case rustPanic(_ message: String)227228 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 message239}240}241}242243fileprivate extension NSLock {244 func withLock<T>(f:() throws -> T) rethrows -> T {245 self.lock()246 defer { self.unlock()}247 return try f()248}249}250251fileprivate let CALL_SUCCESS: Int8 = 0252fileprivate let CALL_ERROR: Int8 = 1253fileprivate let CALL_UNEXPECTED_ERROR: Int8 = 2254fileprivate let CALL_CANCELLED: Int8 = 3255256fileprivate extension RustCallStatus {257 init(){258 self.init(259 code: CALL_SUCCESS,260 errorBuf: RustBuffer.init(261 capacity: 0,262 len: 0,263 data: nil264)265)266}267}268269private func rustCall<T>(_ callback:(UnsafeMutablePointer<RustCallStatus>)-> T) throws -> T {270 let neverThrow:((RustBuffer) throws -> Never)?= nil271 return try makeRustCall(callback, errorHandler: neverThrow)272}273274private 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}279280private func makeRustCall<T, E: Swift.Error>(281 _ callback:(UnsafeMutablePointer<RustCallStatus>)-> T,282 errorHandler:((RustBuffer) throws -> E)?283) throws -> T {284 uniffiEnsureSophaxMlsInitialized()285 var callStatus = RustCallStatus.init()286 let returnedVal = callback(&callStatus)287 try uniffiCheckCallStatus(callStatus: callStatus, errorHandler: errorHandler)288 return returnedVal289}290291private func uniffiCheckCallStatus<E: Swift.Error>(292 callStatus: RustCallStatus,293 errorHandler:((RustBuffer) throws -> E)?294) throws {295 switch callStatus.code {296 case CALL_SUCCESS:297 return298299 case CALL_ERROR:300 if let errorHandler = errorHandler {301 throw try errorHandler(callStatus.errorBuf)302} else {303 callStatus.errorBuf.deallocate()304 throw UniffiInternalError.unexpectedRustCallError305}306307 case CALL_UNEXPECTED_ERROR:308// When the rust code sees a panic, it tries to construct a RustBuffer309// with the message. But if that code panics, then it just sends back310// 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}317318 case CALL_CANCELLED:319 fatalError("Cancellation not supported yet")320321 default:322 throw UniffiInternalError.unexpectedRustCallStatusCode323}324}325326private 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_ERROR335 callStatus.pointee.errorBuf = FfiConverterString.lower(String(describing: error))336}337}338339private func uniffiTraitInterfaceCallWithError<T, E>(340 callStatus: UnsafeMutablePointer<RustCallStatus>,341 makeCall:() throws -> T,342 writeReturn:(T)->(),343 lowerError:(E)-> RustBuffer344){345 do {346 try writeReturn(makeCall())347} catch let error as E {348 callStatus.pointee.code = CALL_ERROR349 callStatus.pointee.errorBuf = lowerError(error)350} catch {351 callStatus.pointee.code = CALL_UNEXPECTED_ERROR352 callStatus.pointee.errorBuf = FfiConverterString.lower(String(describing: error))353}354}355// Initial value and increment amount for handles. 356// These ensure that SWIFT handles always have the lowest bit set357fileprivate let UNIFFI_HANDLEMAP_INITIAL: UInt64 = 1358fileprivate let UNIFFI_HANDLEMAP_DELTA: UInt64 = 2359360fileprivate final class UniffiHandleMap<T>:@unchecked Sendable {361// All mutation happens with this lock held, which is why we implement @unchecked Sendable.362 private let lock = NSLock()363 private var map:[UInt64: T]=[:]364 private var currentHandle: UInt64 = UNIFFI_HANDLEMAP_INITIAL365366 func insert(obj: T)-> UInt64 {367 lock.withLock {368 return doInsert(obj)369}370}371372// Low-level insert function, this assumes `lock` is held.373 private func doInsert(_ obj: T)-> UInt64 {374 let handle = currentHandle375 currentHandle += UNIFFI_HANDLEMAP_DELTA376 map[handle]= obj377 return handle378}379380 func get(handle: UInt64) throws -> T {381 try lock.withLock {382 guard let obj = map[handle] else {383 throw UniffiInternalError.unexpectedStaleHandle384}385 return obj386}387}388389 func clone(handle: UInt64) throws -> UInt64 {390 try lock.withLock {391 guard let obj = map[handle] else {392 throw UniffiInternalError.unexpectedStaleHandle393}394 return doInsert(obj)395}396}397398@discardableResult399 func remove(handle: UInt64) throws -> T {400 try lock.withLock {401 guard let obj = map.removeValue(forKey: handle) else {402 throw UniffiInternalError.unexpectedStaleHandle403}404 return obj405}406}407408 var count: Int {409 get {410 map.count411}412}413}414415416// Public interface members begin here.417418419#if swift(>=5.8)420@_documentation(visibility: private)421#endif422fileprivate struct FfiConverterUInt64: FfiConverterPrimitive {423 typealias FfiType = UInt64424 typealias SwiftType = UInt64425426 public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> UInt64 {427 return try lift(readInt(&buf))428}429430 public static func write(_ value: SwiftType, into buf: inout [UInt8]){431 writeInt(&buf, lower(value))432}433}434435#if swift(>=5.8)436@_documentation(visibility: private)437#endif438fileprivate struct FfiConverterString: FfiConverter {439 typealias SwiftType = String440 typealias FfiType = RustBuffer441442 public static func lift(_ value: RustBuffer) throws -> String {443 defer {444 value.deallocate()445}446 if value.data == nil {447 return String()448}449 let bytes = UnsafeBufferPointer<UInt8>(start: value.data!, count: Int(value.len))450 return String(bytes: bytes, encoding: String.Encoding.utf8)!451}452453 public static func lower(_ value: String)-> RustBuffer {454 return value.utf8CString.withUnsafeBufferPointer { ptr in455// The swift string gives us int8_t, we want uint8_t.456 ptr.withMemoryRebound(to: UInt8.self){ ptr in457// The swift string gives us a trailing null byte, we don't want it.458 let buf = UnsafeBufferPointer(rebasing: ptr.prefix(upTo: ptr.count - 1))459 return RustBuffer.from(buf)460}461}462}463464 public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> String {465 let len: Int32 = try readInt(&buf)466 return String(bytes: try readBytes(&buf, count: Int(len)), encoding: String.Encoding.utf8)!467}468469 public static func write(_ value: String, into buf: inout [UInt8]){470 let len = Int32(value.utf8.count)471 writeInt(&buf, len)472 writeBytes(&buf, value.utf8)473}474}475476#if swift(>=5.8)477@_documentation(visibility: private)478#endif479fileprivate struct FfiConverterData: FfiConverterRustBuffer {480 typealias SwiftType = Data481482 public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Data {483 let len: Int32 = try readInt(&buf)484 return Data(try readBytes(&buf, count: Int(len)))485}486487 public static func write(_ value: Data, into buf: inout [UInt8]){488 let len = Int32(value.count)489 writeInt(&buf, len)490 writeBytes(&buf, value)491}492}493494495496497/**498 * Local MLS identity derived from the peer's existing Ed25519 key.499 */500public protocol MlsClientHandleProtocol: AnyObject, Sendable {501502/**503 * Generate a fresh single-use KeyPackage for this identity.504 */505 func generateKeyPackage() throws -> Data506507}508/**509 * Local MLS identity derived from the peer's existing Ed25519 key.510 */511open class MlsClientHandle: MlsClientHandleProtocol,@unchecked Sendable {512 fileprivate let handle: UInt64513514/// Used to instantiate a [FFIObject] without an actual handle, for fakes in tests, mostly.515#if swift(>=5.8)516@_documentation(visibility: private)517#endif518 public struct NoHandle {519 public init(){}520}521522// TODO: We'd like this to be `private` but for Swifty reasons,523// we can't implement `FfiConverter` without making this `required` and we can't524// make it `required` without making it `public`.525#if swift(>=5.8)526@_documentation(visibility: private)527#endif528 required public init(unsafeFromHandle handle: UInt64){529 self.handle = handle530}531532// This constructor can be used to instantiate a fake object.533// - Parameter noHandle: Placeholder value so we can have a constructor separate from the default empty one that may be implemented for classes extending [FFIObject].534//535// - Warning:536// Any object instantiated with this constructor cannot be passed to an actual Rust-backed object. Since there isn't a backing handle the FFI lower functions will crash.537#if swift(>=5.8)538@_documentation(visibility: private)539#endif540 public init(noHandle: NoHandle){541 self.handle = 0542}543544#if swift(>=5.8)545@_documentation(visibility: private)546#endif547 public func uniffiCloneHandle()-> UInt64 {548 return try! rustCall { uniffi_sophax_mls_fn_clone_mlsclienthandle(self.handle, $0)}549}550public convenience init(peerId: String, signingKeyBytes: Data)throws {551 let handle =552 try rustCallWithError(FfiConverterTypeMlsError_lift){553 uniffi_sophax_mls_fn_constructor_mlsclienthandle_new(554 FfiConverterString.lower(peerId),555 FfiConverterData.lower(signingKeyBytes),$0556)557}558 self.init(unsafeFromHandle: handle)559}560561 deinit {562 if handle == 0 {563// Mock objects have handle=0 don't try to free them564 return565}566567 try! rustCall { uniffi_sophax_mls_fn_free_mlsclienthandle(handle, $0)}568}569570571572573/**574 * Generate a fresh single-use KeyPackage for this identity.575 */576open func generateKeyPackage()throws -> Data {577 return try FfiConverterData.lift(try rustCallWithError(FfiConverterTypeMlsError_lift){578 uniffi_sophax_mls_fn_method_mlsclienthandle_generate_key_package(579 self.uniffiCloneHandle(),$0580)581})582}583584585586}587588589#if swift(>=5.8)590@_documentation(visibility: private)591#endif592public struct FfiConverterTypeMlsClientHandle: FfiConverter {593 typealias FfiType = UInt64594 typealias SwiftType = MlsClientHandle595596 public static func lift(_ handle: UInt64) throws -> MlsClientHandle {597 return MlsClientHandle(unsafeFromHandle: handle)598}599600 public static func lower(_ value: MlsClientHandle)-> UInt64 {601 return value.uniffiCloneHandle()602}603604 public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> MlsClientHandle {605 let handle: UInt64 = try readInt(&buf)606 return try lift(handle)607}608609 public static func write(_ value: MlsClientHandle, into buf: inout [UInt8]){610 writeInt(&buf, lower(value))611}612}613614615#if swift(>=5.8)616@_documentation(visibility: private)617#endif618public func FfiConverterTypeMlsClientHandle_lift(_ handle: UInt64) throws -> MlsClientHandle {619 return try FfiConverterTypeMlsClientHandle.lift(handle)620}621622#if swift(>=5.8)623@_documentation(visibility: private)624#endif625public func FfiConverterTypeMlsClientHandle_lower(_ value: MlsClientHandle)-> UInt64 {626 return FfiConverterTypeMlsClientHandle.lower(value)627}628629630631632public struct CommitOutput: Equatable, Hashable {633/**634 * Broadcast to all group members635 */636 public var commitBytes: Data637/**638 * Send to new member (present only for AddMember commits)639 */640 public var welcomeBytes: Data?641/**642 * Updated state — replace persisted state after distributing commit643 */644 public var newGroupState: Data645 public var newEpoch: UInt64646647// Default memberwise initializers are never public by default, so we648// declare one manually.649 public init(650/**651 * Broadcast to all group members652 */commitBytes: Data,653/**654 * Send to new member (present only for AddMember commits)655 */welcomeBytes: Data?,656/**657 * Updated state — replace persisted state after distributing commit658 */newGroupState: Data, newEpoch: UInt64){659 self.commitBytes = commitBytes660 self.welcomeBytes = welcomeBytes661 self.newGroupState = newGroupState662 self.newEpoch = newEpoch663}664665666667668}669670#if compiler(>=6)671extension CommitOutput: Sendable {}672#endif673674#if swift(>=5.8)675@_documentation(visibility: private)676#endif677public struct FfiConverterTypeCommitOutput: FfiConverterRustBuffer {678 public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> CommitOutput {679 return680 try CommitOutput(681 commitBytes: FfiConverterData.read(from:&buf),682 welcomeBytes: FfiConverterOptionData.read(from:&buf),683 newGroupState: FfiConverterData.read(from:&buf),684 newEpoch: FfiConverterUInt64.read(from:&buf)685)686}687688 public static func write(_ value: CommitOutput, into buf: inout [UInt8]){689 FfiConverterData.write(value.commitBytes, into:&buf)690 FfiConverterOptionData.write(value.welcomeBytes, into:&buf)691 FfiConverterData.write(value.newGroupState, into:&buf)692 FfiConverterUInt64.write(value.newEpoch, into:&buf)693}694}695696697#if swift(>=5.8)698@_documentation(visibility: private)699#endif700public func FfiConverterTypeCommitOutput_lift(_ buf: RustBuffer) throws -> CommitOutput {701 return try FfiConverterTypeCommitOutput.lift(buf)702}703704#if swift(>=5.8)705@_documentation(visibility: private)706#endif707public func FfiConverterTypeCommitOutput_lower(_ value: CommitOutput)-> RustBuffer {708 return FfiConverterTypeCommitOutput.lower(value)709}710711712public struct CreateGroupOutput: Equatable, Hashable {713/**714 * Coordinator's group state — persist encrypted immediately715 */716 public var groupState: Data717/**718 * One Welcome per invited member; send via DR unicast719 */720 public var welcomes:[MemberWelcome]721722// Default memberwise initializers are never public by default, so we723// declare one manually.724 public init(725/**726 * Coordinator's group state — persist encrypted immediately727 */groupState: Data,728/**729 * One Welcome per invited member; send via DR unicast730 */welcomes:[MemberWelcome]){731 self.groupState = groupState732 self.welcomes = welcomes733}734735736737738}739740#if compiler(>=6)741extension CreateGroupOutput: Sendable {}742#endif743744#if swift(>=5.8)745@_documentation(visibility: private)746#endif747public struct FfiConverterTypeCreateGroupOutput: FfiConverterRustBuffer {748 public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> CreateGroupOutput {749 return750 try CreateGroupOutput(751 groupState: FfiConverterData.read(from:&buf),752 welcomes: FfiConverterSequenceTypeMemberWelcome.read(from:&buf)753)754}755756 public static func write(_ value: CreateGroupOutput, into buf: inout [UInt8]){757 FfiConverterData.write(value.groupState, into:&buf)758 FfiConverterSequenceTypeMemberWelcome.write(value.welcomes, into:&buf)759}760}761762763#if swift(>=5.8)764@_documentation(visibility: private)765#endif766public func FfiConverterTypeCreateGroupOutput_lift(_ buf: RustBuffer) throws -> CreateGroupOutput {767 return try FfiConverterTypeCreateGroupOutput.lift(buf)768}769770#if swift(>=5.8)771@_documentation(visibility: private)772#endif773public func FfiConverterTypeCreateGroupOutput_lower(_ value: CreateGroupOutput)-> RustBuffer {774 return FfiConverterTypeCreateGroupOutput.lower(value)775}776777778public struct DecryptOutput: Equatable, Hashable {779 public var plaintext: Data780 public var newGroupState: Data781782// Default memberwise initializers are never public by default, so we783// declare one manually.784 public init(plaintext: Data, newGroupState: Data){785 self.plaintext = plaintext786 self.newGroupState = newGroupState787}788789790791792}793794#if compiler(>=6)795extension DecryptOutput: Sendable {}796#endif797798#if swift(>=5.8)799@_documentation(visibility: private)800#endif801public struct FfiConverterTypeDecryptOutput: FfiConverterRustBuffer {802 public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> DecryptOutput {803 return804 try DecryptOutput(805 plaintext: FfiConverterData.read(from:&buf),806 newGroupState: FfiConverterData.read(from:&buf)807)808}809810 public static func write(_ value: DecryptOutput, into buf: inout [UInt8]){811 FfiConverterData.write(value.plaintext, into:&buf)812 FfiConverterData.write(value.newGroupState, into:&buf)813}814}815816817#if swift(>=5.8)818@_documentation(visibility: private)819#endif820public func FfiConverterTypeDecryptOutput_lift(_ buf: RustBuffer) throws -> DecryptOutput {821 return try FfiConverterTypeDecryptOutput.lift(buf)822}823824#if swift(>=5.8)825@_documentation(visibility: private)826#endif827public func FfiConverterTypeDecryptOutput_lower(_ value: DecryptOutput)-> RustBuffer {828 return FfiConverterTypeDecryptOutput.lower(value)829}830831832public struct EncryptOutput: Equatable, Hashable {833 public var ciphertext: Data834 public var newGroupState: Data835836// Default memberwise initializers are never public by default, so we837// declare one manually.838 public init(ciphertext: Data, newGroupState: Data){839 self.ciphertext = ciphertext840 self.newGroupState = newGroupState841}842843844845846}847848#if compiler(>=6)849extension EncryptOutput: Sendable {}850#endif851852#if swift(>=5.8)853@_documentation(visibility: private)854#endif855public struct FfiConverterTypeEncryptOutput: FfiConverterRustBuffer {856 public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> EncryptOutput {857 return858 try EncryptOutput(859 ciphertext: FfiConverterData.read(from:&buf),860 newGroupState: FfiConverterData.read(from:&buf)861)862}863864 public static func write(_ value: EncryptOutput, into buf: inout [UInt8]){865 FfiConverterData.write(value.ciphertext, into:&buf)866 FfiConverterData.write(value.newGroupState, into:&buf)867}868}869870871#if swift(>=5.8)872@_documentation(visibility: private)873#endif874public func FfiConverterTypeEncryptOutput_lift(_ buf: RustBuffer) throws -> EncryptOutput {875 return try FfiConverterTypeEncryptOutput.lift(buf)876}877878#if swift(>=5.8)879@_documentation(visibility: private)880#endif881public func FfiConverterTypeEncryptOutput_lower(_ value: EncryptOutput)-> RustBuffer {882 return FfiConverterTypeEncryptOutput.lower(value)883}884885886public struct MemberWelcome: Equatable, Hashable {887 public var peerId: String888 public var welcomeBytes: Data889/**890 * Ratchet tree bytes — send alongside welcome for clients without tree extension891 */892 public var ratchetTreeBytes: Data893894// Default memberwise initializers are never public by default, so we895// declare one manually.896 public init(peerId: String, welcomeBytes: Data,897/**898 * Ratchet tree bytes — send alongside welcome for clients without tree extension899 */ratchetTreeBytes: Data){900 self.peerId = peerId901 self.welcomeBytes = welcomeBytes902 self.ratchetTreeBytes = ratchetTreeBytes903}904905906907908}909910#if compiler(>=6)911extension MemberWelcome: Sendable {}912#endif913914#if swift(>=5.8)915@_documentation(visibility: private)916#endif917public struct FfiConverterTypeMemberWelcome: FfiConverterRustBuffer {918 public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> MemberWelcome {919 return920 try MemberWelcome(921 peerId: FfiConverterString.read(from:&buf),922 welcomeBytes: FfiConverterData.read(from:&buf),923 ratchetTreeBytes: FfiConverterData.read(from:&buf)924)925}926927 public static func write(_ value: MemberWelcome, into buf: inout [UInt8]){928 FfiConverterString.write(value.peerId, into:&buf)929 FfiConverterData.write(value.welcomeBytes, into:&buf)930 FfiConverterData.write(value.ratchetTreeBytes, into:&buf)931}932}933934935#if swift(>=5.8)936@_documentation(visibility: private)937#endif938public func FfiConverterTypeMemberWelcome_lift(_ buf: RustBuffer) throws -> MemberWelcome {939 return try FfiConverterTypeMemberWelcome.lift(buf)940}941942#if swift(>=5.8)943@_documentation(visibility: private)944#endif945public func FfiConverterTypeMemberWelcome_lower(_ value: MemberWelcome)-> RustBuffer {946 return FfiConverterTypeMemberWelcome.lower(value)947}948949950public struct ProcessedCommit: Equatable, Hashable {951 public var addedPeerIds:[String]952 public var removedPeerIds:[String]953 public var newGroupState: Data954 public var newEpoch: UInt64955956// Default memberwise initializers are never public by default, so we957// declare one manually.958 public init(addedPeerIds:[String], removedPeerIds:[String], newGroupState: Data, newEpoch: UInt64){959 self.addedPeerIds = addedPeerIds960 self.removedPeerIds = removedPeerIds961 self.newGroupState = newGroupState962 self.newEpoch = newEpoch963}964965966967968}969970#if compiler(>=6)971extension ProcessedCommit: Sendable {}972#endif973974#if swift(>=5.8)975@_documentation(visibility: private)976#endif977public struct FfiConverterTypeProcessedCommit: FfiConverterRustBuffer {978 public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> ProcessedCommit {979 return980 try ProcessedCommit(981 addedPeerIds: FfiConverterSequenceString.read(from:&buf),982 removedPeerIds: FfiConverterSequenceString.read(from:&buf),983 newGroupState: FfiConverterData.read(from:&buf),984 newEpoch: FfiConverterUInt64.read(from:&buf)985)986}987988 public static func write(_ value: ProcessedCommit, into buf: inout [UInt8]){989 FfiConverterSequenceString.write(value.addedPeerIds, into:&buf)990 FfiConverterSequenceString.write(value.removedPeerIds, into:&buf)991 FfiConverterData.write(value.newGroupState, into:&buf)992 FfiConverterUInt64.write(value.newEpoch, into:&buf)993}994}995996997#if swift(>=5.8)998@_documentation(visibility: private)999#endif1000public func FfiConverterTypeProcessedCommit_lift(_ buf: RustBuffer) throws -> ProcessedCommit {1001 return try FfiConverterTypeProcessedCommit.lift(buf)1002}10031004#if swift(>=5.8)1005@_documentation(visibility: private)1006#endif1007public func FfiConverterTypeProcessedCommit_lower(_ value: ProcessedCommit)-> RustBuffer {1008 return FfiConverterTypeProcessedCommit.lower(value)1009}101010111012public enum MlsError: Swift.Error, Equatable, Hashable, Foundation.LocalizedError {1013101410151016 case Protocol(msg: String1017)1018 case DecryptionFailed1019 case InvalidState(msg: String1020)1021 case Serialization(msg: String1022)1023 case UnknownMember1024 case InvalidCommit1025102610271028102910301031 public var errorDescription: String?{1032 String(reflecting: self)1033}10341035}10361037#if compiler(>=6)1038extension MlsError: Sendable {}1039#endif10401041#if swift(>=5.8)1042@_documentation(visibility: private)1043#endif1044public struct FfiConverterTypeMlsError: FfiConverterRustBuffer {1045 typealias SwiftType = MlsError10461047 public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> MlsError {1048 let variant: Int32 = try readInt(&buf)1049 switch variant {10501051105210531054 case 1: return .Protocol(1055 msg: try FfiConverterString.read(from:&buf)1056)1057 case 2: return .DecryptionFailed1058 case 3: return .InvalidState(1059 msg: try FfiConverterString.read(from:&buf)1060)1061 case 4: return .Serialization(1062 msg: try FfiConverterString.read(from:&buf)1063)1064 case 5: return .UnknownMember1065 case 6: return .InvalidCommit10661067 default: throw UniffiInternalError.unexpectedEnumCase1068}1069}10701071 public static func write(_ value: MlsError, into buf: inout [UInt8]){1072 switch value {107310741075107610771078 case let .Protocol(msg):1079 writeInt(&buf, Int32(1))1080 FfiConverterString.write(msg, into:&buf)108110821083 case .DecryptionFailed:1084 writeInt(&buf, Int32(2))108510861087 case let .InvalidState(msg):1088 writeInt(&buf, Int32(3))1089 FfiConverterString.write(msg, into:&buf)109010911092 case let .Serialization(msg):1093 writeInt(&buf, Int32(4))1094 FfiConverterString.write(msg, into:&buf)109510961097 case .UnknownMember:1098 writeInt(&buf, Int32(5))109911001101 case .InvalidCommit:1102 writeInt(&buf, Int32(6))11031104}1105}1106}110711081109#if swift(>=5.8)1110@_documentation(visibility: private)1111#endif1112public func FfiConverterTypeMlsError_lift(_ buf: RustBuffer) throws -> MlsError {1113 return try FfiConverterTypeMlsError.lift(buf)1114}11151116#if swift(>=5.8)1117@_documentation(visibility: private)1118#endif1119public func FfiConverterTypeMlsError_lower(_ value: MlsError)-> RustBuffer {1120 return FfiConverterTypeMlsError.lower(value)1121}11221123#if swift(>=5.8)1124@_documentation(visibility: private)1125#endif1126fileprivate struct FfiConverterOptionData: FfiConverterRustBuffer {1127 typealias SwiftType = Data?11281129 public static func write(_ value: SwiftType, into buf: inout [UInt8]){1130 guard let value = value else {1131 writeInt(&buf, Int8(0))1132 return1133}1134 writeInt(&buf, Int8(1))1135 FfiConverterData.write(value, into:&buf)1136}11371138 public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType {1139 switch try readInt(&buf) as Int8 {1140 case 0: return nil1141 case 1: return try FfiConverterData.read(from:&buf)1142 default: throw UniffiInternalError.unexpectedOptionalTag1143}1144}1145}11461147#if swift(>=5.8)1148@_documentation(visibility: private)1149#endif1150fileprivate struct FfiConverterSequenceString: FfiConverterRustBuffer {1151 typealias SwiftType =[String]11521153 public static func write(_ value:[String], into buf: inout [UInt8]){1154 let len = Int32(value.count)1155 writeInt(&buf, len)1156 for item in value {1157 FfiConverterString.write(item, into:&buf)1158}1159}11601161 public static func read(from buf: inout (data: Data, offset: Data.Index)) throws ->[String]{1162 let len: Int32 = try readInt(&buf)1163 var seq =[String]()1164 seq.reserveCapacity(Int(len))1165 for _ in 0 ..< len {1166 seq.append(try FfiConverterString.read(from:&buf))1167}1168 return seq1169}1170}11711172#if swift(>=5.8)1173@_documentation(visibility: private)1174#endif1175fileprivate struct FfiConverterSequenceData: FfiConverterRustBuffer {1176 typealias SwiftType =[Data]11771178 public static func write(_ value:[Data], into buf: inout [UInt8]){1179 let len = Int32(value.count)1180 writeInt(&buf, len)1181 for item in value {1182 FfiConverterData.write(item, into:&buf)1183}1184}11851186 public static func read(from buf: inout (data: Data, offset: Data.Index)) throws ->[Data]{1187 let len: Int32 = try readInt(&buf)1188 var seq =[Data]()1189 seq.reserveCapacity(Int(len))1190 for _ in 0 ..< len {1191 seq.append(try FfiConverterData.read(from:&buf))1192}1193 return seq1194}1195}11961197#if swift(>=5.8)1198@_documentation(visibility: private)1199#endif1200fileprivate struct FfiConverterSequenceTypeMemberWelcome: FfiConverterRustBuffer {1201 typealias SwiftType =[MemberWelcome]12021203 public static func write(_ value:[MemberWelcome], into buf: inout [UInt8]){1204 let len = Int32(value.count)1205 writeInt(&buf, len)1206 for item in value {1207 FfiConverterTypeMemberWelcome.write(item, into:&buf)1208}1209}12101211 public static func read(from buf: inout (data: Data, offset: Data.Index)) throws ->[MemberWelcome]{1212 let len: Int32 = try readInt(&buf)1213 var seq =[MemberWelcome]()1214 seq.reserveCapacity(Int(len))1215 for _ in 0 ..< len {1216 seq.append(try FfiConverterTypeMemberWelcome.read(from:&buf))1217}1218 return seq1219}1220}1221/**1222 * Add a member (coordinator only). Returns commit to broadcast + welcome to send.1223 */1224public func mlsAddMember(client: MlsClientHandle, groupId: Data, groupState: Data, keyPackageBytes: Data)throws -> CommitOutput {1225 return try FfiConverterTypeCommitOutput_lift(try rustCallWithError(FfiConverterTypeMlsError_lift){1226 uniffi_sophax_mls_fn_func_mls_add_member(1227 FfiConverterTypeMlsClientHandle_lower(client),1228 FfiConverterData.lower(groupId),1229 FfiConverterData.lower(groupState),1230 FfiConverterData.lower(keyPackageBytes),$01231)1232})1233}1234/**1235 * Create a new MLS group (coordinator only).1236 */1237public func mlsCreateGroup(client: MlsClientHandle, groupId: Data, memberKeyPackages:[Data])throws -> CreateGroupOutput {1238 return try FfiConverterTypeCreateGroupOutput_lift(try rustCallWithError(FfiConverterTypeMlsError_lift){1239 uniffi_sophax_mls_fn_func_mls_create_group(1240 FfiConverterTypeMlsClientHandle_lower(client),1241 FfiConverterData.lower(groupId),1242 FfiConverterSequenceData.lower(memberKeyPackages),$01243)1244})1245}1246/**1247 * Decrypt an MLS application message.1248 */1249public func mlsDecrypt(client: MlsClientHandle, groupId: Data, groupState: Data, ciphertext: Data)throws -> DecryptOutput {1250 return try FfiConverterTypeDecryptOutput_lift(try rustCallWithError(FfiConverterTypeMlsError_lift){1251 uniffi_sophax_mls_fn_func_mls_decrypt(1252 FfiConverterTypeMlsClientHandle_lower(client),1253 FfiConverterData.lower(groupId),1254 FfiConverterData.lower(groupState),1255 FfiConverterData.lower(ciphertext),$01256)1257})1258}1259/**1260 * Encrypt an application message.1261 */1262public func mlsEncrypt(client: MlsClientHandle, groupId: Data, groupState: Data, plaintext: Data)throws -> EncryptOutput {1263 return try FfiConverterTypeEncryptOutput_lift(try rustCallWithError(FfiConverterTypeMlsError_lift){1264 uniffi_sophax_mls_fn_func_mls_encrypt(1265 FfiConverterTypeMlsClientHandle_lower(client),1266 FfiConverterData.lower(groupId),1267 FfiConverterData.lower(groupState),1268 FfiConverterData.lower(plaintext),$01269)1270})1271}1272/**1273 * Process a Commit broadcast (coordinator's output). Validates coordinator in Swift.1274 */1275public func mlsProcessCommit(client: MlsClientHandle, groupId: Data, groupState: Data, commitBytes: Data)throws -> ProcessedCommit {1276 return try FfiConverterTypeProcessedCommit_lift(try rustCallWithError(FfiConverterTypeMlsError_lift){1277 uniffi_sophax_mls_fn_func_mls_process_commit(1278 FfiConverterTypeMlsClientHandle_lower(client),1279 FfiConverterData.lower(groupId),1280 FfiConverterData.lower(groupState),1281 FfiConverterData.lower(commitBytes),$01282)1283})1284}1285/**1286 * Join a group by processing a Welcome message.1287 */1288public func mlsProcessWelcome(client: MlsClientHandle, welcomeBytes: Data, ratchetTreeBytes: Data?)throws -> Data {1289 return try FfiConverterData.lift(try rustCallWithError(FfiConverterTypeMlsError_lift){1290 uniffi_sophax_mls_fn_func_mls_process_welcome(1291 FfiConverterTypeMlsClientHandle_lower(client),1292 FfiConverterData.lower(welcomeBytes),1293 FfiConverterOptionData.lower(ratchetTreeBytes),$01294)1295})1296}1297/**1298 * Remove a member (coordinator only).1299 */1300public func mlsRemoveMember(client: MlsClientHandle, groupId: Data, groupState: Data, peerId: String)throws -> CommitOutput {1301 return try FfiConverterTypeCommitOutput_lift(try rustCallWithError(FfiConverterTypeMlsError_lift){1302 uniffi_sophax_mls_fn_func_mls_remove_member(1303 FfiConverterTypeMlsClientHandle_lower(client),1304 FfiConverterData.lower(groupId),1305 FfiConverterData.lower(groupState),1306 FfiConverterString.lower(peerId),$01307)1308})1309}13101311private enum InitializationResult {1312 case ok1313 case contractVersionMismatch1314 case apiChecksumMismatch1315}1316// Use a global variable to perform the versioning checks. Swift ensures that1317// the code inside is only computed once.1318private let initializationResult: InitializationResult ={1319// Get the bindings contract version from our ComponentInterface1320 let bindings_contract_version = 301321// Get the scaffolding contract version by calling the into the dylib1322 let scaffolding_contract_version = ffi_sophax_mls_uniffi_contract_version()1323 if bindings_contract_version != scaffolding_contract_version {1324 return InitializationResult.contractVersionMismatch1325}1326 if (uniffi_sophax_mls_checksum_func_mls_add_member()!= 11557){1327 return InitializationResult.apiChecksumMismatch1328}1329 if (uniffi_sophax_mls_checksum_func_mls_create_group()!= 24513){1330 return InitializationResult.apiChecksumMismatch1331}1332 if (uniffi_sophax_mls_checksum_func_mls_decrypt()!= 8372){1333 return InitializationResult.apiChecksumMismatch1334}1335 if (uniffi_sophax_mls_checksum_func_mls_encrypt()!= 34671){1336 return InitializationResult.apiChecksumMismatch1337}1338 if (uniffi_sophax_mls_checksum_func_mls_process_commit()!= 23788){1339 return InitializationResult.apiChecksumMismatch1340}1341 if (uniffi_sophax_mls_checksum_func_mls_process_welcome()!= 40331){1342 return InitializationResult.apiChecksumMismatch1343}1344 if (uniffi_sophax_mls_checksum_func_mls_remove_member()!= 29216){1345 return InitializationResult.apiChecksumMismatch1346}1347 if (uniffi_sophax_mls_checksum_method_mlsclienthandle_generate_key_package()!= 18079){1348 return InitializationResult.apiChecksumMismatch1349}1350 if (uniffi_sophax_mls_checksum_constructor_mlsclienthandle_new()!= 51287){1351 return InitializationResult.apiChecksumMismatch1352}13531354 return InitializationResult.ok1355}()13561357// Make the ensure init function public so that other modules which have external type references to1358// our types can call it.1359public func uniffiEnsureSophaxMlsInitialized(){1360 switch initializationResult {1361 case .ok:1362 break1363 case .contractVersionMismatch:1364 fatalError("UniFFI contract version mismatch: try cleaning and rebuilding your project")1365 case .apiChecksumMismatch:1366 fatalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project")1367}1368}13691370// swiftlint:enable all