Emcee
36 строк · 1.1 Кб
1import ResourceLocation
2
3public protocol ResourceLocationType {
4static var name: String { get }
5}
6
7/// This is just a wrapper around ResourceLocation, but it provides a type safety for it
8public struct TypedResourceLocation<T: ResourceLocationType>: Codable, Hashable, CustomStringConvertible, RepresentableByResourceLocation {
9public let resourceLocation: ResourceLocation
10
11public init(_ resourceLocation: ResourceLocation) {
12self.resourceLocation = resourceLocation
13}
14
15public init?(_ resourceLocation: ResourceLocation?) {
16guard let resourceLocation = resourceLocation else {
17return nil
18}
19
20self.resourceLocation = resourceLocation
21}
22
23public init(from decoder: Decoder) throws {
24let container = try decoder.singleValueContainer()
25resourceLocation = try container.decode(ResourceLocation.self)
26}
27
28public func encode(to encoder: Encoder) throws {
29var container = encoder.singleValueContainer()
30try container.encode(resourceLocation)
31}
32
33public var description: String {
34return "<\(T.name): \(resourceLocation)>"
35}
36}
37