mirror of https://github.com/apple/pkl-swift
85 lines
2.5 KiB
Swift
85 lines
2.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
// Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Foundation
|
|
|
|
struct Box<Value> {
|
|
let value: Value
|
|
init(_ value: Value) {
|
|
self.value = value
|
|
}
|
|
}
|
|
|
|
extension Box: Encodable where Value: Encodable {
|
|
func encode(to encoder: Encoder) throws {
|
|
try self.value.encode(to: encoder)
|
|
}
|
|
}
|
|
|
|
extension Box: Decodable where Value: Decodable {
|
|
init(from decoder: Decoder) throws {
|
|
try self.init(Value(from: decoder))
|
|
}
|
|
}
|
|
|
|
extension Box where Value == Data {
|
|
init(from decoder: Decoder) throws {
|
|
let container = try decoder.singleValueContainer()
|
|
try self.init(container.decode(Value.self))
|
|
}
|
|
|
|
func encode(to encoder: Encoder) throws {
|
|
var container = encoder.singleValueContainer()
|
|
try container.encode(self.value)
|
|
}
|
|
}
|
|
|
|
extension Box where Value == Date {
|
|
init(from decoder: Decoder) throws {
|
|
let container = try decoder.singleValueContainer()
|
|
try self.init(container.decode(Value.self))
|
|
}
|
|
|
|
func encode(to encoder: Encoder) throws {
|
|
var container = encoder.singleValueContainer()
|
|
try container.encode(self.value)
|
|
}
|
|
}
|
|
|
|
extension Box where Value == [UInt8] {
|
|
init(from decoder: Decoder) throws {
|
|
let container = try decoder.singleValueContainer()
|
|
try self.init(container.decode(Value.self))
|
|
}
|
|
|
|
func encode(to encoder: Encoder) throws {
|
|
var container = encoder.singleValueContainer()
|
|
try container.encode(self.value)
|
|
}
|
|
}
|
|
|
|
extension Box where Value == URL {
|
|
init(from decoder: Decoder) throws {
|
|
let container = try decoder.singleValueContainer()
|
|
try self.init(container.decode(Value.self))
|
|
}
|
|
|
|
func encode(to encoder: Encoder) throws {
|
|
var container = encoder.singleValueContainer()
|
|
try container.encode(self.value)
|
|
}
|
|
}
|