From 54d8065dd34659547190152fbdb7f8fcce0e614d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Sun, 28 Oct 2018 13:46:28 +0000 Subject: [PATCH] Add TagList::iter_tag_list_simple() for getting a single value per tag --- gstreamer/src/tags.rs | 78 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/gstreamer/src/tags.rs b/gstreamer/src/tags.rs index cc49c5c8d..e3da2de6c 100644 --- a/gstreamer/src/tags.rs +++ b/gstreamer/src/tags.rs @@ -476,6 +476,10 @@ impl TagListRef { TagListIterator::new(self) } + pub fn iter_tag_list_simple(&self) -> TagListSimpleIterator { + TagListSimpleIterator::new(self) + } + pub fn to_string(&self) -> String { unsafe { from_glib_full(ffi::gst_tag_list_to_string(self.as_ptr())) } } @@ -699,6 +703,64 @@ impl<'a> DoubleEndedIterator for TagListIterator<'a> { impl<'a> ExactSizeIterator for TagListIterator<'a> {} +pub struct TagListSimpleIterator<'a> { + taglist: &'a TagListRef, + idx: u32, + size: u32, +} + +impl<'a> TagListSimpleIterator<'a> { + fn new(taglist: &'a TagListRef) -> TagListSimpleIterator<'a> { + skip_assert_initialized!(); + let size = taglist.n_tags(); + TagListSimpleIterator { + taglist, + idx: 0, + size: if size > 0 { size as u32 } else { 0 }, + } + } +} + +impl<'a> Iterator for TagListSimpleIterator<'a> { + type Item = (&'a str, glib::SendValue); + + fn next(&mut self) -> Option { + if self.idx >= self.size { + return None; + } + + let name = self.taglist.nth_tag_name(self.idx); + let item = (name, self.taglist.get_generic(name).unwrap()); + self.idx += 1; + + Some(item) + } + + fn size_hint(&self) -> (usize, Option) { + if self.idx == self.size { + return (0, Some(0)); + } + + let remaining = (self.size - self.idx) as usize; + + (remaining, Some(remaining)) + } +} + +impl<'a> DoubleEndedIterator for TagListSimpleIterator<'a> { + fn next_back(&mut self) -> Option { + if self.idx == self.size { + return None; + } + + self.size -= 1; + let name = self.taglist.nth_tag_name(self.idx); + Some((name, self.taglist.get_generic(name).unwrap())) + } +} + +impl<'a> ExactSizeIterator for TagListSimpleIterator<'a> {} + #[cfg(test)] mod tests { use super::*; @@ -836,5 +898,21 @@ mod tests { let first_duration = tag_iter.next().unwrap(); assert_eq!(first_duration.get(), Some(::SECOND * 120)); assert!(tag_iter.next().is_none()); + + // TagListSimpleIterator + let mut tag_list_iter = tags.iter_tag_list_simple(); + assert_eq!(tag_list_iter.size_hint(), (2, Some(2))); + + let (tag_name, tag_value) = tag_list_iter.next().unwrap(); + assert_eq!(tag_name, *TAG_TITLE); + assert_eq!( + tag_value.get(), + Some("some title, second title, third title") + ); + + let (tag_name, tag_value) = tag_list_iter.next().unwrap(); + assert_eq!(tag_name, *TAG_DURATION); + assert_eq!(tag_value.get(), Some(::SECOND * 120)); + assert!(tag_iter.next().is_none()); } }