-
Notifications
You must be signed in to change notification settings - Fork 8
/
tree_handler_programmably.go
99 lines (79 loc) · 2.59 KB
/
tree_handler_programmably.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//go:build !tinywasm
package gtree
import (
"errors"
"io"
)
var (
idxCounter = newCounter()
)
// OutputProgrammably outputs tree to w.
// This function requires node generated by NewRoot function.
func OutputProgrammably(w io.Writer, root *Node, options ...Option) error {
if err := validateTreeRoot(root); err != nil {
return err
}
idxCounter.reset()
cfg := newConfig(options)
return initializeTree(cfg).outputProgrammably(w, root, cfg)
}
// MkdirProgrammably makes directories.
// This function requires node generated by NewRoot function.
func MkdirProgrammably(root *Node, options ...Option) error {
if err := validateTreeRoot(root); err != nil {
return err
}
idxCounter.reset()
cfg := newConfig(options)
return initializeTree(cfg).mkdirProgrammably(root, cfg)
}
// VerifyProgrammably verifies directory.
// This function requires node generated by NewRoot function.
func VerifyProgrammably(root *Node, options ...Option) error {
if err := validateTreeRoot(root); err != nil {
return err
}
idxCounter.reset()
cfg := newConfig(options)
return initializeTree(cfg).verifyProgrammably(root, cfg)
}
// WalkProgrammably executes user-defined function while traversing tree structure recursively.
// This function requires node generated by NewRoot function.
func WalkProgrammably(root *Node, callback func(*WalkerNode) error, options ...Option) error {
if err := validateTreeRoot(root); err != nil {
return err
}
idxCounter.reset()
cfg := newConfig(options)
return initializeTree(cfg).walkProgrammably(root, callback, cfg)
}
// NewRoot creates a starting node for building tree.
func NewRoot(text string) *Node {
return newNode(text, rootHierarchyNum, idxCounter.next())
}
// Add adds a node and returns an instance of it.
// If a node with the same text already exists in the same hierarchy of the tree, that node will be returned.
func (parent *Node) Add(text string) *Node {
if child := parent.findChildByText(text); child != nil {
return child
}
current := newNode(text, parent.hierarchy+1, idxCounter.next())
current.setParent(parent)
parent.addChild(current)
return current
}
var (
// ErrNilNode is returned if the argument *gtree.Node of OutputProgrammably / MkdirProgrammably / VerifyProgrammably function is nill.
ErrNilNode = errors.New("nil node")
// ErrNotRoot is returned if the argument *gtree.Node of OutputProgrammably / MkdirProgrammably / VerifyProgrammably function is not root of the tree.
ErrNotRoot = errors.New("not root node")
)
func validateTreeRoot(root *Node) error {
if root == nil {
return ErrNilNode
}
if !root.isRoot() {
return ErrNotRoot
}
return nil
}