// This file was autogenerated by some hot garbage in the `uniffi` crate.
// Trust me, you don't want to mess with it!

// swiftlint:disable all
import Foundation

// Depending on the consumer's build setup, the low-level FFI code
// might be in a separate module, or it might be compiled inline into
// this module. This is a bit of light hackery to work with both.
#if canImport(allourthings_coreFFI)
    import allourthings_coreFFI
#endif

private extension RustBuffer {
    /// Allocate a new buffer, copying the contents of a `UInt8` array.
    init(bytes: [UInt8]) {
        let rbuf = bytes.withUnsafeBufferPointer { ptr in
            RustBuffer.from(ptr)
        }
        self.init(capacity: rbuf.capacity, len: rbuf.len, data: rbuf.data)
    }

    static func empty() -> RustBuffer {
        RustBuffer(capacity: 0, len: 0, data: nil)
    }

    static func from(_ ptr: UnsafeBufferPointer<UInt8>) -> RustBuffer {
        try! rustCall { ffi_allourthings_core_rustbuffer_from_bytes(ForeignBytes(bufferPointer: ptr), $0) }
    }

    /// Frees the buffer in place.
    /// The buffer must not be used after this is called.
    func deallocate() {
        try! rustCall { ffi_allourthings_core_rustbuffer_free(self, $0) }
    }
}

private extension ForeignBytes {
    init(bufferPointer: UnsafeBufferPointer<UInt8>) {
        self.init(len: Int32(bufferPointer.count), data: bufferPointer.baseAddress)
    }
}

// For every type used in the interface, we provide helper methods for conveniently
// lifting and lowering that type from C-compatible data, and for reading and writing
// values of that type in a buffer.

// Helper classes/extensions that don't change.
// Someday, this will be in a library of its own.

private extension Data {
    init(rustBuffer: RustBuffer) {
        self.init(
            bytesNoCopy: rustBuffer.data!,
            count: Int(rustBuffer.len),
            deallocator: .none
        )
    }
}

// Define reader functionality.  Normally this would be defined in a class or
// struct, but we use standalone functions instead in order to make external
// types work.
//
// With external types, one swift source file needs to be able to call the read
// method on another source file's FfiConverter, but then what visibility
// should Reader have?
// - If Reader is fileprivate, then this means the read() must also
//   be fileprivate, which doesn't work with external types.
// - If Reader is internal/public, we'll get compile errors since both source
//   files will try define the same type.
//
// Instead, the read() method and these helper functions input a tuple of data

private func createReader(data: Data) -> (data: Data, offset: Data.Index) {
    (data: data, offset: 0)
}

/// Reads an integer at the current offset, in big-endian order, and advances
/// the offset on success. Throws if reading the integer would move the
/// offset past the end of the buffer.
private func readInt<T: FixedWidthInteger>(_ reader: inout (data: Data, offset: Data.Index)) throws -> T {
    let range = reader.offset ..< reader.offset + MemoryLayout<T>.size
    guard reader.data.count >= range.upperBound else {
        throw UniffiInternalError.bufferOverflow
    }
    if T.self == UInt8.self {
        let value = reader.data[reader.offset]
        reader.offset += 1
        return value as! T
    }
    var value: T = 0
    let _ = withUnsafeMutableBytes(of: &value) { reader.data.copyBytes(to: $0, from: range) }
    reader.offset = range.upperBound
    return value.bigEndian
}

/// Reads an arbitrary number of bytes, to be used to read
/// raw bytes, this is useful when lifting strings
private func readBytes(_ reader: inout (data: Data, offset: Data.Index), count: Int) throws -> [UInt8] {
    let range = reader.offset ..< (reader.offset + count)
    guard reader.data.count >= range.upperBound else {
        throw UniffiInternalError.bufferOverflow
    }
    var value = [UInt8](repeating: 0, count: count)
    value.withUnsafeMutableBufferPointer { buffer in
        reader.data.copyBytes(to: buffer, from: range)
    }
    reader.offset = range.upperBound
    return value
}

/// Reads a float at the current offset.
private func readFloat(_ reader: inout (data: Data, offset: Data.Index)) throws -> Float {
    return try Float(bitPattern: readInt(&reader))
}

/// Reads a float at the current offset.
private func readDouble(_ reader: inout (data: Data, offset: Data.Index)) throws -> Double {
    return try Double(bitPattern: readInt(&reader))
}

/// Indicates if the offset has reached the end of the buffer.
private func hasRemaining(_ reader: (data: Data, offset: Data.Index)) -> Bool {
    return reader.offset < reader.data.count
}

// Define writer functionality.  Normally this would be defined in a class or
// struct, but we use standalone functions instead in order to make external
// types work.  See the above discussion on Readers for details.

private func createWriter() -> [UInt8] {
    return []
}

private func writeBytes<S: Sequence>(_ writer: inout [UInt8], _ byteArr: S) where S.Element == UInt8 {
    writer.append(contentsOf: byteArr)
}

/// Writes an integer in big-endian order.
///
/// Warning: make sure what you are trying to write
/// is in the correct type!
private func writeInt<T: FixedWidthInteger>(_ writer: inout [UInt8], _ value: T) {
    var value = value.bigEndian
    withUnsafeBytes(of: &value) { writer.append(contentsOf: $0) }
}

private func writeFloat(_ writer: inout [UInt8], _ value: Float) {
    writeInt(&writer, value.bitPattern)
}

private func writeDouble(_ writer: inout [UInt8], _ value: Double) {
    writeInt(&writer, value.bitPattern)
}

/// Protocol for types that transfer other types across the FFI. This is
/// analogous to the Rust trait of the same name.
private protocol FfiConverter {
    associatedtype FfiType
    associatedtype SwiftType

    static func lift(_ value: FfiType) throws -> SwiftType
    static func lower(_ value: SwiftType) -> FfiType
    static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType
    static func write(_ value: SwiftType, into buf: inout [UInt8])
}

/// Types conforming to `Primitive` pass themselves directly over the FFI.
private protocol FfiConverterPrimitive: FfiConverter where FfiType == SwiftType {}

extension FfiConverterPrimitive {
    #if swift(>=5.8)
        @_documentation(visibility: private)
    #endif
    public static func lift(_ value: FfiType) throws -> SwiftType {
        return value
    }

    #if swift(>=5.8)
        @_documentation(visibility: private)
    #endif
    public static func lower(_ value: SwiftType) -> FfiType {
        return value
    }
}

/// Types conforming to `FfiConverterRustBuffer` lift and lower into a `RustBuffer`.
/// Used for complex types where it's hard to write a custom lift/lower.
private protocol FfiConverterRustBuffer: FfiConverter where FfiType == RustBuffer {}

extension FfiConverterRustBuffer {
    #if swift(>=5.8)
        @_documentation(visibility: private)
    #endif
    public static func lift(_ buf: RustBuffer) throws -> SwiftType {
        var reader = createReader(data: Data(rustBuffer: buf))
        let value = try read(from: &reader)
        if hasRemaining(reader) {
            throw UniffiInternalError.incompleteData
        }
        buf.deallocate()
        return value
    }

    #if swift(>=5.8)
        @_documentation(visibility: private)
    #endif
    public static func lower(_ value: SwiftType) -> RustBuffer {
        var writer = createWriter()
        write(value, into: &writer)
        return RustBuffer(bytes: writer)
    }
}

