Implement From<_> for Value to simplify code a lot

This commit is contained in:
Sebastian Dröge 2017-01-13 21:45:23 +01:00
parent ac9ba47403
commit 745a6a3c17
3 changed files with 81 additions and 36 deletions

View file

@ -157,8 +157,7 @@ impl AudioFormat {
flavors::SoundFormat::MP3 | flavors::SoundFormat::MP3 |
flavors::SoundFormat::MP3_8KHZ => { flavors::SoundFormat::MP3_8KHZ => {
Some(Caps::new_simple("audio/mpeg", Some(Caps::new_simple("audio/mpeg",
&[("mpegversion", &value::Value::Int(1)), &[("mpegversion", &1.into()), ("layer", &3.into())]))
("layer", &value::Value::Int(3))]))
} }
flavors::SoundFormat::PCM_NE | flavors::SoundFormat::PCM_NE |
flavors::SoundFormat::PCM_LE => { flavors::SoundFormat::PCM_LE => {
@ -166,21 +165,19 @@ impl AudioFormat {
// Assume little-endian for "PCM_NE", it's probably more common and we have no // Assume little-endian for "PCM_NE", it's probably more common and we have no
// way to know what the endianness of the system creating the stream was // way to know what the endianness of the system creating the stream was
Some(Caps::new_simple("audio/x-raw", Some(Caps::new_simple("audio/x-raw",
&[("layout", &[("layout", &"interleaved".into()),
&value::Value::String("interleaved".into())),
("format", ("format",
&value::Value::String(if self.width == 8 { &if self.width == 8 {
"U8".into() "U8".into()
} else { } else {
"S16LE".into() "S16LE".into()
}))])) })]))
} else { } else {
None None
} }
} }
flavors::SoundFormat::ADPCM => { flavors::SoundFormat::ADPCM => {
Some(Caps::new_simple("audio/x-adpcm", Some(Caps::new_simple("audio/x-adpcm", &[("layout", &"swf".into())]))
&[("layout", &value::Value::String("swf".into()))]))
} }
flavors::SoundFormat::NELLYMOSER_16KHZ_MONO | flavors::SoundFormat::NELLYMOSER_16KHZ_MONO |
flavors::SoundFormat::NELLYMOSER_8KHZ_MONO | flavors::SoundFormat::NELLYMOSER_8KHZ_MONO |
@ -190,10 +187,10 @@ impl AudioFormat {
flavors::SoundFormat::AAC => { flavors::SoundFormat::AAC => {
self.aac_sequence_header.as_ref().map(|header| { self.aac_sequence_header.as_ref().map(|header| {
Caps::new_simple("audio/mpeg", Caps::new_simple("audio/mpeg",
&[("mpegversion", &value::Value::Int(4)), &[("mpegversion", &4.into()),
("framed", &value::Value::Bool(true)), ("framed", &true.into()),
("stream-format", &value::Value::String("raw".into())), ("stream-format", &"raw".into()),
("codec_data", &value::Value::Buffer(header.clone()))]) ("codec_data", &header.into())])
}) })
} }
flavors::SoundFormat::SPEEX => { flavors::SoundFormat::SPEEX => {
@ -231,8 +228,7 @@ impl AudioFormat {
} }
Some(Caps::new_simple("audio/x-speex", Some(Caps::new_simple("audio/x-speex",
&[("streamheader", &[("streamheader",
&value::Value::Array(vec![value::Value::Buffer(header), &vec![header.into(), comment.into()].into())]))
value::Value::Buffer(comment)]))]))
} }
flavors::SoundFormat::DEVICE_SPECIFIC => { flavors::SoundFormat::DEVICE_SPECIFIC => {
// Nobody knows // Nobody knows
@ -242,11 +238,11 @@ impl AudioFormat {
if self.rate != 0 { if self.rate != 0 {
caps.as_mut() caps.as_mut()
.map(|c| c.set_simple(&[("rate", &value::Value::Int(self.rate as i32))])); .map(|c| c.set_simple(&[("rate", &(self.rate as i32).into())]));
} }
if self.channels != 0 { if self.channels != 0 {
caps.as_mut() caps.as_mut()
.map(|c| c.set_simple(&[("channels", &value::Value::Int(self.channels as i32))])); .map(|c| c.set_simple(&[("channels", &(self.channels as i32).into())]));
} }
caps caps
@ -318,8 +314,7 @@ impl VideoFormat {
fn to_caps(&self) -> Option<Caps> { fn to_caps(&self) -> Option<Caps> {
let mut caps = match self.format { let mut caps = match self.format {
flavors::CodecId::SORENSON_H263 => { flavors::CodecId::SORENSON_H263 => {
Some(Caps::new_simple("video/x-flash-video", Some(Caps::new_simple("video/x-flash-video", &[("flvversion", &1.into())]))
&[("flvversion", &value::Value::Int(1))]))
} }
flavors::CodecId::SCREEN => Some(Caps::new_simple("video/x-flash-screen", &[])), flavors::CodecId::SCREEN => Some(Caps::new_simple("video/x-flash-screen", &[])),
flavors::CodecId::VP6 => Some(Caps::new_simple("video/x-vp6-flash", &[])), flavors::CodecId::VP6 => Some(Caps::new_simple("video/x-vp6-flash", &[])),
@ -328,15 +323,15 @@ impl VideoFormat {
flavors::CodecId::H264 => { flavors::CodecId::H264 => {
self.avc_sequence_header.as_ref().map(|header| { self.avc_sequence_header.as_ref().map(|header| {
Caps::new_simple("video/x-h264", Caps::new_simple("video/x-h264",
&[("stream-format", &value::Value::String("avc".into())), &[("stream-format", &"avc".into()),
("codec_data", &value::Value::Buffer(header.clone()))]) ("codec_data", &header.into())])
}) })
} }
flavors::CodecId::H263 => Some(Caps::new_simple("video/x-h263", &[])), flavors::CodecId::H263 => Some(Caps::new_simple("video/x-h263", &[])),
flavors::CodecId::MPEG4Part2 => { flavors::CodecId::MPEG4Part2 => {
Some(Caps::new_simple("video/x-h263", Some(Caps::new_simple("video/x-h263",
&[("mpegversion", &value::Value::Int(4)), &[("mpegversion", &4.into()),
("systemstream", &value::Value::Bool(false))])) ("systemstream", &false.into())]))
} }
flavors::CodecId::JPEG => { flavors::CodecId::JPEG => {
// Unused according to spec // Unused according to spec
@ -346,26 +341,23 @@ impl VideoFormat {
if let (Some(width), Some(height)) = (self.width, self.height) { if let (Some(width), Some(height)) = (self.width, self.height) {
caps.as_mut().map(|c| { caps.as_mut().map(|c| {
c.set_simple(&[("width", &value::Value::Int(width as i32)), c.set_simple(&[("width", &(width as i32).into()),
("height", &value::Value::Int(height as i32))]) ("height", &(height as i32).into())])
}); });
} }
if let Some(par) = self.pixel_aspect_ratio { if let Some(par) = self.pixel_aspect_ratio {
if par.0 != 0 && par.1 != 0 { if par.0 != 0 && par.1 != 0 {
caps.as_mut().map(|c| { caps.as_mut().map(|c| {
c.set_simple(&[("pixel-aspect-ratio", c.set_simple(&[("pixel-aspect-ratio", &(par.0 as i32, par.1 as i32).into())])
&value::Value::Fraction(par.0 as i32, par.1 as i32))])
}); });
} }
} }
if let Some(fps) = self.framerate { if let Some(fps) = self.framerate {
if fps.1 != 0 { if fps.1 != 0 {
caps.as_mut().map(|c| { caps.as_mut()
c.set_simple(&[("framerate", .map(|c| c.set_simple(&[("framerate", &(fps.0 as i32, fps.1 as i32).into())]));
&value::Value::Fraction(fps.0 as i32, fps.1 as i32))])
});
} }
} }

View file

@ -171,12 +171,11 @@ mod tests {
init(); init();
let caps = Caps::new_simple("foo/bar", let caps = Caps::new_simple("foo/bar",
&[("int", &Value::Int(12)), &[("int", &12.into()),
("bool", &Value::Bool(true)), ("bool", &true.into()),
("string", &Value::String("bla".into())), ("string", &"bla".into()),
("fraction", &Value::Fraction(1, 2)), ("fraction", &(1, 2).into()),
("array", ("array", &vec![1.into(), 2.into()].into())]);
&Value::Array(vec![Value::Int(1), Value::Int(2)]))]);
assert_eq!(caps.to_string(), assert_eq!(caps.to_string(),
"foo/bar, int=(int)12, bool=(boolean)true, string=(string)bla, \ "foo/bar, int=(int)12, bool=(boolean)true, string=(string)bla, \
fraction=(fraction)1/2, array=(int)< 1, 2 >"); fraction=(fraction)1/2, array=(int)< 1, 2 >");

View file

@ -111,3 +111,57 @@ impl Drop for GValue {
} }
} }
} }
impl From<bool> for Value {
fn from(f: bool) -> Value {
Value::Bool(f)
}
}
impl From<i32> for Value {
fn from(f: i32) -> Value {
Value::Int(f)
}
}
impl From<String> for Value {
fn from(f: String) -> Value {
Value::String(f)
}
}
impl<'a> From<&'a str> for Value {
fn from(f: &'a str) -> Value {
Value::String(f.into())
}
}
impl From<(i32, i32)> for Value {
fn from((f_n, f_d): (i32, i32)) -> Value {
Value::Fraction(f_n, f_d)
}
}
impl From<Buffer> for Value {
fn from(f: Buffer) -> Value {
Value::Buffer(f)
}
}
impl<'a> From<&'a Buffer> for Value {
fn from(f: &'a Buffer) -> Value {
Value::Buffer(f.clone())
}
}
impl From<Vec<Value>> for Value {
fn from(f: Vec<Value>) -> Value {
Value::Array(f)
}
}
impl<'a> From<&'a [Value]> for Value {
fn from(f: &'a [Value]) -> Value {
Value::Array(f.to_vec())
}
}