//
//  Application.swift
//  Pachyderm
//
//  Created by Shadowfacts on 9/9/18.
//  Copyright © 2018 Shadowfacts. All rights reserved.
//

import Foundation

public struct Application: Decodable, Sendable {
    public let name: String
    public let website: URL?
    
    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        
        self.name = try container.decode(String.self, forKey: .name)
        
        if let websiteStr = try container.decodeIfPresent(String.self, forKey: .website),
            let url = URL(string: websiteStr) {
            self.website = url
        } else {
            self.website = nil
        }
    }
    
    private enum CodingKeys: String, CodingKey {
        case name
        case website
    }
}
