// Code generated by sidekick. DO NOT EDIT.
//
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import GoogleCloudWkt
import GoogleRpc
import GoogleType

/// The RuleExecutionError resource represents an error generated from
/// running/deploying a rule.
public struct RuleExecutionError: Codable, Equatable, GoogleCloudWkt._AnyPackable,
  Sendable
{
  /// Output only. The resource name of the rule execution error.
  /// Format:
  /// projects/{project}/locations/{location}/instances/{instance}/ruleExecutionErrors/{rule_execution_error}
  public var name: Swift.String = Swift.String()

  /// Output only. The error status corresponding with the rule execution error.
  public var error: GoogleRpc.Status? = nil

  /// Output only. The event time range that the rule execution error corresponds
  /// with.
  public var timeRange: GoogleType.Interval? = nil

  /// The resource name of the source that generated the rule execution error.
  public var source: OneOf_Source? = nil

  /// Initialize a new instance of `RuleExecutionError`.
  public init() {}

  /// Use `config` to return a new instance of this object, with some fields updated.
  ///
  /// Commonly used to initialize the value, for example:
  ///
  /// ```
  /// let value = RuleExecutionError().with { $0.rule = ... }
  /// ```
  public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
    var copy = self
    try config(&copy)
    return copy
  }

  private enum CodingKeys: Swift.String, CodingKey {
    case rule = "rule"
    case curatedRule = "curatedRule"
    case name = "name"
    case error = "error"
    case timeRange = "timeRange"
  }

  public init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    self.name = try container.decode(Swift.String.self, forKey: .name)
    self.error = try container.decodeIfPresent(GoogleRpc.Status.self, forKey: .error)
    self.timeRange = try container.decodeIfPresent(GoogleType.Interval.self, forKey: .timeRange)

    var source: OneOf_Source? = nil
    let sourceCheckAndSet = {
      if source != nil {
        throw DecodingError.dataCorrupted(
          DecodingError.Context(
            codingPath: decoder.codingPath,
            debugDescription: "Multiple values set for oneof 'source'"))
      }
      source = $0
    }
    if let rule = try container.decodeIfPresent(Swift.String.self, forKey: .rule) {
      try sourceCheckAndSet(.rule(rule))
    }
    if let curatedRule = try container.decodeIfPresent(Swift.String.self, forKey: .curatedRule) {
      try sourceCheckAndSet(.curatedRule(curatedRule))
    }
    self.source = source
  }

  public func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(self.name, forKey: .name)
    try container.encode(self.error, forKey: .error)
    try container.encode(self.timeRange, forKey: .timeRange)

    if let choice = self.source {
      switch choice {
      case .rule(let value):
        try container.encode(value, forKey: .rule)
      case .curatedRule(let value):
        try container.encode(value, forKey: .curatedRule)
      }
    }
  }

  /// The resource name of the source that generated the rule execution error.
  public enum OneOf_Source: Codable, Equatable, Sendable {
    /// Output only. The resource name of the rule that generated the rule
    /// execution error.
    case rule(Swift.String)
    /// Output only. The resource name of the curated rule that generated the
    /// rule execution error.
    case curatedRule(Swift.String)
  }

  public static var _anyTypeUrl: Swift.String {
    return "type.googleapis.com/google.cloud.chronicle.v1.RuleExecutionError"
  }
  public init(fromAny any: GoogleCloudWkt.`Any`) throws {
    self = try GoogleCloudWkt._slowAnyDeserialize(Self.self, from: any)
  }
  public func _pack() throws -> GoogleCloudWkt.Struct {
    return try GoogleCloudWkt._slowAnySerialize(message: self)
  }
}
