Batman
This commit is contained in:
commit
be2f5d3d1d
5 changed files with 83 additions and 0 deletions
5
go.mod
Normal file
5
go.mod
Normal file
|
@ -0,0 +1,5 @@
|
|||
module git.mstar.dev/mstar/treeificator
|
||||
|
||||
go 1.24.2
|
||||
|
||||
require git.mstar.dev/mstar/goutils v1.12.3 // indirect
|
2
go.sum
Normal file
2
go.sum
Normal file
|
@ -0,0 +1,2 @@
|
|||
git.mstar.dev/mstar/goutils v1.12.3 h1:Wx7i8/a99Cp+Y/XcXgqQr0r9cSsJu7QkWBlKyprTH44=
|
||||
git.mstar.dev/mstar/goutils v1.12.3/go.mod h1:juxY0eZEMnA95fedRp2LVXvUBgEjz66nE8SEdGKcxMA=
|
25
informer.go
Normal file
25
informer.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
package treeificator
|
||||
|
||||
// Informers inform the parser about the prefix and suffix that make up a node
|
||||
type Informer interface {
|
||||
// Get the prefix starting the informer's node type
|
||||
GetPrefix() string
|
||||
// Get the suffix starting the informer's node type
|
||||
GetSuffix() string
|
||||
// Get the name of the node type.
|
||||
// Each name (in lowercase) may only exist once. The parser will enforce this.
|
||||
GetName() string
|
||||
}
|
||||
|
||||
// Defa
|
||||
type DefaultInformer struct{}
|
||||
|
||||
func (defaultinformer *DefaultInformer) GetPrefix() string {
|
||||
return ""
|
||||
}
|
||||
func (defaultinformer *DefaultInformer) GetSuffix() string {
|
||||
return ""
|
||||
}
|
||||
func (defaultinformer *DefaultInformer) GetName() string {
|
||||
return "default"
|
||||
}
|
38
node.go
Normal file
38
node.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
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()
|
||||
}
|
||||
}
|
13
parser.go
Normal file
13
parser.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package treeificator
|
||||
|
||||
func Marshal(raw string, informers ...Informer) Node {
|
||||
prefixToInformer := map[string]Informer{}
|
||||
return Node{
|
||||
NodeType: &DefaultInformer{},
|
||||
Elements: marshal(raw, prefixToInformer),
|
||||
}
|
||||
}
|
||||
|
||||
func marshal(raw string, informers map[string]Informer) []NodeElement {
|
||||
return []NodeElement{NodeElement{Text: &raw}}
|
||||
}
|
Loading…
Reference in a new issue