Created
January 8, 2017 02:37
-
-
Save cdzombak/46300be5521a74ab54969fb7a7744bc8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FileURL.swift | |
// Created by Chris Dzombak on 11/16/16. | |
// | |
import Validated | |
public typealias FileURL = Validated<URL, FileURLValidator> | |
public struct FileURLValidator: Validator, _FileURL_Validating { | |
public static func validate(_ value: URL) -> Bool { | |
return value.isFileURL | |
} | |
} | |
public protocol _FileURL_Validating {} | |
public protocol _FileURL_URLConformer { | |
var path: String { get } | |
var pathComponents: [String] { get } | |
func appendingPathComponent(_ pathComponent: String) -> URL | |
func appendingPathComponent(_ pathComponent: String, isDirectory: Bool) -> URL | |
} | |
extension URL: _FileURL_URLConformer {} | |
// Wrap NSURL API that will never return `nil` for `file://` URLs: | |
public extension Validated where WrapperType: _FileURL_URLConformer, V: _FileURL_Validating { | |
var path: String { | |
return value.path | |
} | |
var pathComponents: [String] { | |
return value.pathComponents | |
} | |
func fileURLByAppendingPathComponent(_ pathComponent: String) -> FileURL { | |
let result = value.appendingPathComponent(pathComponent) | |
guard let validResult = try? FileURL(result) else { | |
fatalError("Appending to a file URL should always result in another file URL") | |
} | |
return validResult | |
} | |
func fileURLByAppendingPathComponent(_ pathComponent: String, isDirectory: Bool) -> FileURL { | |
let result = value.appendingPathComponent(pathComponent, isDirectory: isDirectory) | |
guard let validResult = try? FileURL(result) else { | |
fatalError("Appending to a file URL should always result in another file URL") | |
} | |
return validResult | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment