| 1 |
// Author: Jim Idle - [email protected] / [email protected] |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package jwz |
| 5 |
|
| 6 |
// NoThreadableError indicates that the container was not in a valid state when a utility function was called |
| 7 |
// against it |
| 8 |
type NoThreadableError struct{} |
| 9 |
|
| 10 |
func (e NoThreadableError) Error() string { |
| 11 |
return "Container is not a root (has parent), but contains no Threadable element" |
| 12 |
} |
| 13 |
|
| 14 |
// threadContainer is used to encapsulate a Threadable implementation. It holds some intermediate state used while |
| 15 |
// threading. This is private to the module and is used while performing the threading operation, then discarded. |
| 16 |
type threadContainer struct { |
| 17 |
// threadable contains the base of the threadable items in this instance of the struct |
| 18 |
// |
| 19 |
threadable Threadable |
| 20 |
|
| 21 |
// parent shows which threadable item is the parent of this container, which can be nil for a root node |
| 22 |
// |
| 23 |
parent *threadContainer |
| 24 |
|
| 25 |
// child shows which threadable is the child of this container |
| 26 |
// |
| 27 |
child *threadContainer |
| 28 |
|
| 29 |
// next shows which threadable is the sibling of this container |
| 30 |
// |
| 31 |
next *threadContainer |
| 32 |
|
| 33 |
// forID holds the message id that this container is holding. This is only useful for |
| 34 |
// when we don't ever see the actual email (it is referenced by one email, but we don't |
| 35 |
// have the email as we are parsing a partial set) |
| 36 |
// |
| 37 |
forID string |
| 38 |
} |
| 39 |
|
| 40 |
// flush copies the ThreadContainer tree structure down into the underlying |
| 41 |
// Threadable elements. I.E. make the IThreadable tree look like |
| 42 |
// the ThreadContainer tree. |
| 43 |
func (tc *threadContainer) flush() error { |
| 44 |
// Only a root node can have no threadable element - if we have a parent, then |
| 45 |
// we are not a root node. |
| 46 |
// |
| 47 |
if tc.parent != nil && tc.threadable == nil { |
| 48 |
return NoThreadableError{} |
| 49 |
} |
| 50 |
|
| 51 |
tc.parent = nil |
| 52 |
|
| 53 |
if tc.threadable != nil { |
| 54 |
if tc.child == nil { |
| 55 |
tc.threadable.SetChild(nil) |
| 56 |
} else { |
| 57 |
tc.threadable.SetChild(tc.child.threadable) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
if tc.child != nil { |
| 62 |
_ = tc.child.flush() |
| 63 |
tc.child = nil |
| 64 |
} |
| 65 |
|
| 66 |
if tc.threadable != nil { |
| 67 |
if tc.next == nil { |
| 68 |
tc.threadable.SetNext(nil) |
| 69 |
} else { |
| 70 |
tc.threadable.SetNext(tc.next.threadable) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
if tc.next != nil { |
| 75 |
_ = tc.next.flush() |
| 76 |
tc.next = nil |
| 77 |
} |
| 78 |
|
| 79 |
tc.threadable = nil |
| 80 |
|
| 81 |
return nil |
| 82 |
} |
| 83 |
|
| 84 |
// findChild returns true if child is under self's tree. This is used for |
| 85 |
// detecting circularities in the references header. |
| 86 |
func (tc *threadContainer) findChild(target *threadContainer) bool { |
| 87 |
found := false |
| 88 |
for t := tc.child; t != nil; t = t.next { |
| 89 |
found = found || t == target || t.findChild(target) |
| 90 |
} |
| 91 |
return found |
| 92 |
} |
| 93 |
|
| 94 |
// reverseChildren does what it implies and reverses the order of the child elements |
| 95 |
func (tc *threadContainer) reverseChildren() { |
| 96 |
if tc.child != nil { |
| 97 |
|
| 98 |
var kid, prev, rest *threadContainer |
| 99 |
|
| 100 |
// Reverse the children of this one |
| 101 |
// |
| 102 |
prev = nil |
| 103 |
kid = tc.child |
| 104 |
rest = kid.next |
| 105 |
for kid != nil { |
| 106 |
|
| 107 |
kid.next = prev |
| 108 |
|
| 109 |
prev = kid |
| 110 |
kid = rest |
| 111 |
if rest != nil { |
| 112 |
rest = rest.next |
| 113 |
} |
| 114 |
} |
| 115 |
tc.child = prev |
| 116 |
|
| 117 |
// Now reverse the children of this one's children |
| 118 |
// |
| 119 |
for kid = tc.child; kid != nil; kid = kid.next { |
| 120 |
kid.reverseChildren() |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
func (tc *threadContainer) fillDummy(t Threadable) { |
| 126 |
// Only a root node can have no threadable element - if we have a message id, then |
| 127 |
// we are not a root node. |
| 128 |
// |
| 129 |
if tc.threadable == nil && tc.forID != "" { |
| 130 |
tc.threadable = t.MakeDummy(tc.forID) |
| 131 |
} |
| 132 |
|
| 133 |
// Depth first, but it does not matter |
| 134 |
// |
| 135 |
if tc.child != nil { |
| 136 |
tc.child.fillDummy(t) |
| 137 |
} |
| 138 |
|
| 139 |
// Simple walk this one, no need to save stack space by following the next chain here |
| 140 |
// |
| 141 |
if tc.next != nil { |
| 142 |
tc.next.fillDummy(t) |
| 143 |
} |
| 144 |
} |