/*
 * Copyright 2019 Google
 *
 * 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
 *
 *      http://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

/// Represents a code coverage report generated by XCov (maps to the JSON report one to one).
public struct CoverageReport: Decodable {
  public var targets: [Target]
  public var coverage: Double

  public init(targets: [Target], coverage: Double) {
    self.targets = targets
    self.coverage = coverage
  }

  /// Creates a CoverageReport from a JSON file generated by XCov.
  public static func load(path: String) throws -> CoverageReport {
    let data = try Data(contentsOf: NSURL(fileURLWithPath: path) as URL)
    let decoder = JSONDecoder()
    return try decoder.decode(CoverageReport.self, from: data)
  }
}

/// An XCov target.
public struct Target: Decodable {
  public var name: String
  public var files: [File]
  public var coverage: Double

  public init(name: String, coverage: Double) {
    self.name = name
    self.coverage = coverage
    files = []
  }
}

/// An XCov file.
public struct File: Decodable {
  public var name: String
  public var coverage: Double
  public var type: String
  public var functions: [Function]
}

/// An XCov function.
public struct Function: Decodable {
  public var name: String
  public var coverage: Double
}
