Skip to content

Commit 8d29736

Browse files
make Filename decodable
1 parent c1e9c15 commit 8d29736

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

Sources/PathUtils/Filename.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,11 @@ public struct Filename: Equatable, Hashable, CustomStringConvertible {
3535
}
3636

3737
public func url(relativeTo url: URL, pathExtension: String) -> URL {
38-
3938
return url.appendingPathComponent(self.string)
4039
.appendingPathExtension(pathExtension)
4140
}
4241

4342
public func url(relativeTo folder: Folder, pathExtension: String) -> URL {
44-
4543
return url(relativeTo: folder.url,
4644
pathExtension: pathExtension)
4745
}
@@ -55,8 +53,21 @@ public func + (lhs: Filename, rhs: String) -> Filename {
5553
return Filename(lhs.value + rhs)
5654
}
5755

58-
extension Filename: Comparable { }
56+
extension Filename: Comparable {
57+
public static func < (lhs: Filename, rhs: Filename) -> Bool {
58+
return lhs.value < rhs.value
59+
}
60+
}
5961

60-
public func < (lhs: Filename, rhs: Filename) -> Bool {
61-
return lhs.value < rhs.value
62+
extension Filename: Decodable {
63+
public enum DecodingError: Error {
64+
case emptyFilename
65+
}
66+
67+
public init(from decoder: Decoder) throws {
68+
let container = try decoder.singleValueContainer()
69+
let string = try container.decode(String.self)
70+
guard let contentfulString = ContentfulString(string) else { throw DecodingError.emptyFilename }
71+
self.init(contentfulString)
72+
}
6273
}

0 commit comments

Comments
 (0)