LeadKit/TISwiftUtils/Sources/Helpers/DSL.swift

61 lines
1.9 KiB
Swift

//
// Copyright (c) 2023 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import ObjectiveC
// https://habr.com/ru/companies/broadcast/articles/752614/
@dynamicMemberLookup
public struct DSL<T> {
let obj: T
public subscript<Value>(dynamicMember keyPath: WritableKeyPath<T, Value>) -> (Value) -> DSL<T> {
{ [obj] value in
var object = obj
object[keyPath: keyPath] = value
return DSL(obj: object)
}
}
public subscript<Value>(dynamicMember keyPath: WritableKeyPath<T, Value>) -> (Value) -> T {
{ [obj] value in
var object = obj
object[keyPath: keyPath] = value
return object
}
}
}
public protocol DSLCompatible {
associatedtype DSLType
var dsl: DSL<DSLType> { get }
}
public extension DSLCompatible {
var dsl: DSL<Self> {
DSL(obj: self)
}
}
extension NSObject: DSLCompatible {}