Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// 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
You can’t perform that action at this time.