feat: Working on node builder

This commit is contained in:
2024-12-01 15:25:42 -05:00
parent ff49b12198
commit 96a1fac07b
11 changed files with 357 additions and 21 deletions

View File

@@ -16,16 +16,16 @@ public enum NodeBuilder {
.init(components, separator: .empty())
}
public static func buildEither<N0: NodeRepresentable, N1: NodeRepresentable>(
first component: N0
) -> _ConditionalNode<N0, N1> {
.first(component)
public static func buildEither<N: NodeRepresentable>(
first component: N
) -> N {
component
}
public static func buildEither<N0: NodeRepresentable, N1: NodeRepresentable>(
second component: N1
) -> _ConditionalNode<N0, N1> {
.second(component)
public static func buildEither<N: NodeRepresentable>(
second component: N
) -> N {
component
}
public static func buildBlock<N: NodeRepresentable>(_ components: N...) -> _ManyNode {
@@ -37,8 +37,13 @@ public enum NodeBuilder {
// expression.eraseToAnyNode()
// }
public static func buildOptional<N: NodeRepresentable>(_ component: N?) -> AnyNode {
component?.eraseToAnyNode() ?? AnyNode { Text.empty() }
public static func buildOptional<N: NodeRepresentable>(_ component: N?) -> _ConditionalNode<N, Text> {
switch component {
case let .some(node):
return .first(node)
case .none:
return .second(.empty())
}
}
public static func buildFinalResult<N: NodeRepresentable>(_ component: N) -> N {
@@ -79,15 +84,18 @@ public struct _ManyNode: NodeRepresentable {
@usableFromInline
init(
_ nodes: [any NodeRepresentable],
separator: AnyNode = Text.empty().eraseToAnyNode()
separator: any NodeRepresentable = "\n" as any NodeRepresentable
) {
self.nodes = nodes
self.separator = separator
}
@inlinable
public init(_ nodes: [any NodeRepresentable], separator: any NodeRepresentable) {
self.nodes = nodes
// Flatten the nodes.
var allNodes = [any NodeRepresentable]()
for node in nodes {
if let many = node as? Self {
allNodes += many.nodes
} else {
allNodes.append(node)
}
}
self.nodes = allNodes
self.separator = separator
}