Protocol
MTLGenerationStrategy
Protocol for MTL text generation output strategies.
protocol MTLGenerationStrategy : Sendable
Overview
Generation strategies abstract the destination and lifecycle of generated text, enabling MTL templates to generate output to files, memory, or other targets without changing the template execution logic.
Overview
MTL supports different generation strategies:
-
File system: Write directly to files on disk
-
In-memory: Accumulate text in memory for testing or programmatic access
-
Custom: Implement custom strategies for databases, network, etc.
Strategy Lifecycle
Each file block in an MTL template follows this lifecycle:
-
Create writer:
createWriter(url:mode:charset:indentation:)is called -
Template writes: Template statements write to the returned writer
-
Finalize:
finalizeWriter(_:)is called to commit/save the output
Example Usage
// File system strategy
let fileStrategy = MTLFileSystemStrategy(basePath: "/output")
let writer1 = try await fileStrategy.createWriter(
url: "models/Person.swift",
mode: .overwrite,
charset: "UTF-8",
indentation: MTLIndentation()
)
await writer1.writeLine("class Person {}")
try await fileStrategy.finalizeWriter(writer1)
// File written to /output/models/Person.swift
// In-memory strategy (for testing)
let memoryStrategy = MTLInMemoryStrategy()
let writer2 = try await memoryStrategy.createWriter(
url: "test.txt",
mode: .create,
charset: "UTF-8",
indentation: MTLIndentation()
)
await writer2.writeLine("Test output")
try await memoryStrategy.finalizeWriter(writer2)
let files = await memoryStrategy.getGeneratedFiles()
// files["test.txt"] == "Test output\n"
Topics
Instance Methods
Creates a new writer for the specified target.
Finalizes and commits the writer’s content to its target.
Relationships
Inherits From
Conforming Types
See Also
Execution
Manages the execution state for MTL template generation.