From 2fc3470fa1368d9b76ee3ee704803ec410975ffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Sat, 11 Nov 2017 13:14:49 +0100 Subject: [PATCH] Add a Builder for caps and structures --- gstreamer/src/caps.rs | 45 ++++++++++++++++++++++++++++++++++++++ gstreamer/src/structure.rs | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/gstreamer/src/caps.rs b/gstreamer/src/caps.rs index e8d0e8420..ce1cc8ae4 100644 --- a/gstreamer/src/caps.rs +++ b/gstreamer/src/caps.rs @@ -28,6 +28,11 @@ unsafe impl MiniObject for CapsRef { } impl GstRc { + pub fn builder(name: &str) -> Builder { + assert_initialized_main_thread!(); + Builder::new(name) + } + pub fn new_empty() -> Self { assert_initialized_main_thread!(); unsafe { from_glib_full(ffi::gst_caps_new_empty()) } @@ -366,6 +371,29 @@ impl ToOwned for CapsRef { unsafe impl Sync for CapsRef {} unsafe impl Send for CapsRef {} +pub struct Builder { + s: ::Structure, +} + +impl Builder { + fn new(name: &str) -> Self { + Builder { + s: ::Structure::new_empty(name), + } + } + + pub fn field(mut self, name: &str, value: V) -> Self { + self.s.set(name, value); + self + } + + pub fn build(self) -> Caps { + let mut caps = Caps::new_empty(); + caps.get_mut().unwrap().append_structure(self.s); + caps + } +} + #[cfg(test)] mod tests { use super::*; @@ -406,4 +434,21 @@ mod tests { ).as_ref() ); } + + #[test] + fn test_builder() { + ::init().unwrap(); + + let caps = Caps::builder("foo/bar") + .field("int", 12) + .field("bool", true) + .field("string", "bla") + .field("fraction", Fraction::new(1, 2)) + .field("array", Array::new(&[&1, &2])) + .build(); + assert_eq!( + caps.to_string(), + "foo/bar, int=(int)12, bool=(boolean)true, string=(string)bla, fraction=(fraction)1/2, array=(int)< 1, 2 >" + ); + } } diff --git a/gstreamer/src/structure.rs b/gstreamer/src/structure.rs index 7e4eb14b3..1f2124bdb 100644 --- a/gstreamer/src/structure.rs +++ b/gstreamer/src/structure.rs @@ -28,6 +28,11 @@ use gobject_ffi; pub struct Structure(*mut StructureRef, PhantomData); impl Structure { + pub fn builder(name: &str) -> Builder { + assert_initialized_main_thread!(); + Builder::new(name) + } + pub fn new_empty(name: &str) -> Structure { assert_initialized_main_thread!(); Structure( @@ -591,6 +596,27 @@ impl<'a> DoubleEndedIterator for Iter<'a> { impl<'a> ExactSizeIterator for Iter<'a> {} +pub struct Builder { + s: Structure, +} + +impl Builder { + fn new(name: &str) -> Self { + Builder { + s: Structure::new_empty(name), + } + } + + pub fn field(mut self, name: &str, value: V) -> Self { + self.s.set(name, value); + self + } + + pub fn build(self) -> Structure { + self.s + } +} + #[cfg(test)] mod tests { use super::*; @@ -624,4 +650,20 @@ mod tests { let s2 = Structure::new("test", &[("f1", &"abc"), ("f2", &"bcd"), ("f3", &123i32)]); assert_eq!(s, s2); } + + #[test] + fn test_builder() { + ::init().unwrap(); + + let s = Structure::builder("test") + .field("f1", "abc") + .field("f2", String::from("bcd")) + .field("f3", 123i32) + .build(); + + assert_eq!(s.get_name(), "test"); + assert_eq!(s.get::<&str>("f1").unwrap(), "abc"); + assert_eq!(s.get::<&str>("f2").unwrap(), "bcd"); + assert_eq!(s.get::("f3").unwrap(), 123i32); + } }