treeificator/node.go
2025-04-23 16:18:24 +02:00

38 lines
905 B
Go

package treeificator
import "strings"
type Node struct {
// What kind of node this is
NodeType Informer
// The elements in this node, ordered by appearance
Elements []NodeElement
}
// An element in a node. Either a string or another node.
// Leaf elements will always be a string.
// Root element will always be a [DefaultInformaer].
type NodeElement struct {
Text *string
Node *Node
}
// Unmarshal a node into the string it got constructed from
func (n *Node) Unmarshal() string {
builder := strings.Builder{}
builder.WriteString(n.NodeType.GetPrefix())
for _, elem := range n.Elements {
builder.WriteString(elem.Unmarshal())
}
builder.WriteString(n.NodeType.GetSuffix())
return builder.String()
}
// Unmarshal an element into the string it got constructed from
func (e *NodeElement) Unmarshal() string {
if e.Text != nil {
return *e.Text
} else {
return e.Node.Unmarshal()
}
}