/// An error type for FFI errors. These errors occur at the UniFFI level, not
/// the library level.
private enum UniffiInternalError: LocalizedError {
    case bufferOverflow
    case incompleteData
    case unexpectedOptionalTag
    case unexpectedEnumCase
    case unexpectedNullPointer
    case unexpectedRustCallStatusCode
    case unexpectedRustCallError
    case unexpectedStaleHandle
    case rustPanic(_ message: String)

    var errorDescription: String? {
        switch self {
        case .bufferOverflow: return "Reading the requested value would read past the end of the buffer"
        case .incompleteData: return "The buffer still has data after lifting its containing value"
        case .unexpectedOptionalTag: return "Unexpected optional tag; should be 0 or 1"
        case .unexpectedEnumCase: return "Raw enum value doesn't match any cases"
        case .unexpectedNullPointer: return "Raw pointer value was null"
        case .unexpectedRustCallStatusCode: return "Unexpected RustCallStatus code"
        case .unexpectedRustCallError: return "CALL_ERROR but no errorClass specified"
        case .unexpectedStaleHandle: return "The object in the handle map has been dropped already"
        case let .rustPanic(message): return message
        }
    }
}

private extension NSLock {
    func withLock<T>(f: () throws -> T) rethrows -> T {
        lock()
        defer { self.unlock() }
        return try f()
    }
}

private let CALL_SUCCESS: Int8 = 0
private let CALL_ERROR: Int8 = 1
private let CALL_UNEXPECTED_ERROR: Int8 = 2
private let CALL_CANCELLED: Int8 = 3

private extension RustCallStatus {
    init() {
        self.init(
            code: CALL_SUCCESS,
            errorBuf: RustBuffer(
                capacity: 0,
                len: 0,
                data: nil
            )
        )
    }
}

private func rustCall<T>(_ callback: (UnsafeMutablePointer<RustCallStatus>) -> T) throws -> T {
    let neverThrow: ((RustBuffer) throws -> Never)? = nil
    return try makeRustCall(callback, errorHandler: neverThrow)
}

private func rustCallWithError<T, E: Swift.Error>(
    _ errorHandler: @escaping (RustBuffer) throws -> E,
    _ callback: (UnsafeMutablePointer<RustCallStatus>) -> T
) throws -> T {
    try makeRustCall(callback, errorHandler: errorHandler)
}

private func makeRustCall<T, E: Swift.Error>(
    _ callback: (UnsafeMutablePointer<RustCallStatus>) -> T,
    errorHandler: ((RustBuffer) throws -> E)?
) throws -> T {
    uniffiEnsureInitialized()
    var callStatus = RustCallStatus()
    let returnedVal = callback(&callStatus)
    try uniffiCheckCallStatus(callStatus: callStatus, errorHandler: errorHandler)
    return returnedVal
}

private func uniffiCheckCallStatus<E: Swift.Error>(
    callStatus: RustCallStatus,
    errorHandler: ((RustBuffer) throws -> E)?
) throws {
    switch callStatus.code {
    case CALL_SUCCESS:
        return

    case CALL_ERROR:
        if let errorHandler = errorHandler {
            throw try errorHandler(callStatus.errorBuf)
        } else {
            callStatus.errorBuf.deallocate()
            throw UniffiInternalError.unexpectedRustCallError
        }

    case CALL_UNEXPECTED_ERROR:
        // When the rust code sees a panic, it tries to construct a RustBuffer
        // with the message.  But if that code panics, then it just sends back
        // an empty buffer.
        if callStatus.errorBuf.len > 0 {
            throw try UniffiInternalError.rustPanic(FfiConverterString.lift(callStatus.errorBuf))
        } else {
            callStatus.errorBuf.deallocate()
            throw UniffiInternalError.rustPanic("Rust panic")
        }

    case CALL_CANCELLED:
        fatalError("Cancellation not supported yet")

    default:
        throw UniffiInternalError.unexpectedRustCallStatusCode
    }
}

private func uniffiTraitInterfaceCall<T>(
    callStatus: UnsafeMutablePointer<RustCallStatus>,
    makeCall: () throws -> T,
    writeReturn: (T) -> Void
) {
    do {
        try writeReturn(makeCall())
    } catch {
        callStatus.pointee.code = CALL_UNEXPECTED_ERROR
        callStatus.pointee.errorBuf = FfiConverterString.lower(String(describing: error))
    }
}

private func uniffiTraitInterfaceCallWithError<T, E>(
    callStatus: UnsafeMutablePointer<RustCallStatus>,
    makeCall: () throws -> T,
    writeReturn: (T) -> Void,
    lowerError: (E) -> RustBuffer
) {
    do {
        try writeReturn(makeCall())
    } catch let error as E {
        callStatus.pointee.code = CALL_ERROR
        callStatus.pointee.errorBuf = lowerError(error)
    } catch {
        callStatus.pointee.code = CALL_UNEXPECTED_ERROR
        callStatus.pointee.errorBuf = FfiConverterString.lower(String(describing: error))
    }
}

private class UniffiHandleMap<T> {
    private var map: [UInt64: T] = [:]
    private let lock = NSLock()
    private var currentHandle: UInt64 = 1

    func insert(obj: T) -> UInt64 {
        lock.withLock {
            let handle = currentHandle
            currentHandle += 1
            map[handle] = obj
            return handle
        }
    }

    func get(handle: UInt64) throws -> T {
        try lock.withLock {
            guard let obj = map[handle] else {
                throw UniffiInternalError.unexpectedStaleHandle
            }
            return obj
        }
    }

    @discardableResult
    func remove(handle: UInt64) throws -> T {
        try lock.withLock {
            guard let obj = map.removeValue(forKey: handle) else {
                throw UniffiInternalError.unexpectedStaleHandle
            }
            return obj
        }
    }

    var count: Int {
        map.count
    }
}

// Public interface members begin here.

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
private struct FfiConverterDouble: FfiConverterPrimitive {
    typealias FfiType = Double
    typealias SwiftType = Double

    static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Double {
        return try lift(readDouble(&buf))
    }

