Files
phbar/Sources/phbar/Models/Event/PHEventManifest.swift
T

43 lines
1.5 KiB
Swift

import Foundation
import TOML
/// The manifest describing an external event recognizer, read from
/// `<config>/events/<name>/event.toml`.
///
/// ```toml
/// library = "event.dylib" # required: shared library file name
/// symbol = "phbar_event_create" # optional: factory symbol (default below)
/// apiVersion = 1 # optional: target PHEventAPIVersion
/// ```
///
/// Additional keys (e.g. an `arguments` table) are **ignored by the host**: the
/// `@objc` boundary carries no arguments. A recognizer that wants configuration
/// reads its own `event.toml` directly (it is native code with filesystem
/// access) see the official `mpd` trigger for an example.
struct PHEventManifest: Decodable {
/// File name of the shared library within the event folder.
let library: String
/// `dlsym` symbol of the factory. Defaults to `PHEventCreateSymbol` when nil.
let symbol: String?
/// API version the library was built against. Omitted = unversioned.
let apiVersion: Int?
/// Read and decode the manifest living in `folder/event.toml`.
static func load(at folder: URL) throws -> PHEventManifest {
let url = folder.appending(path: "event.toml")
let data = try Data(contentsOf: url)
guard let contents = String(data: data, encoding: .utf8), !contents.isEmpty else {
throw PHEventManifestError.unreadable(url)
}
do {
return try TOMLDecoder().decode(PHEventManifest.self, from: contents)
} catch {
throw PHEventManifestError.unreadable(url)
}
}
}
enum PHEventManifestError: Error {
case unreadable(URL)
}