Add methods to iter over format spec elements

This commit is contained in:
Dhruv Manilawala 2024-05-13 14:17:23 +05:30
parent 128414cd95
commit ce4d4ae6ac
1 changed files with 18 additions and 0 deletions

View File

@ -1087,6 +1087,24 @@ pub struct FStringFormatSpec {
pub elements: Vec<FStringElement>,
}
impl FStringFormatSpec {
/// Returns an iterator over all the [`FStringLiteralElement`] nodes contained in this format
/// spec of the f-string.
pub fn literals(&self) -> impl Iterator<Item = &FStringLiteralElement> {
self.elements
.iter()
.filter_map(|element| element.as_literal())
}
/// Returns an iterator over all the [`FStringExpressionElement`] nodes contained in this
/// format spec of the f-string.
pub fn expressions(&self) -> impl Iterator<Item = &FStringExpressionElement> {
self.elements
.iter()
.filter_map(|element| element.as_expression())
}
}
impl Ranged for FStringFormatSpec {
fn range(&self) -> TextRange {
self.range