Skip to content

Commit

Permalink
refactor stack
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Oct 8, 2024
1 parent 9205fab commit 88399ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
3 changes: 0 additions & 3 deletions src/parsers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ impl<R: Read> BencodeParser<R> {
self.iter += 1;
}

// todo: if we exit the loop with a non default stack, that's an error
// (incomplete bencode value).

Ok(())
}

Expand Down
17 changes: 16 additions & 1 deletion src/parsers/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,37 @@ impl Display for Stack {
}

impl Stack {
/// It adds a new state to the stack.
pub fn push(&mut self, item: State) {
self.items.push(item);
}

/// It returns and consume the stack top.
///
/// It doesn't allow popping the initial state.
///
/// # Panics
///
/// Will panic is the stack state is the initial state.
pub fn pop(&mut self) {
self.guard_immutable_initial_state();
self.items.pop();
}

/// It swaps the stack top with the new item.
///
/// It doesn't allow swapping the initial state.
///
/// # Panics
///
/// Will panic is the stack state is the initial state.
pub fn swap_top(&mut self, new_item: State) {
self.guard_immutable_initial_state();
self.items.pop();
self.push(new_item);
}

/// It returns the top element on the stack without removing it.
/// It returns the top element on the stack without consuming it.
///
/// # Panics
///
Expand Down

0 comments on commit 88399ab

Please sign in to comment.