feat: Working on node builder
This commit is contained in:
@@ -1,21 +1,34 @@
|
||||
// TODO: This doesn't seem correct, maybe remove this type.
|
||||
public struct AnyNode: NodeRepresentable {
|
||||
|
||||
@usableFromInline
|
||||
let node: any NodeRepresentable
|
||||
let makeBody: @Sendable () -> String
|
||||
|
||||
// @usableFromInline
|
||||
// let node: Value
|
||||
|
||||
@inlinable
|
||||
public init<N: NodeRepresentable>(@NodeBuilder _ build: () -> N) {
|
||||
self.node = build()
|
||||
self.makeBody = build().render
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public init<N: NodeRepresentable>(_ node: N) {
|
||||
self.node = node
|
||||
self.makeBody = node.render
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func render() -> String {
|
||||
node.render()
|
||||
makeBody()
|
||||
}
|
||||
//
|
||||
// public var body: some NodeRepresentable {
|
||||
// node
|
||||
// }
|
||||
|
||||
// @inlinable
|
||||
// public func render() -> String {
|
||||
// node.render()
|
||||
// }
|
||||
}
|
||||
|
||||
public extension NodeRepresentable {
|
||||
|
||||
@@ -2,36 +2,62 @@ public struct Group: NodeRepresentable {
|
||||
@usableFromInline
|
||||
let node: any NodeRepresentable
|
||||
|
||||
@usableFromInline
|
||||
init(
|
||||
separator: AnyNode,
|
||||
node: any NodeRepresentable
|
||||
@inlinable
|
||||
public init(
|
||||
separator: any NodeRepresentable = " ",
|
||||
@NodeBuilder node: () -> any NodeRepresentable
|
||||
) {
|
||||
if let many = node as? _ManyNode {
|
||||
self.node = _ManyNode(many.nodes, separator: separator)
|
||||
} else {
|
||||
self.node = node
|
||||
self.node = node()
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public init(
|
||||
separator: any NodeRepresentable,
|
||||
@NodeBuilder _ build: () -> any NodeRepresentable
|
||||
) {
|
||||
self.init(separator: separator.eraseToAnyNode(), node: build())
|
||||
}
|
||||
// @inlinable
|
||||
// public init(
|
||||
// separator: any NodeRepresentable,
|
||||
// @NodeBuilder _ build: () -> any NodeRepresentable
|
||||
// ) {
|
||||
// self.init(separator: separator.eraseToAnyNode(), node: build())
|
||||
// }
|
||||
|
||||
@inlinable
|
||||
public init(
|
||||
separator: String = " ",
|
||||
@NodeBuilder _ build: () -> any NodeRepresentable
|
||||
) {
|
||||
self.init(separator: separator.eraseToAnyNode(), node: build())
|
||||
}
|
||||
// @inlinable
|
||||
// public init(
|
||||
// separator: String = " ",
|
||||
// @NodeBuilder _ build: () -> any NodeRepresentable
|
||||
// ) {
|
||||
// self.init(separator: separator.eraseToAnyNode(), node: build())
|
||||
// }
|
||||
|
||||
@inlinable
|
||||
public func render() -> String {
|
||||
node.render()
|
||||
}
|
||||
}
|
||||
|
||||
public struct Group2<Content: NodeRepresentable>: Node {
|
||||
|
||||
@usableFromInline
|
||||
let content: Content
|
||||
|
||||
@usableFromInline
|
||||
let separator: any NodeRepresentable
|
||||
|
||||
@inlinable
|
||||
public init(
|
||||
separator: any NodeRepresentable = " ",
|
||||
@NodeBuilder _ content: () -> Content
|
||||
) {
|
||||
self.content = content()
|
||||
self.separator = separator
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public var body: some NodeRepresentable {
|
||||
guard let content = content as? _ManyNode else {
|
||||
return content.eraseToAnyNode()
|
||||
}
|
||||
return _ManyNode(content.nodes, separator: separator).eraseToAnyNode()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,3 +44,32 @@ public extension LabeledContent {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct LabeledContent2<Label: Node, Content: Node>: Node {
|
||||
|
||||
@usableFromInline
|
||||
let separator: any NodeRepresentable
|
||||
|
||||
@usableFromInline
|
||||
let label: Label
|
||||
|
||||
@usableFromInline
|
||||
let content: Content
|
||||
|
||||
public init(
|
||||
separator: any NodeRepresentable = " ",
|
||||
@NodeBuilder label: () -> Label,
|
||||
@NodeBuilder content: () -> Content
|
||||
) {
|
||||
self.separator = separator
|
||||
self.label = label()
|
||||
self.content = content()
|
||||
}
|
||||
|
||||
public var body: some NodeRepresentable {
|
||||
Group2(separator: separator) {
|
||||
label
|
||||
content
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,3 +4,10 @@ extension String: NodeRepresentable {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
extension String: Node {
|
||||
|
||||
public var body: some NodeRepresentable {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
@preconcurrency import Rainbow
|
||||
|
||||
public struct Text: NodeRepresentable {
|
||||
public struct Text: Node {
|
||||
@usableFromInline
|
||||
let text: String
|
||||
|
||||
@@ -10,7 +10,7 @@ public struct Text: NodeRepresentable {
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func render() -> String {
|
||||
public var body: some NodeRepresentable {
|
||||
text
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user