field_ios

In-the-field app for Radroots on iOS
git clone https://radroots.dev/git/field_ios.git
Log | Files | Refs | README | LICENSE

TeraGeneratedSubmission.swift (2314B)


      1 import Foundation
      2 import TeraKitBindings
      3 
      4 enum TeraGeneratedSubmission {
      5   static func reserveID() throws -> String {
      6     let value = try submissionReserveId()
      7     try version(value.schemaVersion)
      8     guard validID(value.id) else { throw mismatch() }
      9     return value.id
     10   }
     11 
     12   static func reserve(runtime: TeraRuntime, request: TeraSubmissionRequest) async throws -> TeraSubmissionReservation {
     13     let value = try await runtime.submissionReserve(request: FfiSubmissionReservationRequest(
     14       schemaVersion: 1, commandId: request.commandID, scope: request.scope.generatedValue,
     15       composerId: request.composerID, expectedRevision: request.expectedRevision
     16     ))
     17     return try translate(value, request: request)
     18   }
     19 
     20   static func translate(_ value: FfiSubmissionReservationReceipt, request: TeraSubmissionRequest) throws -> TeraSubmissionReservation {
     21     try version(value.schemaVersion)
     22     let captured = try value.captured.composerAppValue
     23     guard value.commandId == request.commandID, validID(value.commandId), validID(value.reservationId),
     24           captured.scope == request.scope, captured.id == request.composerID,
     25           captured.revision == request.expectedRevision,
     26           value.reservedAtUnixMs > 0, value.reservedAtUnixMs <= UInt64(Int64.max)
     27     else { throw mismatch() }
     28     return TeraSubmissionReservation(commandID: value.commandId, reservationID: value.reservationId,
     29                                      captured: captured, reservedAtUnixMilliseconds: value.reservedAtUnixMs,
     30                                      replayed: value.replayed)
     31   }
     32 
     33   static func validID(_ value: String) -> Bool {
     34     value.utf8.count == 32 && value != String(repeating: "0", count: 32)
     35       && value.utf8.allSatisfy { (48 ... 57).contains($0) || (97 ... 102).contains($0) }
     36   }
     37 
     38   static func version(_ version: UInt16) throws {
     39     guard version == 1 else {
     40       throw TeraRuntimeFailure.local(operation: "runtime.submission", code: "submission_schema_unsupported",
     41                                      safeMessage: "This reservation format requires a compatible app.")
     42     }
     43   }
     44 
     45   static func mismatch() -> TeraRuntimeFailure {
     46     .local(operation: "runtime.submission", code: "submission_receipt_mismatch",
     47            safeMessage: "The reservation response could not be reconciled.")
     48   }
     49 }