// Automatically generated by xdrgen
// DO NOT EDIT or your changes may be overwritten

import Foundation

// === xdr source ============================================================

//  //: CreateAccountRuleData is used to pass necessary params to create a new account rule
//  struct CreateAccountRuleData
//  {
//      //: Resource is used to specify an entity (for some - with properties) that can be managed through operations
//      AccountRuleResource resource;
//      //: Value from enum that can be applied to `resource`
//      AccountRuleAction action;
//      //: True if such `action` on such `resource` is prohibited, otherwise allows
//      bool forbids;
//      //: Arbitrary stringified json object that will be attached to rule
//      longstring details;
//  
//      //: reserved for future use
//      union switch (LedgerVersion v)
//      {
//      case EMPTY_VERSION:
//          void;
//      } ext;
//  };

//  ===========================================================================
public struct CreateAccountRuleData: XDRCodable {
  public var resource: AccountRuleResource
  public var action: AccountRuleAction
  public var forbids: Bool
  public var details: Longstring
  public var ext: CreateAccountRuleDataExt

  public init(
      resource: AccountRuleResource,
      action: AccountRuleAction,
      forbids: Bool,
      details: Longstring,
      ext: CreateAccountRuleDataExt) {

    self.resource = resource
    self.action = action
    self.forbids = forbids
    self.details = details
    self.ext = ext
  }

  public func toXDR() -> Data {
    var xdr = Data()

    xdr.append(self.resource.toXDR())
    xdr.append(self.action.toXDR())
    xdr.append(self.forbids.toXDR())
    xdr.append(self.details.toXDR())
    xdr.append(self.ext.toXDR())

    return xdr
  }

  public init(xdrData: inout Data) throws {
    self.resource = try AccountRuleResource(xdrData: &xdrData)
    self.action = try AccountRuleAction(xdrData: &xdrData)
    self.forbids = try Bool(xdrData: &xdrData)
    self.details = try Longstring(xdrData: &xdrData)
    self.ext = try CreateAccountRuleDataExt(xdrData: &xdrData)
  }

  public enum CreateAccountRuleDataExt: XDRDiscriminatedUnion {
    case emptyVersion

    public var discriminant: Int32 {
      switch self {
      case .emptyVersion: return LedgerVersion.emptyVersion.rawValue
      }
    }

    public func toXDR() -> Data {
      var xdr = Data()

      xdr.append(self.discriminant.toXDR())

      switch self {
      case .emptyVersion: xdr.append(Data())
      }

      return xdr
    }

    public init(xdrData: inout Data) throws {
      let discriminant = try Int32(xdrData: &xdrData)

      switch discriminant {
      case LedgerVersion.emptyVersion.rawValue: self = .emptyVersion
      default:
        throw XDRErrors.unknownEnumCase
      }
    }

  }
}
