import Foundation

struct Errors : Codable {
    
	let error_id : ErrorId?
	let error_text : String?

	enum CodingKeys: String, CodingKey {
		case error_id = "error_id"
		case error_text = "error_text"
	}

	init(from decoder: Decoder) throws {
		let values = try decoder.container(keyedBy: CodingKeys.self)
		error_id = try values.decodeIfPresent(ErrorId.self, forKey: .error_id)
		error_text = try values.decodeIfPresent(String.self, forKey: .error_text)
	}

}

enum ErrorId: Codable {
    
    case integer(Int)
    case string(String)
    
    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let x = try? container.decode(Int.self) {
            self = .integer(x)
            return
        }
        if let x = try? container.decode(String.self) {
            self = .string(x)
            return
        }
        throw DecodingError.typeMismatch(APIStatus.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for APIStatus"))
    }
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .integer(let x):
            try container.encode(x)
        case .string(let x):
            try container.encode(x)
        }
    }
    
}
