pub fn insert_child(
        &mut self,
        prefix_count: usize,
        insertable: Insertable,
    ) -> EditResult<Node::Class> {
        let (node_count, class) = match insertable {
            Insertable::CountedNode(count, c) => (
                count,
                Node::Class::from_char(c).ok_or(EditErr::CharNotANode(c))?,
            ),
        };
        let count = prefix_count * node_count;
        // If the count is 0, do nothing and exit
        if count == 0 {
            return Err(EditErr::NoNodesToInsert);
        }
        self.perform_edit(
            |this: &mut Self,
             _parent_and_index: Option<(&'arena Node, usize)>,
             cursor: &'arena Node| {
                log::debug!("Inserting '{}' as a new child.", class.to_char());
                // Clone the node that currently is the cursor, and add the new nodes to the end of
                // the child list
                let mut cloned_cursor = cursor.clone();
                // Add `count` children to the cursor
                for _ in 0..count {
                    // Validate the tree at every step
                    if !cursor.is_valid_child(cursor.children().len(), class) {
                        log::debug!("New node could not be a valid child of the cursor");
                        // Short circuit if `c` couldn't be a valid child of the cursor
                        return Err(EditErr::CannotBeChild {
                            class,
                            parent_name: cursor.display_name(),
                        });
                    }
                    // Add the new child to the children of the cloned cursor
                    cloned_cursor.insert_child(
                        this.arena.alloc(Node::from_class(class)),
                        this.arena,
                        cloned_cursor.children().len(),
                    )?;
                }
                // Move the cursor to the last added child
                this.current_cursor_path
                    .push(cloned_cursor.children().len() - 1);
                // Return success
                Ok((
                    cloned_cursor,
                    EditLocation::Cursor,
                    EditSuccess::InsertChild(class),
                ))
            },
        )
    }
/// Updates the internal state so that the tree now contains `new_node` inserted as the last
/// child of the selected node.  Also moves the cursor so that the new node is selected.
Updates the internal state so that the tree now contains `new_node` inserted as the last
child of the selected node.  Also moves the cursor so that the new node is selected.
Updates the internal state so that the tree now contains ` new_node ` inserted as the last child of the selected node . Also moves the cursor so that the new node is selected .
pub fn insert_child(
        &mut self,
        prefix_count: usize,
        insertable: Insertable,
    ) -> EditResult<Node::Class> {
        let (node_count, class) = match insertable {
            Insertable::CountedNode(count, c) => (
                count,
                Node::Class::from_char(c).ok_or(EditErr::CharNotANode(c))?,
            ),
        };
        let count = prefix_count * node_count;
        // If the count is 0, do nothing and exit
        if count == 0 {
            return Err(EditErr::NoNodesToInsert);
        }
        self.perform_edit(
            |this: &mut Self,
             _parent_and_index: Option<(&'arena Node, usize)>,
             cursor: &'arena Node| {
                log::debug!("Inserting '{}' as a new child.", class.to_char());
                // Clone the node that currently is the cursor, and add the new nodes to the end of
                // the child list
                let mut cloned_cursor = cursor.clone();
                // Add `count` children to the cursor
                for _ in 0..count {
                    // Validate the tree at every step
                    if !cursor.is_valid_child(cursor.children().len(), class) {
                        log::debug!("New node could not be a valid child of the cursor");
                        // Short circuit if `c` couldn't be a valid child of the cursor
                        return Err(EditErr::CannotBeChild {
                            class,
                            parent_name: cursor.display_name(),
                        });
                    }
                    // Add the new child to the children of the cloned cursor
                    cloned_cursor.insert_child(
                        this.arena.alloc(Node::from_class(class)),
                        this.arena,
                        cloned_cursor.children().len(),
                    )?;
                }
                // Move the cursor to the last added child
                this.current_cursor_path
                    .push(cloned_cursor.children().len() - 1);
                // Return success
                Ok((
                    cloned_cursor,
                    EditLocation::Cursor,
                    EditSuccess::InsertChild(class),
                ))
            },
        )
    }
pub fn insert_child ( & mut self , prefix_count : usize , insertable : Insertable , ) -> EditResult < Node :: Class > { let ( node_count , class ) = match insertable { Insertable :: CountedNode ( count , c ) => ( count , Node :: Class :: from_char ( c ) . ok_or ( EditErr :: CharNotANode ( c ) ) ? , ) , } ; let count = prefix_count * node_count ; // If the count is 0, do nothing and exit if count == 0 { return Err ( EditErr :: NoNodesToInsert ) ; } self . perform_edit ( | this : & mut Self , _parent_and_index : Option < ( & ' arena Node , usize ) > , cursor : & ' arena Node | { log :: debug ! ( " " class to_char ( ) ) ; // Clone the node that currently is the cursor, and add the new nodes to the end of // the child list let mut cloned_cursor = cursor . clone ( ) ; // Add `count` children to the cursor for _ in 0 .. count { // Validate the tree at every step if ! cursor . is_valid_child ( cursor . children ( ) . len ( ) , class ) { log :: debug ! ( " " ) ; // Short circuit if `c` couldn't be a valid child of the cursor return Err ( EditErr :: CannotBeChild { class , parent_name : cursor . display_name ( ) , } ) ; } // Add the new child to the children of the cloned cursor cloned_cursor . insert_child ( this . arena . alloc ( Node :: from_class ( class ) ) , this . arena , cloned_cursor . children ( ) . len ( ) , ) ? ; } // Move the cursor to the last added child this . current_cursor_path . push ( cloned_cursor . children ( ) . len ( ) - 1 ) ; // Return success Ok ( ( cloned_cursor , EditLocation :: Cursor , EditSuccess :: InsertChild ( class ) , ) ) } , ) }
Updates the internal state so that the tree now contains `new_node` inserted as the last
child of the selected node.
Updates the internal state so that the tree now contains ` new_node ` inserted as the last child of the selected node .
&mut self
prefix_count
usize
insertable
Insertable
&mut self
prefix_count
usize
insertable
Insertable