Path(_ url:) -> Path(url:)

This commit is contained in:
Max Howell
2019-02-11 20:40:27 -05:00
parent ed4b773870
commit c9d300a7b6
3 changed files with 16 additions and 7 deletions

View File

@@ -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) {
/**/
}
```

View File

@@ -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

View File

@@ -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 {