diff --git a/README.md b/README.md index 367dd6c..dfbc519 100644 --- a/README.md +++ b/README.md @@ -281,7 +281,7 @@ Therefore, if you are not using this feature you are fine. If you have URLs the way to get a `Path` is: ```swift -if let path = Path(url) { +if let path = Path(url: url) { /*…*/ } ``` diff --git a/Sources/Path.swift b/Sources/Path.swift index 0bf00e3..f60c9f8 100644 --- a/Sources/Path.swift +++ b/Sources/Path.swift @@ -104,13 +104,22 @@ public struct Path: Equatable, Hashable, Comparable { self.string = join_(prefix: "/", pathComponents: pathComponents) } - public init?(_ url: URL) { + /** + Creates a new absolute, standardized path from the provided file-scheme URL. + - Note: If the URL is not a file URL, returns `nil`. + */ + public init?(url: URL) { guard url.scheme == "file" else { return nil } - self.init(string: url.path) + self.init(url.path) //NOTE: URL cannot be a file-reference url, unlike NSURL, so this always works } - public init?(_ url: NSURL) { + /** + Creates a new absolute, standardized path from the provided file-scheme URL. + - Note: If the URL is not a file URL, returns `nil`. + - Note: If the URL is a file reference URL, converts it to a POSIX path first. + */ + public init?(url: NSURL) { guard url.scheme == "file", let path = url.path else { return nil } self.init(string: path) // ^^ works even if the url is a file-reference url diff --git a/Tests/PathTests/PathTests.swift b/Tests/PathTests/PathTests.swift index 4f7933b..9bafc54 100644 --- a/Tests/PathTests/PathTests.swift +++ b/Tests/PathTests/PathTests.swift @@ -577,10 +577,10 @@ class PathTests: XCTestCase { } func testURLInitializer() throws { - XCTAssertEqual(Path(Path.home.url), Path.home) + XCTAssertEqual(Path(url: Path.home.url), Path.home) XCTAssertEqual(Path.home.fileReferenceURL.flatMap(Path.init), Path.home) - XCTAssertNil(Path(URL(string: "https://foo.com")!)) - XCTAssertNil(Path(NSURL(string: "https://foo.com")!)) + XCTAssertNil(Path(url: URL(string: "https://foo.com")!)) + XCTAssertNil(Path(url: NSURL(string: "https://foo.com")!)) } func testInitializerForRelativePath() throws {