Skip to content

Commit

Permalink
Merge pull request #413 from lucatrv/add_range_headers_method
Browse files Browse the repository at this point in the history
feat: add `Range::headers` method
  • Loading branch information
tafia committed Mar 8, 2024
2 parents e53b0cb + 7d36dd3 commit 70facea
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,34 @@ impl<T: CellType> Range<T> {
}
}

impl<T: CellType + fmt::Display> Range<T> {
/// Get range headers.
///
/// # Examples
/// ```
/// use calamine::{Range, Data};
///
/// let mut range = Range::new((0, 0), (5, 2));
/// range.set_value((0, 0), Data::String(String::from("a")));
/// range.set_value((0, 1), Data::Int(1));
/// range.set_value((0, 2), Data::Bool(true));
/// let headers = range.headers();
/// assert_eq!(
/// headers,
/// Some(vec![
/// String::from("a"),
/// String::from("1"),
/// String::from("true")
/// ])
/// );
/// ```
pub fn headers(&self) -> Option<Vec<String>> {
self.rows()
.next()
.map(|row| row.iter().map(ToString::to_string).collect())
}
}

impl<T: CellType> Index<usize> for Range<T> {
type Output = [T];
fn index(&self, index: usize) -> &[T] {
Expand Down

0 comments on commit 70facea

Please sign in to comment.