How to set root from orphan node
Drakulix opened this issue · comments
This is basically the last topic from #7 .
@iwburns , you said remove_node_lift_children
is not supposed to set the root node, even if only one child remaining.
Given that case, there is no way to manually set that child as the new root, without removing it at first.
My current workaround:
let new_root_id = self.tree.get(&old_root_id).unwrap().children()[0].clone();
let children = self.tree.get(&new_root_id).unwrap().children().clone();
let new_root = self.tree.remove_node_orphan_children(new_root_id.clone()).unwrap();
self.tree.remove_node_drop_children(old_root_id).unwrap();
self.tree.set_root(new_root);
for child in children
{
self.tree.move_node_to_parent(&child, &new_root_id);
}
This is hideous.
How would you feel about a method similar to move_node_to_parent
except that it sets the node as the root and makes the current root it's last child?
I know that it would be nice if move_node_lift_children
did what you wanted here, but there's just something about that behavior that seems strange to me. I'm not saying I am completely rejecting that behavior as a possibility, but I'm not a fan of it at the moment.
Also, I think the other method described above would be useful in its own right, regardless of what we do with remove node lift children.
Yes that would be much better.
I do not have a problem with move_node_lift_children
not doing exactly what I want, I just need something, that is not that workaround.
The workaround is even more broken, because the NodeId
of the new root changes, which requires a bunch of new logic in my current program. I might start an implementation on move_node_to_root
rather sooner then later.
Feel free, I would hop on it myself but I'm out shopping right now.
The only thing I'm not sure about is what to call it. I like set_as_root
but I also feel like it's too close to the existing set_root
. Thoughts?
Actually, I like your suggestion of move_node_to_root
as well, but if you think of any other good alternatives feel free to shout them out.
I like move_node_to_root
, because that is more the group of functions it belongs to.
In regards to #19 , we could then even do something like this:
pub enum MoveBehavior
{
Root,
Parent(&NodeId),
}
pub fn move_node(&mut self, node: &NodeId, behavior: MoveBehavior) -> Result<....>