Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,31 @@ impl Header {
_ => None,
})
}

/// Find the variable object at a specified path.
///
/// ## Example
///
/// ```rust
/// let mut parser = vcd::Parser::new(&b"
/// $scope module a $end
/// $var integer 32 y counter_b $end
/// $comment this is a comment $end
/// $scope module b $end
/// $var integer 16 n0 counter $end
/// $var integer 16 n1 counter $end
/// $upscope $end
/// $upscope $end
/// $enddefinitions $end
/// "[..]);
/// let header = parser.parse_header().unwrap();
/// let var_count = header.num_vars();
/// assert_eq!(var_count, 3);
/// ```
pub fn num_vars(&self) -> usize {
self.items
.iter()
.map(ScopeItem::num_vars)
.sum()
}
}
11 changes: 11 additions & 0 deletions src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,14 @@ pub enum ScopeItem {
/// `$comment` - Comment
Comment(String),
}

impl ScopeItem {
/// Returns the number of variables in this scope item.
pub fn num_vars(&self) -> usize {
match self {
ScopeItem::Scope(s) => s.items.iter().map(ScopeItem::num_vars).sum(),
ScopeItem::Var(_) => 1,
ScopeItem::Comment(_) => 0,
}
}
}