Codable protocol for Common Lisp structures
import CLCodable
struct Foo: CLDecodable {
let bar: Int
let baz: String
// MARK: - CLDecodable
init(clToken token: CLToken) throws {
guard case .structure(let structureName, let slots) = token else {
let message = "Expected structure root, got: '\(token)'"
throw CLReadError.wrongRoot(.init(message))
}
guard "\(type(of: self))" == structureName else {
let message = "Expected structure of type '\(type(of: self))', got: '\(token)'"
throw CLReadError.wrongRoot(.init(message))
}
guard let bar = try slots["bar"]?.int() else {
throw CLReadError.missingValue(.init("bar"))
}
self.bar = bar
guard let baz = try slots["baz"]?.string() else {
throw CLReadError.missingValue(.init("baz"))
}
self.baz = baz
}
}
func parseResponse() -> Foo? {
let data = "#S(FOO :BAR 24 :BAZ \"Hello, World!\")"
return try? clRead(data)
}
- Read Lisp structures and lists into Swift structures and arrays
- Print Swift structures and arrays back into Lisp structures and lists
- Transform kebab-case into camelCase and back (for straightforward cases)
- Handle package names
- Autogenerate decodable/encodable methods
Using Swift Package Manager
package.dependencies.append(
.package(url: "https://github.com/zapko/CLCodable", from: "0.8.1")
)
Can be found in tests:
Can be found in Samples. The plan is to autogenerate them the same way Swift is doing it for Decodable/Encodable.