pub struct Block { /* private fields */ }
Expand description
Represents a block, a collection of Statement
s that can end with a LastStatement
.
Implementations§
Source§impl Block
impl Block
Sourcepub fn new(
statements: Vec<Statement>,
last_statement: Option<LastStatement>,
) -> Self
pub fn new( statements: Vec<Statement>, last_statement: Option<LastStatement>, ) -> Self
Creates a new Block with the given statements and optional last statement.
Sourcepub fn with_tokens(self, tokens: BlockTokens) -> Self
pub fn with_tokens(self, tokens: BlockTokens) -> Self
Attaches token information to this block and returns the updated block.
Sourcepub fn set_tokens(&mut self, tokens: BlockTokens)
pub fn set_tokens(&mut self, tokens: BlockTokens)
Attaches token information to this block.
Sourcepub fn get_tokens(&self) -> Option<&BlockTokens>
pub fn get_tokens(&self) -> Option<&BlockTokens>
Returns a reference to the token information attached to this block, if any.
Sourcepub fn mutate_tokens(&mut self) -> Option<&mut BlockTokens>
pub fn mutate_tokens(&mut self) -> Option<&mut BlockTokens>
Returns a mutable reference to the token information attached to this block, if any.
Sourcepub fn push_statement<T: Into<Statement>>(&mut self, statement: T)
pub fn push_statement<T: Into<Statement>>(&mut self, statement: T)
Adds a statement to the end of the block. Updates token information if present.
This method maintains consistency between the statements and their token information.
Sourcepub fn remove_statement(&mut self, index: usize)
pub fn remove_statement(&mut self, index: usize)
Removes a statement at the specified index. Updates token information if present.
Does nothing if the index is out of bounds.
Sourcepub fn with_statement<T: Into<Statement>>(self, statement: T) -> Self
pub fn with_statement<T: Into<Statement>>(self, statement: T) -> Self
Adds a statement to the end of the block and returns the updated block.
Sourcepub fn insert_statement(
&mut self,
index: usize,
statement: impl Into<Statement>,
)
pub fn insert_statement( &mut self, index: usize, statement: impl Into<Statement>, )
Inserts a statement at the specified index, appending if the index is beyond the end. Updates token information if present.
This method maintains consistency between the statements and their token information.
Sourcepub fn set_last_statement(&mut self, last_statement: impl Into<LastStatement>)
pub fn set_last_statement(&mut self, last_statement: impl Into<LastStatement>)
Sets the last statement of the block.
Sourcepub fn with_last_statement(
self,
last_statement: impl Into<LastStatement>,
) -> Self
pub fn with_last_statement( self, last_statement: impl Into<LastStatement>, ) -> Self
Sets the last statement of the block and returns the updated block.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks if the block is empty (contains no statements or last statement).
Sourcepub fn statements_len(&self) -> usize
pub fn statements_len(&self) -> usize
Returns the number of statements in the block.
Sourcepub fn iter_statements(&self) -> impl Iterator<Item = &Statement>
pub fn iter_statements(&self) -> impl Iterator<Item = &Statement>
Returns an iterator over references to the statements in the block.
Sourcepub fn reverse_iter_statements(&self) -> impl Iterator<Item = &Statement>
pub fn reverse_iter_statements(&self) -> impl Iterator<Item = &Statement>
Returns an iterator over references to the statements in the block in reverse order.
Sourcepub fn get_last_statement(&self) -> Option<&LastStatement>
pub fn get_last_statement(&self) -> Option<&LastStatement>
Returns a reference to the last statement of the block, if any.
Sourcepub fn filter_statements<F>(&mut self, f: F)
pub fn filter_statements<F>(&mut self, f: F)
Filters statements in the block, removing those for which the predicate returns false
.
Updates token information if present.
This method ensures token information stays consistent with the statements collection.
Sourcepub fn filter_mut_statements<F>(&mut self, f: F)
pub fn filter_mut_statements<F>(&mut self, f: F)
Filters statements with mutable access, removing those for which the predicate returns false
.
Updates token information if present.
This method gives mutable access to statements during filtering.
Sourcepub fn truncate(&mut self, length: usize)
pub fn truncate(&mut self, length: usize)
Truncates the statements to the specified length.
Updates token information if present.
Sourcepub fn iter_mut_statements(&mut self) -> impl Iterator<Item = &mut Statement>
pub fn iter_mut_statements(&mut self) -> impl Iterator<Item = &mut Statement>
Returns a mutable iterator over the statements in the block.
Sourcepub fn first_statement(&self) -> Option<&Statement>
pub fn first_statement(&self) -> Option<&Statement>
Returns a reference to the first statement in the block, if any.
Sourcepub fn first_mut_statement(&mut self) -> Option<&mut Statement>
pub fn first_mut_statement(&mut self) -> Option<&mut Statement>
Returns a mutable reference to the first statement in the block, if any.
Sourcepub fn take_statements(&mut self) -> Vec<Statement>
pub fn take_statements(&mut self) -> Vec<Statement>
Removes all statements from the block and returns them.
Clears token information if present.
Sourcepub fn take_last_statement(&mut self) -> Option<LastStatement>
pub fn take_last_statement(&mut self) -> Option<LastStatement>
Removes and returns the last statement of the block, if any.
Clears the last semicolon token if present.
Sourcepub fn set_statements(&mut self, statements: Vec<Statement>)
pub fn set_statements(&mut self, statements: Vec<Statement>)
Sets the statements of the block.
Clears any existing semicolon tokens.
Sourcepub fn mutate_last_statement(&mut self) -> Option<&mut LastStatement>
pub fn mutate_last_statement(&mut self) -> Option<&mut LastStatement>
Returns a mutable reference to the last statement of the block, if any.
Sourcepub fn replace_last_statement<S: Into<LastStatement>>(
&mut self,
statement: S,
) -> Option<LastStatement>
pub fn replace_last_statement<S: Into<LastStatement>>( &mut self, statement: S, ) -> Option<LastStatement>
Replaces the last statement with the given statement and returns the old one, if any.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears all statements and the last statement from the block.
Also clears all token information if present.
Sourcepub fn mutate_first_token(&mut self) -> &mut Token
pub fn mutate_first_token(&mut self) -> &mut Token
Returns a mutable reference to the first token in this block, creating it if it doesn’t exist.
Sourcepub fn mutate_last_token(&mut self) -> &mut Token
pub fn mutate_last_token(&mut self) -> &mut Token
Returns a mutable reference to the last token for this block, creating it if missing.
Sourcepub fn clear_comments(&mut self)
pub fn clear_comments(&mut self)
Clears all comments from the tokens in this node.
Sourcepub fn clear_whitespaces(&mut self)
pub fn clear_whitespaces(&mut self)
Clears all whitespaces information from the tokens in this node.
Trait Implementations§
Source§impl From<LastStatement> for Block
impl From<LastStatement> for Block
Source§fn from(statement: LastStatement) -> Block
fn from(statement: LastStatement) -> Block
Source§impl From<ReturnStatement> for Block
impl From<ReturnStatement> for Block
Source§fn from(statement: ReturnStatement) -> Block
fn from(statement: ReturnStatement) -> Block
impl Eq for Block
impl StructuralPartialEq for Block
Auto Trait Implementations§
impl Freeze for Block
impl RefUnwindSafe for Block
impl Send for Block
impl Sync for Block
impl Unpin for Block
impl UnwindSafe for Block
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<I, T> ExtractContext<I, ()> for T
impl<I, T> ExtractContext<I, ()> for T
Source§fn extract_context(self, _original_input: I)
fn extract_context(self, _original_input: I)
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more