feat: Moves core functionality into it's own library.

This commit is contained in:
2024-12-05 09:59:30 -05:00
parent 33356d8648
commit e7bbbec7c2
24 changed files with 464 additions and 387 deletions

View File

@@ -0,0 +1,67 @@
public protocol NodeRepresentable {
func render() -> String
}
public protocol TextNode: NodeRepresentable {
// swiftlint:disable type_name
associatedtype _Body: TextNode
typealias Body = _Body
// swiftlint:enable type_name
var body: Body { get }
}
public extension TextNode {
func render() -> String {
body.render()
}
}
// MARK: - String
extension String: NodeRepresentable {
public func render() -> String {
self
}
}
extension String: TextNode {
public var body: some TextNode {
self
}
}
// MARK: - Optional
extension Optional: TextNode where Wrapped: TextNode {
@TextBuilder
public var body: some TextNode {
switch self {
case let .some(node):
node
case .none:
Empty()
}
}
}
extension Optional: NodeRepresentable where Wrapped: NodeRepresentable, Wrapped: TextNode {
public func render() -> String {
body.render()
}
}
// MARK: - Array
extension Array: TextNode where Element: TextNode {
public var body: some TextNode {
NodeContainer(nodes: self)
}
}
extension Array: NodeRepresentable where Element: NodeRepresentable, Element: TextNode {
public func render() -> String {
body.render()
}
}