Files
Lyrebird/Sources/LyrebirdGraphDSL/DSLBuiltinRegistry.swift
T

46 lines
1.8 KiB
Swift

// DSLBuiltinRegistry.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
// Lookup surface over the generated builtin table. The table itself lives in
// `Generated/GeneratedDSLRegistry.swift`, emitted by Tools/DSLRegistryGen from
// the generator sources — this file only provides name+rate lookup over it, so
// the graph builder never depends on how the table is populated.
/// The generators the DSL can construct, by name and rate.
public enum DSLBuiltinRegistry {
/// All builtins, in generated (alphabetical, then rate) order.
public static let all: [DSLBuiltin] = generated
/// Builtin lookup keyed by name then rate. Built once.
public static let byName: [String: [DSLRate: DSLBuiltin]] = {
var table: [String: [DSLRate: DSLBuiltin]] = [:]
for builtin: DSLBuiltin in generated {
table[builtin.name, default: [:]][builtin.rate] = builtin
}
return table
}()
/// The builtin registered under `name` at `rate` (default audio).
/// - Throws: ``DSLBuildError/unknownBuiltin(_:)`` when no generator exists at
/// that name, or ``DSLBuildError/rateUnavailable(builtin:rate:)`` when the
/// name exists but not at the requested rate.
public static func lookup(_ name: String, rate: DSLRate = .audio) throws -> DSLBuiltin {
guard let rates: [DSLRate: DSLBuiltin] = byName[name] else {
throw DSLBuildError.unknownBuiltin(name)
}
guard let builtin: DSLBuiltin = rates[rate] else {
throw DSLBuildError.rateUnavailable(builtin: name, rate: rate.rawValue)
}
return builtin
}
/// The rates a generator name is available at.
public static func rates(of name: String) -> Set<DSLRate> {
Set((byName[name] ?? [:]).keys)
}
}