    static func write(_ value: Double, into buf: inout [UInt8]) {
        writeDouble(&buf, lower(value))
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
private struct FfiConverterBool: FfiConverter {
    typealias FfiType = Int8
    typealias SwiftType = Bool

    static func lift(_ value: Int8) throws -> Bool {
        return value != 0
    }

    static func lower(_ value: Bool) -> Int8 {
        return value ? 1 : 0
    }

    static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Bool {
        return try lift(readInt(&buf))
    }

    static func write(_ value: Bool, into buf: inout [UInt8]) {
        writeInt(&buf, lower(value))
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
private struct FfiConverterString: FfiConverter {
    typealias SwiftType = String
    typealias FfiType = RustBuffer

    static func lift(_ value: RustBuffer) throws -> String {
        defer {
            value.deallocate()
        }
        if value.data == nil {
            return String()
        }
        let bytes = UnsafeBufferPointer<UInt8>(start: value.data!, count: Int(value.len))
        return String(bytes: bytes, encoding: String.Encoding.utf8)!
    }

    static func lower(_ value: String) -> RustBuffer {
        return value.utf8CString.withUnsafeBufferPointer { ptr in
            // The swift string gives us int8_t, we want uint8_t.
            ptr.withMemoryRebound(to: UInt8.self) { ptr in
                // The swift string gives us a trailing null byte, we don't want it.
                let buf = UnsafeBufferPointer(rebasing: ptr.prefix(upTo: ptr.count - 1))
                return RustBuffer.from(buf)
            }
        }
    }

    static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> String {
        let len: Int32 = try readInt(&buf)
        return try String(bytes: readBytes(&buf, count: Int(len)), encoding: String.Encoding.utf8)!
    }

    static func write(_ value: String, into buf: inout [UInt8]) {
        let len = Int32(value.utf8.count)
        writeInt(&buf, len)
        writeBytes(&buf, value.utf8)
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
private struct FfiConverterData: FfiConverterRustBuffer {
    typealias SwiftType = Data

    static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Data {
        let len: Int32 = try readInt(&buf)
        return try Data(readBytes(&buf, count: Int(len)))
    }

    static func write(_ value: Data, into buf: inout [UInt8]) {
        let len = Int32(value.count)
        writeInt(&buf, len)
        writeBytes(&buf, value)
    }
}

public protocol UniCatalogStoreProtocol: AnyObject {
    func addAttachment(itemId: String, filename: String, kind: AttachmentKind, data: Data, label: String?) throws -> UniItem

    func addItem(item: UniNewItem) throws -> UniItem

    func deleteAttachment(itemId: String, filename: String) throws -> UniItem?

    func deleteItem(id: String) throws -> Bool

    func getAttachment(itemId: String, filename: String) throws -> Data

    func getItem(idOrName: String) throws -> UniItem?

    func getItemFields() throws -> [String]

    func listItems(filter: UniListFilter?) throws -> [UniItem]

    func rebuildCache() throws

    func refresh() throws

    func searchItems(query: String) throws -> [UniItem]

    func updateItem(id: String, updates: UniItemUpdate) throws -> UniItem?
}

open class UniCatalogStore:
    UniCatalogStoreProtocol
{
    fileprivate let pointer: UnsafeMutableRawPointer!

    // Used to instantiate a [FFIObject] without an actual pointer, for fakes in tests, mostly.
    #if swift(>=5.8)
        @_documentation(visibility: private)
    #endif
    public struct NoPointer {
        public init() {}
    }

    // TODO: We'd like this to be `private` but for Swifty reasons,
    // we can't implement `FfiConverter` without making this `required` and we can't
    // make it `required` without making it `public`.
    public required init(unsafeFromRawPointer pointer: UnsafeMutableRawPointer) {
        self.pointer = pointer
    }

    // This constructor can be used to instantiate a fake object.
    // - Parameter noPointer: Placeholder value so we can have a constructor separate from the default empty one that may be implemented for classes extending [FFIObject].
    //
    // - Warning:
    //     Any object instantiated with this constructor cannot be passed to an actual Rust-backed object. Since there isn't a backing [Pointer] the FFI lower functions will crash.
    #if swift(>=5.8)
        @_documentation(visibility: private)
    #endif
    public init(noPointer _: NoPointer) {
        pointer = nil
    }

    #if swift(>=5.8)
        @_documentation(visibility: private)
    #endif
    public func uniffiClonePointer() -> UnsafeMutableRawPointer {
        return try! rustCall { uniffi_allourthings_core_fn_clone_unicatalogstore(self.pointer, $0) }
    }

    public convenience init(dataDir: String) {
        let pointer =
            try! rustCall {
                uniffi_allourthings_core_fn_constructor_unicatalogstore_new(
                    FfiConverterString.lower(dataDir), $0
                )
            }
        self.init(unsafeFromRawPointer: pointer)
    }

    deinit {
        guard let pointer = pointer else {
            return
        }

        try! rustCall { uniffi_allourthings_core_fn_free_unicatalogstore(pointer, $0) }
    }

    public static func newWithCache(dataDir: String, cacheDir: String) throws -> UniCatalogStore {
        return try FfiConverterTypeUniCatalogStore.lift(rustCallWithError(FfiConverterTypeCatalogError.lift) {
            uniffi_allourthings_core_fn_constructor_unicatalogstore_new_with_cache(
                FfiConverterString.lower(dataDir),
                FfiConverterString.lower(cacheDir), $0
            )
        })
    }

    open func addAttachment(itemId: String, filename: String, kind: AttachmentKind, data: Data, label: String?) throws -> UniItem {
        return try FfiConverterTypeUniItem.lift(rustCallWithError(FfiConverterTypeCatalogError.lift) {
            uniffi_allourthings_core_fn_method_unicatalogstore_add_attachment(self.uniffiClonePointer(),
                                                                              FfiConverterString.lower(itemId),
                                                                              FfiConverterString.lower(filename),
                                                                              FfiConverterTypeAttachmentKind.lower(kind),
                                                                              FfiConverterData.lower(data),
                                                                              FfiConverterOptionString.lower(label), $0)
        })
    }

    open func addItem(item: UniNewItem) throws -> UniItem {
        return try FfiConverterTypeUniItem.lift(rustCallWithError(FfiConverterTypeCatalogError.lift) {
            uniffi_allourthings_core_fn_method_unicatalogstore_add_item(self.uniffiClonePointer(),
                                                                        FfiConverterTypeUniNewItem.lower(item), $0)
        })
    }

    open func deleteAttachment(itemId: String, filename: String) throws -> UniItem? {
        return try FfiConverterOptionTypeUniItem.lift(rustCallWithError(FfiConverterTypeCatalogError.lift) {
            uniffi_allourthings_core_fn_method_unicatalogstore_delete_attachment(self.uniffiClonePointer(),
                                                                                 FfiConverterString.lower(itemId),
                                                                                 FfiConverterString.lower(filename), $0)
        })
    }

    open func deleteItem(id: String) throws -> Bool {
        return try FfiConverterBool.lift(rustCallWithError(FfiConverterTypeCatalogError.lift) {
            uniffi_allourthings_core_fn_method_unicatalogstore_delete_item(self.uniffiClonePointer(),
                                                                           FfiConverterString.lower(id), $0)
        })
    }

    open func getAttachment(itemId: String, filename: String) throws -> Data {
        return try FfiConverterData.lift(rustCallWithError(FfiConverterTypeCatalogError.lift) {
            uniffi_allourthings_core_fn_method_unicatalogstore_get_attachment(self.uniffiClonePointer(),
                                                                              FfiConverterString.lower(itemId),
                                                                              FfiConverterString.lower(filename), $0)
        })
    }

    open func getItem(idOrName: String) throws -> UniItem? {
        return try FfiConverterOptionTypeUniItem.lift(rustCallWithError(FfiConverterTypeCatalogError.lift) {
            uniffi_allourthings_core_fn_method_unicatalogstore_get_item(self.uniffiClonePointer(),
                                                                        FfiConverterString.lower(idOrName), $0)
        })
    }

    open func getItemFields() throws -> [String] {
        return try FfiConverterSequenceString.lift(rustCallWithError(FfiConverterTypeCatalogError.lift) {
            uniffi_allourthings_core_fn_method_unicatalogstore_get_item_fields(self.uniffiClonePointer(), $0)
        })
    }

    open func listItems(filter: UniListFilter?) throws -> [UniItem] {
        return try FfiConverterSequenceTypeUniItem.lift(rustCallWithError(FfiConverterTypeCatalogError.lift) {
            uniffi_allourthings_core_fn_method_unicatalogstore_list_items(self.uniffiClonePointer(),
                                                                          FfiConverterOptionTypeUniListFilter.lower(filter), $0)
        })
    }

    open func rebuildCache() throws {
        try rustCallWithError(FfiConverterTypeCatalogError.lift) {
            uniffi_allourthings_core_fn_method_unicatalogstore_rebuild_cache(self.uniffiClonePointer(), $0)
        }
    }

    open func refresh() throws {
        try rustCallWithError(FfiConverterTypeCatalogError.lift) {
            uniffi_allourthings_core_fn_method_unicatalogstore_refresh(self.uniffiClonePointer(), $0)
        }
    }

    open func searchItems(query: String) throws -> [UniItem] {
        return try FfiConverterSequenceTypeUniItem.lift(rustCallWithError(FfiConverterTypeCatalogError.lift) {
            uniffi_allourthings_core_fn_method_unicatalogstore_search_items(self.uniffiClonePointer(),
                                                                            FfiConverterString.lower(query), $0)
        })
    }

    open func updateItem(id: String, updates: UniItemUpdate) throws -> UniItem? {
        return try FfiConverterOptionTypeUniItem.lift(rustCallWithError(FfiConverterTypeCatalogError.lift) {
            uniffi_allourthings_core_fn_method_unicatalogstore_update_item(self.uniffiClonePointer(),
                                                                           FfiConverterString.lower(id),
                                                                           FfiConverterTypeUniItemUpdate.lower(updates), $0)
        })
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public struct FfiConverterTypeUniCatalogStore: FfiConverter {
    typealias FfiType = UnsafeMutableRawPointer
    typealias SwiftType = UniCatalogStore

    public static func lift(_ pointer: UnsafeMutableRawPointer) throws -> UniCatalogStore {
        return UniCatalogStore(unsafeFromRawPointer: pointer)
    }

    public static func lower(_ value: UniCatalogStore) -> UnsafeMutableRawPointer {
        return value.uniffiClonePointer()
    }

    public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> UniCatalogStore {
        let v: UInt64 = try readInt(&buf)
        // The Rust code won't compile if a pointer won't fit in a UInt64.
        // We have to go via `UInt` because that's the thing that's the size of a pointer.
        let ptr = UnsafeMutableRawPointer(bitPattern: UInt(truncatingIfNeeded: v))
        if ptr == nil {
            throw UniffiInternalError.unexpectedNullPointer
        }
        return try lift(ptr!)
    }

    public static func write(_ value: UniCatalogStore, into buf: inout [UInt8]) {
        // This fiddling is because `Int` is the thing that's the same size as a pointer.
        // The Rust code won't compile if a pointer won't fit in a `UInt64`.
        writeInt(&buf, UInt64(bitPattern: Int64(Int(bitPattern: lower(value)))))
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public func FfiConverterTypeUniCatalogStore_lift(_ pointer: UnsafeMutableRawPointer) throws -> UniCatalogStore {
    return try FfiConverterTypeUniCatalogStore.lift(pointer)
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public func FfiConverterTypeUniCatalogStore_lower(_ value: UniCatalogStore) -> UnsafeMutableRawPointer {
    return FfiConverterTypeUniCatalogStore.lower(value)
}

public struct UniAttachment {
    public var filename: String
    public var kind: AttachmentKind
    public var label: String?

    /// Default memberwise initializers are never public by default, so we
    /// declare one manually.
    public init(filename: String, kind: AttachmentKind, label: String?) {
        self.filename = filename
        self.kind = kind
        self.label = label
    }
}

extension UniAttachment: Equatable, Hashable {
    public static func == (lhs: UniAttachment, rhs: UniAttachment) -> Bool {
        if lhs.filename != rhs.filename {
            return false
        }
        if lhs.kind != rhs.kind {
            return false
        }
        if lhs.label != rhs.label {
            return false
        }
        return true
    }

    public func hash(into hasher: inout Hasher) {
        hasher.combine(filename)
        hasher.combine(kind)
        hasher.combine(label)
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public struct FfiConverterTypeUniAttachment: FfiConverterRustBuffer {
    public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> UniAttachment {
        return
            try UniAttachment(
                filename: FfiConverterString.read(from: &buf),
                kind: FfiConverterTypeAttachmentKind.read(from: &buf),
                label: FfiConverterOptionString.read(from: &buf)
            )
    }

    public static func write(_ value: UniAttachment, into buf: inout [UInt8]) {
        FfiConverterString.write(value.filename, into: &buf)
        FfiConverterTypeAttachmentKind.write(value.kind, into: &buf)
        FfiConverterOptionString.write(value.label, into: &buf)
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public func FfiConverterTypeUniAttachment_lift(_ buf: RustBuffer) throws -> UniAttachment {
    return try FfiConverterTypeUniAttachment.lift(buf)
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public func FfiConverterTypeUniAttachment_lower(_ value: UniAttachment) -> RustBuffer {
    return FfiConverterTypeUniAttachment.lower(value)
}

public struct UniItem {
    public var id: String
    public var name: String
    public var createdAt: String
    public var updatedAt: String
    public var category: String?
    public var subcategory: String?
    public var brand: String?
    public var model: String?
    public var purchaseDate: String?
    public var purchasePrice: Double?
    public var currency: String?
    public var warrantyExpires: String?
    public var retailer: String?
    public var location: String?
    public var features: [String]?
    public var notes: String?
    public var tags: [String]?
    public var attachments: [UniAttachment]?
    /**
     * JSON-encoded passthrough custom fields (e.g. `{}`).
     */
    public var extraJson: String

    /// Default memberwise initializers are never public by default, so we
    /// declare one manually.
    public init(id: String, name: String, createdAt: String, updatedAt: String, category: String?, subcategory: String?, brand: String?, model: String?, purchaseDate: String?, purchasePrice: Double?, currency: String?, warrantyExpires: String?, retailer: String?, location: String?, features: [String]?, notes: String?, tags: [String]?, attachments: [UniAttachment]?,
                /* 
                    * JSON-encoded passthrough custom fields (e.g. `{}`).
                    */ extraJson: String)
    {
        self.id = id
        self.name = name
        self.createdAt = createdAt
        self.updatedAt = updatedAt
        self.category = category
        self.subcategory = subcategory
        self.brand = brand
        self.model = model
        self.purchaseDate = purchaseDate
        self.purchasePrice = purchasePrice
        self.currency = currency
        self.warrantyExpires = warrantyExpires
        self.retailer = retailer
        self.location = location
        self.features = features
        self.notes = notes
        self.tags = tags
        self.attachments = attachments
        self.extraJson = extraJson
    }
}

extension UniItem: Equatable, Hashable {
    public static func == (lhs: UniItem, rhs: UniItem) -> Bool {
        if lhs.id != rhs.id {
            return false
        }
        if lhs.name != rhs.name {
            return false
        }
        if lhs.createdAt != rhs.createdAt {
            return false
        }
        if lhs.updatedAt != rhs.updatedAt {
            return false
        }
        if lhs.category != rhs.category {
            return false
        }
        if lhs.subcategory != rhs.subcategory {
            return false
        }
        if lhs.brand != rhs.brand {
            return false
        }
        if lhs.model != rhs.model {
            return false
        }
        if lhs.purchaseDate != rhs.purchaseDate {
            return false
        }
        if lhs.purchasePrice != rhs.purchasePrice {
            return false
        }
        if lhs.currency != rhs.currency {
            return false
        }
        if lhs.warrantyExpires != rhs.warrantyExpires {
            return false
        }
        if lhs.retailer != rhs.retailer {
            return false
        }
        if lhs.location != rhs.location {
            return false
        }
        if lhs.features != rhs.features {
            return false
        }
        if lhs.notes != rhs.notes {
            return false
        }
        if lhs.tags != rhs.tags {
            return false
        }
        if lhs.attachments != rhs.attachments {
            return false
        }
        if lhs.extraJson != rhs.extraJson {
            return false
        }
        return true
    }

    public func hash(into hasher: inout Hasher) {
        hasher.combine(id)
        hasher.combine(name)
        hasher.combine(createdAt)
        hasher.combine(updatedAt)
        hasher.combine(category)
        hasher.combine(subcategory)
        hasher.combine(brand)
        hasher.combine(model)
        hasher.combine(purchaseDate)
        hasher.combine(purchasePrice)
        hasher.combine(currency)
        hasher.combine(warrantyExpires)
        hasher.combine(retailer)
        hasher.combine(location)
        hasher.combine(features)
        hasher.combine(notes)
        hasher.combine(tags)
        hasher.combine(attachments)
        hasher.combine(extraJson)
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public struct FfiConverterTypeUniItem: FfiConverterRustBuffer {
    public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> UniItem {
        return
            try UniItem(
                id: FfiConverterString.read(from: &buf),
                name: FfiConverterString.read(from: &buf),
                createdAt: FfiConverterString.read(from: &buf),
                updatedAt: FfiConverterString.read(from: &buf),
                category: FfiConverterOptionString.read(from: &buf),
                subcategory: FfiConverterOptionString.read(from: &buf),
                brand: FfiConverterOptionString.read(from: &buf),
                model: FfiConverterOptionString.read(from: &buf),
                purchaseDate: FfiConverterOptionString.read(from: &buf),
                purchasePrice: FfiConverterOptionDouble.read(from: &buf),
                currency: FfiConverterOptionString.read(from: &buf),
                warrantyExpires: FfiConverterOptionString.read(from: &buf),
                retailer: FfiConverterOptionString.read(from: &buf),
                location: FfiConverterOptionString.read(from: &buf),
                features: FfiConverterOptionSequenceString.read(from: &buf),
                notes: FfiConverterOptionString.read(from: &buf),
                tags: FfiConverterOptionSequenceString.read(from: &buf),
                attachments: FfiConverterOptionSequenceTypeUniAttachment.read(from: &buf),
                extraJson: FfiConverterString.read(from: &buf)
            )
    }

    public static func write(_ value: UniItem, into buf: inout [UInt8]) {
        FfiConverterString.write(value.id, into: &buf)
        FfiConverterString.write(value.name, into: &buf)
        FfiConverterString.write(value.createdAt, into: &buf)
        FfiConverterString.write(value.updatedAt, into: &buf)
        FfiConverterOptionString.write(value.category, into: &buf)
        FfiConverterOptionString.write(value.subcategory, into: &buf)
        FfiConverterOptionString.write(value.brand, into: &buf)
        FfiConverterOptionString.write(value.model, into: &buf)
        FfiConverterOptionString.write(value.purchaseDate, into: &buf)
        FfiConverterOptionDouble.write(value.purchasePrice, into: &buf)
        FfiConverterOptionString.write(value.currency, into: &buf)
        FfiConverterOptionString.write(value.warrantyExpires, into: &buf)
        FfiConverterOptionString.write(value.retailer, into: &buf)
        FfiConverterOptionString.write(value.location, into: &buf)
        FfiConverterOptionSequenceString.write(value.features, into: &buf)
        FfiConverterOptionString.write(value.notes, into: &buf)
        FfiConverterOptionSequenceString.write(value.tags, into: &buf)
        FfiConverterOptionSequenceTypeUniAttachment.write(value.attachments, into: &buf)
        FfiConverterString.write(value.extraJson, into: &buf)
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public func FfiConverterTypeUniItem_lift(_ buf: RustBuffer) throws -> UniItem {
    return try FfiConverterTypeUniItem.lift(buf)
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public func FfiConverterTypeUniItem_lower(_ value: UniItem) -> RustBuffer {
    return FfiConverterTypeUniItem.lower(value)
}

public struct UniItemUpdate {
    public var name: String?
    public var category: String?
    public var subcategory: String?
    public var brand: String?
    public var model: String?
    public var purchaseDate: String?
    public var purchasePrice: Double?
    public var currency: String?
    public var warrantyExpires: String?
    public var retailer: String?
    public var location: String?
    public var features: [String]?
    public var notes: String?
    public var tags: [String]?
    public var attachments: [UniAttachment]?
    /**
     * JSON-encoded extra fields to merge in (pass `"{}"` to change nothing).
     */
    public var extraJson: String

    /// Default memberwise initializers are never public by default, so we
    /// declare one manually.
    public init(name: String?, category: String?, subcategory: String?, brand: String?, model: String?, purchaseDate: String?, purchasePrice: Double?, currency: String?, warrantyExpires: String?, retailer: String?, location: String?, features: [String]?, notes: String?, tags: [String]?, attachments: [UniAttachment]?,
                /* 
                    * JSON-encoded extra fields to merge in (pass `"{}"` to change nothing).
                    */ extraJson: String)
    {
        self.name = name
        self.category = category
        self.subcategory = subcategory
        self.brand = brand
        self.model = model
        self.purchaseDate = purchaseDate
        self.purchasePrice = purchasePrice
        self.currency = currency
        self.warrantyExpires = warrantyExpires
        self.retailer = retailer
        self.location = location
        self.features = features
        self.notes = notes
        self.tags = tags
        self.attachments = attachments
        self.extraJson = extraJson
    }
}

extension UniItemUpdate: Equatable, Hashable {
    public static func == (lhs: UniItemUpdate, rhs: UniItemUpdate) -> Bool {
        if lhs.name != rhs.name {
            return false
        }
        if lhs.category != rhs.category {
            return false
        }
        if lhs.subcategory != rhs.subcategory {
            return false
        }
        if lhs.brand != rhs.brand {
            return false
        }
        if lhs.model != rhs.model {
            return false
        }
        if lhs.purchaseDate != rhs.purchaseDate {
            return false
        }
        if lhs.purchasePrice != rhs.purchasePrice {
            return false
        }
        if lhs.currency != rhs.currency {
            return false
        }
        if lhs.warrantyExpires != rhs.warrantyExpires {
            return false
        }
        if lhs.retailer != rhs.retailer {
            return false
        }
        if lhs.location != rhs.location {
            return false
        }
        if lhs.features != rhs.features {
            return false
        }
        if lhs.notes != rhs.notes {
            return false
        }
        if lhs.tags != rhs.tags {
            return false
        }
        if lhs.attachments != rhs.attachments {
            return false
        }
        if lhs.extraJson != rhs.extraJson {
            return false
        }
        return true
    }

    public func hash(into hasher: inout Hasher) {
        hasher.combine(name)
        hasher.combine(category)
        hasher.combine(subcategory)
        hasher.combine(brand)
        hasher.combine(model)
        hasher.combine(purchaseDate)
        hasher.combine(purchasePrice)
        hasher.combine(currency)
        hasher.combine(warrantyExpires)
        hasher.combine(retailer)
        hasher.combine(location)
        hasher.combine(features)
        hasher.combine(notes)
        hasher.combine(tags)
        hasher.combine(attachments)
        hasher.combine(extraJson)
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public struct FfiConverterTypeUniItemUpdate: FfiConverterRustBuffer {
    public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> UniItemUpdate {
        return
            try UniItemUpdate(
                name: FfiConverterOptionString.read(from: &buf),
                category: FfiConverterOptionString.read(from: &buf),
                subcategory: FfiConverterOptionString.read(from: &buf),
                brand: FfiConverterOptionString.read(from: &buf),
                model: FfiConverterOptionString.read(from: &buf),
                purchaseDate: FfiConverterOptionString.read(from: &buf),
                purchasePrice: FfiConverterOptionDouble.read(from: &buf),
                currency: FfiConverterOptionString.read(from: &buf),
                warrantyExpires: FfiConverterOptionString.read(from: &buf),
                retailer: FfiConverterOptionString.read(from: &buf),
                location: FfiConverterOptionString.read(from: &buf),
                features: FfiConverterOptionSequenceString.read(from: &buf),
                notes: FfiConverterOptionString.read(from: &buf),
                tags: FfiConverterOptionSequenceString.read(from: &buf),
                attachments: FfiConverterOptionSequenceTypeUniAttachment.read(from: &buf),
                extraJson: FfiConverterString.read(from: &buf)
            )
    }

    public static func write(_ value: UniItemUpdate, into buf: inout [UInt8]) {
        FfiConverterOptionString.write(value.name, into: &buf)
        FfiConverterOptionString.write(value.category, into: &buf)
        FfiConverterOptionString.write(value.subcategory, into: &buf)
        FfiConverterOptionString.write(value.brand, into: &buf)
        FfiConverterOptionString.write(value.model, into: &buf)
        FfiConverterOptionString.write(value.purchaseDate, into: &buf)
        FfiConverterOptionDouble.write(value.purchasePrice, into: &buf)
        FfiConverterOptionString.write(value.currency, into: &buf)
        FfiConverterOptionString.write(value.warrantyExpires, into: &buf)
        FfiConverterOptionString.write(value.retailer, into: &buf)
        FfiConverterOptionString.write(value.location, into: &buf)
        FfiConverterOptionSequenceString.write(value.features, into: &buf)
        FfiConverterOptionString.write(value.notes, into: &buf)
        FfiConverterOptionSequenceString.write(value.tags, into: &buf)
        FfiConverterOptionSequenceTypeUniAttachment.write(value.attachments, into: &buf)
        FfiConverterString.write(value.extraJson, into: &buf)
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public func FfiConverterTypeUniItemUpdate_lift(_ buf: RustBuffer) throws -> UniItemUpdate {
    return try FfiConverterTypeUniItemUpdate.lift(buf)
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public func FfiConverterTypeUniItemUpdate_lower(_ value: UniItemUpdate) -> RustBuffer {
    return FfiConverterTypeUniItemUpdate.lower(value)
}

public struct UniListFilter {
    public var category: String?
    public var subcategory: String?
    public var tags: [String]?

    /// Default memberwise initializers are never public by default, so we
    /// declare one manually.
    public init(category: String?, subcategory: String?, tags: [String]?) {
        self.category = category
        self.subcategory = subcategory
        self.tags = tags
    }
}

extension UniListFilter: Equatable, Hashable {
    public static func == (lhs: UniListFilter, rhs: UniListFilter) -> Bool {
        if lhs.category != rhs.category {
            return false
        }
        if lhs.subcategory != rhs.subcategory {
            return false
        }
        if lhs.tags != rhs.tags {
            return false
        }
        return true
    }

    public func hash(into hasher: inout Hasher) {
        hasher.combine(category)
        hasher.combine(subcategory)
        hasher.combine(tags)
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public struct FfiConverterTypeUniListFilter: FfiConverterRustBuffer {
    public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> UniListFilter {
        return
            try UniListFilter(
                category: FfiConverterOptionString.read(from: &buf),
                subcategory: FfiConverterOptionString.read(from: &buf),
                tags: FfiConverterOptionSequenceString.read(from: &buf)
            )
    }

    public static func write(_ value: UniListFilter, into buf: inout [UInt8]) {
        FfiConverterOptionString.write(value.category, into: &buf)
        FfiConverterOptionString.write(value.subcategory, into: &buf)
        FfiConverterOptionSequenceString.write(value.tags, into: &buf)
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public func FfiConverterTypeUniListFilter_lift(_ buf: RustBuffer) throws -> UniListFilter {
    return try FfiConverterTypeUniListFilter.lift(buf)
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public func FfiConverterTypeUniListFilter_lower(_ value: UniListFilter) -> RustBuffer {
    return FfiConverterTypeUniListFilter.lower(value)
}

public struct UniNewItem {
    /**
     * Optional caller-supplied ID. If empty string, one is generated by the store.
     */
    public var id: String?
    public var name: String
    public var category: String?
    public var subcategory: String?
    public var brand: String?
    public var model: String?
    public var purchaseDate: String?
    public var purchasePrice: Double?
    public var currency: String?
    public var warrantyExpires: String?
    public var retailer: String?
    public var location: String?
    public var features: [String]?
    public var notes: String?
    public var tags: [String]?
    public var attachments: [UniAttachment]?
    /**
     * JSON-encoded extra fields (pass `"{}"` if none).
     */
    public var extraJson: String

    /// Default memberwise initializers are never public by default, so we
    /// declare one manually.
    public init(
        /* 
         * Optional caller-supplied ID. If empty string, one is generated by the store.
         */ id: String?, name: String, category: String?, subcategory: String?, brand: String?, model: String?, purchaseDate: String?, purchasePrice: Double?, currency: String?, warrantyExpires: String?, retailer: String?, location: String?, features: [String]?, notes: String?, tags: [String]?, attachments: [UniAttachment]?,
        /* 
            * JSON-encoded extra fields (pass `"{}"` if none).
            */ extraJson: String
    ) {
        self.id = id
        self.name = name
        self.category = category
        self.subcategory = subcategory
        self.brand = brand
        self.model = model
        self.purchaseDate = purchaseDate
        self.purchasePrice = purchasePrice
        self.currency = currency
        self.warrantyExpires = warrantyExpires
        self.retailer = retailer
        self.location = location
        self.features = features
        self.notes = notes
        self.tags = tags
        self.attachments = attachments
        self.extraJson = extraJson
    }
}

extension UniNewItem: Equatable, Hashable {
    public static func == (lhs: UniNewItem, rhs: UniNewItem) -> Bool {
        if lhs.id != rhs.id {
            return false
        }
        if lhs.name != rhs.name {
            return false
        }
        if lhs.category != rhs.category {
            return false
        }
        if lhs.subcategory != rhs.subcategory {
            return false
        }
        if lhs.brand != rhs.brand {
            return false
        }
        if lhs.model != rhs.model {
            return false
        }
        if lhs.purchaseDate != rhs.purchaseDate {
            return false
        }
        if lhs.purchasePrice != rhs.purchasePrice {
            return false
        }
        if lhs.currency != rhs.currency {
            return false
        }
        if lhs.warrantyExpires != rhs.warrantyExpires {
            return false
        }
        if lhs.retailer != rhs.retailer {
            return false
        }
        if lhs.location != rhs.location {
            return false
        }
        if lhs.features != rhs.features {
            return false
        }
        if lhs.notes != rhs.notes {
            return false
        }
        if lhs.tags != rhs.tags {
            return false
        }
        if lhs.attachments != rhs.attachments {
            return false
        }
        if lhs.extraJson != rhs.extraJson {
            return false
        }
        return true
    }

    public func hash(into hasher: inout Hasher) {
        hasher.combine(id)
        hasher.combine(name)
        hasher.combine(category)
        hasher.combine(subcategory)
        hasher.combine(brand)
        hasher.combine(model)
        hasher.combine(purchaseDate)
        hasher.combine(purchasePrice)
        hasher.combine(currency)
        hasher.combine(warrantyExpires)
        hasher.combine(retailer)
        hasher.combine(location)
        hasher.combine(features)
        hasher.combine(notes)
        hasher.combine(tags)
        hasher.combine(attachments)
        hasher.combine(extraJson)
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public struct FfiConverterTypeUniNewItem: FfiConverterRustBuffer {
    public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> UniNewItem {
        return
            try UniNewItem(
                id: FfiConverterOptionString.read(from: &buf),
                name: FfiConverterString.read(from: &buf),
                category: FfiConverterOptionString.read(from: &buf),
                subcategory: FfiConverterOptionString.read(from: &buf),
                brand: FfiConverterOptionString.read(from: &buf),
                model: FfiConverterOptionString.read(from: &buf),
                purchaseDate: FfiConverterOptionString.read(from: &buf),
                purchasePrice: FfiConverterOptionDouble.read(from: &buf),
                currency: FfiConverterOptionString.read(from: &buf),
                warrantyExpires: FfiConverterOptionString.read(from: &buf),
                retailer: FfiConverterOptionString.read(from: &buf),
                location: FfiConverterOptionString.read(from: &buf),
                features: FfiConverterOptionSequenceString.read(from: &buf),
                notes: FfiConverterOptionString.read(from: &buf),
                tags: FfiConverterOptionSequenceString.read(from: &buf),
                attachments: FfiConverterOptionSequenceTypeUniAttachment.read(from: &buf),
                extraJson: FfiConverterString.read(from: &buf)
            )
    }

    public static func write(_ value: UniNewItem, into buf: inout [UInt8]) {
        FfiConverterOptionString.write(value.id, into: &buf)
        FfiConverterString.write(value.name, into: &buf)
        FfiConverterOptionString.write(value.category, into: &buf)
        FfiConverterOptionString.write(value.subcategory, into: &buf)
        FfiConverterOptionString.write(value.brand, into: &buf)
        FfiConverterOptionString.write(value.model, into: &buf)
        FfiConverterOptionString.write(value.purchaseDate, into: &buf)
        FfiConverterOptionDouble.write(value.purchasePrice, into: &buf)
        FfiConverterOptionString.write(value.currency, into: &buf)
        FfiConverterOptionString.write(value.warrantyExpires, into: &buf)
        FfiConverterOptionString.write(value.retailer, into: &buf)
        FfiConverterOptionString.write(value.location, into: &buf)
        FfiConverterOptionSequenceString.write(value.features, into: &buf)
        FfiConverterOptionString.write(value.notes, into: &buf)
        FfiConverterOptionSequenceString.write(value.tags, into: &buf)
        FfiConverterOptionSequenceTypeUniAttachment.write(value.attachments, into: &buf)
        FfiConverterString.write(value.extraJson, into: &buf)
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public func FfiConverterTypeUniNewItem_lift(_ buf: RustBuffer) throws -> UniNewItem {
    return try FfiConverterTypeUniNewItem.lift(buf)
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public func FfiConverterTypeUniNewItem_lower(_ value: UniNewItem) -> RustBuffer {
    return FfiConverterTypeUniNewItem.lower(value)
}

// Note that we don't yet support `indirect` for enums.
// See https://github.com/mozilla/uniffi-rs/issues/396 for further discussion.

public enum AttachmentKind {
    case manual
    case receipt
    case photo
    case warranty
    case other
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public struct FfiConverterTypeAttachmentKind: FfiConverterRustBuffer {
    typealias SwiftType = AttachmentKind

    public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> AttachmentKind {
        let variant: Int32 = try readInt(&buf)
        switch variant {
        case 1: return .manual

        case 2: return .receipt

        case 3: return .photo

        case 4: return .warranty

        case 5: return .other

        default: throw UniffiInternalError.unexpectedEnumCase
        }
    }

    public static func write(_ value: AttachmentKind, into buf: inout [UInt8]) {
        switch value {
        case .manual:
            writeInt(&buf, Int32(1))

        case .receipt:
            writeInt(&buf, Int32(2))

        case .photo:
            writeInt(&buf, Int32(3))

        case .warranty:
            writeInt(&buf, Int32(4))

        case .other:
            writeInt(&buf, Int32(5))
        }
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public func FfiConverterTypeAttachmentKind_lift(_ buf: RustBuffer) throws -> AttachmentKind {
    return try FfiConverterTypeAttachmentKind.lift(buf)
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public func FfiConverterTypeAttachmentKind_lower(_ value: AttachmentKind) -> RustBuffer {
    return FfiConverterTypeAttachmentKind.lower(value)
}

extension AttachmentKind: Equatable, Hashable {}

public enum CatalogError {
    case NotFound(id: String)
    case Io(message: String)
    case Json(message: String)
    case InvalidFilename(filename: String)
    case Cache(message: String)
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
public struct FfiConverterTypeCatalogError: FfiConverterRustBuffer {
    typealias SwiftType = CatalogError

    public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> CatalogError {
        let variant: Int32 = try readInt(&buf)
        switch variant {
        case 1: return try .NotFound(
                id: FfiConverterString.read(from: &buf)
            )
        case 2: return try .Io(
                message: FfiConverterString.read(from: &buf)
            )
        case 3: return try .Json(
                message: FfiConverterString.read(from: &buf)
            )
        case 4: return try .InvalidFilename(
                filename: FfiConverterString.read(from: &buf)
            )
        case 5: return try .Cache(
                message: FfiConverterString.read(from: &buf)
            )
        default: throw UniffiInternalError.unexpectedEnumCase
        }
    }

    public static func write(_ value: CatalogError, into buf: inout [UInt8]) {
        switch value {
        case let .NotFound(id):
            writeInt(&buf, Int32(1))
            FfiConverterString.write(id, into: &buf)

        case let .Io(message):
            writeInt(&buf, Int32(2))
            FfiConverterString.write(message, into: &buf)

        case let .Json(message):
            writeInt(&buf, Int32(3))
            FfiConverterString.write(message, into: &buf)

        case let .InvalidFilename(filename):
            writeInt(&buf, Int32(4))
            FfiConverterString.write(filename, into: &buf)

        case let .Cache(message):
            writeInt(&buf, Int32(5))
            FfiConverterString.write(message, into: &buf)
        }
    }
}

extension CatalogError: Equatable, Hashable {}

extension CatalogError: Foundation.LocalizedError {
    public var errorDescription: String? {
        String(reflecting: self)
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
private struct FfiConverterOptionDouble: FfiConverterRustBuffer {
    typealias SwiftType = Double?

    static func write(_ value: SwiftType, into buf: inout [UInt8]) {
        guard let value = value else {
            writeInt(&buf, Int8(0))
            return
        }
        writeInt(&buf, Int8(1))
        FfiConverterDouble.write(value, into: &buf)
    }

    static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType {
        switch try readInt(&buf) as Int8 {
        case 0: return nil
        case 1: return try FfiConverterDouble.read(from: &buf)
        default: throw UniffiInternalError.unexpectedOptionalTag
        }
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
private struct FfiConverterOptionString: FfiConverterRustBuffer {
    typealias SwiftType = String?

    static func write(_ value: SwiftType, into buf: inout [UInt8]) {
        guard let value = value else {
            writeInt(&buf, Int8(0))
            return
        }
        writeInt(&buf, Int8(1))
        FfiConverterString.write(value, into: &buf)
    }

    static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType {
        switch try readInt(&buf) as Int8 {
        case 0: return nil
        case 1: return try FfiConverterString.read(from: &buf)
        default: throw UniffiInternalError.unexpectedOptionalTag
        }
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
private struct FfiConverterOptionTypeUniItem: FfiConverterRustBuffer {
    typealias SwiftType = UniItem?

    static func write(_ value: SwiftType, into buf: inout [UInt8]) {
        guard let value = value else {
            writeInt(&buf, Int8(0))
            return
        }
        writeInt(&buf, Int8(1))
        FfiConverterTypeUniItem.write(value, into: &buf)
    }

    static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType {
        switch try readInt(&buf) as Int8 {
        case 0: return nil
        case 1: return try FfiConverterTypeUniItem.read(from: &buf)
        default: throw UniffiInternalError.unexpectedOptionalTag
        }
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
private struct FfiConverterOptionTypeUniListFilter: FfiConverterRustBuffer {
    typealias SwiftType = UniListFilter?

    static func write(_ value: SwiftType, into buf: inout [UInt8]) {
        guard let value = value else {
            writeInt(&buf, Int8(0))
            return
        }
        writeInt(&buf, Int8(1))
        FfiConverterTypeUniListFilter.write(value, into: &buf)
    }

    static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType {
        switch try readInt(&buf) as Int8 {
        case 0: return nil
        case 1: return try FfiConverterTypeUniListFilter.read(from: &buf)
        default: throw UniffiInternalError.unexpectedOptionalTag
        }
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
private struct FfiConverterOptionSequenceString: FfiConverterRustBuffer {
    typealias SwiftType = [String]?

    static func write(_ value: SwiftType, into buf: inout [UInt8]) {
        guard let value = value else {
            writeInt(&buf, Int8(0))
            return
        }
        writeInt(&buf, Int8(1))
        FfiConverterSequenceString.write(value, into: &buf)
    }

    static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType {
        switch try readInt(&buf) as Int8 {
        case 0: return nil
        case 1: return try FfiConverterSequenceString.read(from: &buf)
        default: throw UniffiInternalError.unexpectedOptionalTag
        }
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
private struct FfiConverterOptionSequenceTypeUniAttachment: FfiConverterRustBuffer {
    typealias SwiftType = [UniAttachment]?

    static func write(_ value: SwiftType, into buf: inout [UInt8]) {
        guard let value = value else {
            writeInt(&buf, Int8(0))
            return
        }
        writeInt(&buf, Int8(1))
        FfiConverterSequenceTypeUniAttachment.write(value, into: &buf)
    }

    static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType {
        switch try readInt(&buf) as Int8 {
        case 0: return nil
        case 1: return try FfiConverterSequenceTypeUniAttachment.read(from: &buf)
        default: throw UniffiInternalError.unexpectedOptionalTag
        }
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
private struct FfiConverterSequenceString: FfiConverterRustBuffer {
    typealias SwiftType = [String]

    static func write(_ value: [String], into buf: inout [UInt8]) {
        let len = Int32(value.count)
        writeInt(&buf, len)
        for item in value {
            FfiConverterString.write(item, into: &buf)
        }
    }

    static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> [String] {
        let len: Int32 = try readInt(&buf)
        var seq = [String]()
        seq.reserveCapacity(Int(len))
        for _ in 0 ..< len {
            try seq.append(FfiConverterString.read(from: &buf))
        }
        return seq
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
private struct FfiConverterSequenceTypeUniAttachment: FfiConverterRustBuffer {
    typealias SwiftType = [UniAttachment]

    static func write(_ value: [UniAttachment], into buf: inout [UInt8]) {
        let len = Int32(value.count)
        writeInt(&buf, len)
        for item in value {
            FfiConverterTypeUniAttachment.write(item, into: &buf)
        }
    }

    static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> [UniAttachment] {
        let len: Int32 = try readInt(&buf)
        var seq = [UniAttachment]()
        seq.reserveCapacity(Int(len))
        for _ in 0 ..< len {
            try seq.append(FfiConverterTypeUniAttachment.read(from: &buf))
        }
        return seq
    }
}

#if swift(>=5.8)
    @_documentation(visibility: private)
#endif
private struct FfiConverterSequenceTypeUniItem: FfiConverterRustBuffer {
    typealias SwiftType = [UniItem]

    static func write(_ value: [UniItem], into buf: inout [UInt8]) {
        let len = Int32(value.count)
        writeInt(&buf, len)
        for item in value {
            FfiConverterTypeUniItem.write(item, into: &buf)
        }
    }

    static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> [UniItem] {
        let len: Int32 = try readInt(&buf)
        var seq = [UniItem]()
        seq.reserveCapacity(Int(len))
        for _ in 0 ..< len {
            try seq.append(FfiConverterTypeUniItem.read(from: &buf))
        }
        return seq
    }
}

private enum InitializationResult {
    case ok
    case contractVersionMismatch
    case apiChecksumMismatch
}

/// Use a global variable to perform the versioning checks. Swift ensures that
/// the code inside is only computed once.
private var initializationResult: InitializationResult = {
    // Get the bindings contract version from our ComponentInterface
    let bindings_contract_version = 26
    // Get the scaffolding contract version by calling the into the dylib
    let scaffolding_contract_version = ffi_allourthings_core_uniffi_contract_version()
    if bindings_contract_version != scaffolding_contract_version {
        return InitializationResult.contractVersionMismatch
    }
    if uniffi_allourthings_core_checksum_method_unicatalogstore_add_attachment() != 56390 {
        return InitializationResult.apiChecksumMismatch
    }
    if uniffi_allourthings_core_checksum_method_unicatalogstore_add_item() != 7227 {
        return InitializationResult.apiChecksumMismatch
    }
    if uniffi_allourthings_core_checksum_method_unicatalogstore_delete_attachment() != 5953 {
        return InitializationResult.apiChecksumMismatch
    }
    if uniffi_allourthings_core_checksum_method_unicatalogstore_delete_item() != 50535 {
        return InitializationResult.apiChecksumMismatch
    }
    if uniffi_allourthings_core_checksum_method_unicatalogstore_get_attachment() != 51350 {
        return InitializationResult.apiChecksumMismatch
    }
    if uniffi_allourthings_core_checksum_method_unicatalogstore_get_item() != 33540 {
        return InitializationResult.apiChecksumMismatch
    }
    if uniffi_allourthings_core_checksum_method_unicatalogstore_get_item_fields() != 29644 {
        return InitializationResult.apiChecksumMismatch
    }
    if uniffi_allourthings_core_checksum_method_unicatalogstore_list_items() != 56893 {
        return InitializationResult.apiChecksumMismatch
    }
    if uniffi_allourthings_core_checksum_method_unicatalogstore_rebuild_cache() != 55026 {
        return InitializationResult.apiChecksumMismatch
    }
    if uniffi_allourthings_core_checksum_method_unicatalogstore_refresh() != 459 {
        return InitializationResult.apiChecksumMismatch
    }
    if uniffi_allourthings_core_checksum_method_unicatalogstore_search_items() != 64112 {
        return InitializationResult.apiChecksumMismatch
    }
    if uniffi_allourthings_core_checksum_method_unicatalogstore_update_item() != 12782 {
        return InitializationResult.apiChecksumMismatch
    }
    if uniffi_allourthings_core_checksum_constructor_unicatalogstore_new() != 1266 {
        return InitializationResult.apiChecksumMismatch
    }
    if uniffi_allourthings_core_checksum_constructor_unicatalogstore_new_with_cache() != 18926 {
        return InitializationResult.apiChecksumMismatch
    }

    return InitializationResult.ok
}()

private func uniffiEnsureInitialized() {
    switch initializationResult {
    case .ok:
        break
    case .contractVersionMismatch:
        fatalError("UniFFI contract version mismatch: try cleaning and rebuilding your project")
    case .apiChecksumMismatch:
        fatalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
    }
}

// swiftlint:enable all
