| 1 | // Take a look at the license at the top of the repository in the LICENSE file. |
| 2 | |
| 3 | use glib::prelude::*; |
| 4 | |
| 5 | use crate::value::GstValueExt; |
| 6 | |
| 7 | mod sealed { |
| 8 | pub trait Sealed {} |
| 9 | impl<T: super::IsA<glib::Object>> Sealed for T {} |
| 10 | } |
| 11 | |
| 12 | pub trait GObjectExtManualGst: sealed::Sealed + IsA<glib::Object> + 'static { |
| 13 | #[doc (alias = "gst_util_set_object_arg" )] |
| 14 | #[track_caller ] |
| 15 | fn set_property_from_str(&self, name: &str, value: &str) { |
| 16 | let pspec = self.find_property(name).unwrap_or_else(|| { |
| 17 | panic!("property ' {}' of type ' {}' not found" , name, self.type_()); |
| 18 | }); |
| 19 | |
| 20 | let value = { |
| 21 | if pspec.value_type() == crate::Structure::static_type() && value == "NULL" { |
| 22 | None::<crate::Structure>.to_value() |
| 23 | } else { |
| 24 | #[cfg (feature = "v1_20" )] |
| 25 | { |
| 26 | glib::Value::deserialize_with_pspec(value, &pspec).unwrap_or_else(|_| { |
| 27 | panic!( |
| 28 | "property '{}' of type '{}' can't be set from string '{}'" , |
| 29 | name, |
| 30 | self.type_(), |
| 31 | value, |
| 32 | ) |
| 33 | }) |
| 34 | } |
| 35 | #[cfg (not(feature = "v1_20" ))] |
| 36 | { |
| 37 | glib::Value::deserialize(value, pspec.value_type()).unwrap_or_else(|_| { |
| 38 | panic!( |
| 39 | "property ' {}' of type ' {}' can't be set from string ' {}'" , |
| 40 | name, |
| 41 | self.type_(), |
| 42 | value, |
| 43 | ) |
| 44 | }) |
| 45 | } |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | self.set_property(name, value) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl<O: IsA<glib::Object>> GObjectExtManualGst for O {} |
| 54 | |
| 55 | #[cfg (test)] |
| 56 | mod tests { |
| 57 | use super::*; |
| 58 | |
| 59 | #[test ] |
| 60 | fn test_set_property_from_str() { |
| 61 | crate::init().unwrap(); |
| 62 | |
| 63 | let fakesink = crate::ElementFactory::make("fakesink" ).build().unwrap(); |
| 64 | fakesink.set_property_from_str("state-error" , "ready-to-paused" ); |
| 65 | let v = fakesink.property_value("state-error" ); |
| 66 | let (_klass, e) = glib::EnumValue::from_value(&v).unwrap(); |
| 67 | assert_eq!(e.nick(), "ready-to-paused" ); |
| 68 | } |
| 69 | } |
| 70 | |