Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Zeeshan Ali
gstreamer-rs
Commits
9f2f6841
Commit
9f2f6841
authored
Jul 28, 2018
by
François Laignel
Committed by
Sebastian Dröge
Jul 30, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
format: serde: avoid redundant `()` for some types
parent
dfc96167
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
136 additions
and
101 deletions
+136
-101
gstreamer/src/format.rs
gstreamer/src/format.rs
+0
-99
gstreamer/src/format_serde.rs
gstreamer/src/format_serde.rs
+132
-0
gstreamer/src/lib.rs
gstreamer/src/lib.rs
+2
-0
gstreamer/src/value_serde.rs
gstreamer/src/value_serde.rs
+2
-2
No files found.
gstreamer/src/format.rs
View file @
9f2f6841
...
...
@@ -24,13 +24,10 @@ pub enum GenericFormattedValue {
}
#[derive(PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Clone,
Copy,
Debug,
Default)]
#[cfg_attr(feature
=
"ser_de"
,
derive(Serialize,
Deserialize))]
pub
struct
Default
(
pub
Option
<
u64
>
);
#[derive(PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Clone,
Copy,
Debug,
Default)]
#[cfg_attr(feature
=
"ser_de"
,
derive(Serialize,
Deserialize))]
pub
struct
Bytes
(
pub
Option
<
u64
>
);
#[derive(PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Clone,
Copy,
Debug,
Default)]
#[cfg_attr(feature
=
"ser_de"
,
derive(Serialize,
Deserialize))]
pub
struct
Buffers
(
pub
Option
<
u64
>
);
pub
type
Time
=
ClockTime
;
...
...
@@ -486,99 +483,3 @@ impl_format_value_traits!(Default, Default, Default);
impl_format_value_traits!
(
Bytes
,
Bytes
,
Bytes
);
impl_format_value_traits!
(
ClockTime
,
Time
,
Time
);
impl_format_value_traits!
(
Buffers
,
Buffers
,
Buffers
);
#[cfg(test)]
mod
tests
{
#[cfg(feature
=
"ser_de"
)]
#[test]
fn
test_serialize
()
{
extern
crate
ron
;
extern
crate
serde_json
;
use
super
::
Buffers
;
use
super
::
Bytes
;
use
super
::
Default
;
use
ClockTime
;
use
Format
;
use
GenericFormattedValue
;
::
init
()
.unwrap
();
let
mut
pretty_config
=
ron
::
ser
::
PrettyConfig
::
default
();
pretty_config
.new_line
=
""
.to_string
();
let
value
=
GenericFormattedValue
::
Undefined
(
42
);
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Undefined(42)"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Undefined
\"
:42}"
.to_owned
(),
res
);
let
value
=
GenericFormattedValue
::
Default
(
Default
(
Some
(
42
)));
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Default((Some(42)))"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Default
\"
:42}"
.to_owned
(),
res
);
let
value
=
GenericFormattedValue
::
Default
(
Default
(
None
));
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Default((None))"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Default
\"
:null}"
.to_owned
(),
res
);
let
value
=
GenericFormattedValue
::
Bytes
(
Bytes
(
Some
(
42
)));
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Bytes((Some(42)))"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Bytes
\"
:42}"
.to_owned
(),
res
);
let
value
=
GenericFormattedValue
::
Time
(
ClockTime
::
from_nseconds
(
42_123_456_789
));
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Time(Some(42123456789))"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Time
\"
:42123456789}"
.to_owned
(),
res
);
let
value
=
GenericFormattedValue
::
Buffers
(
Buffers
(
Some
(
42
)));
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Buffers((Some(42)))"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Buffers
\"
:42}"
.to_owned
(),
res
);
let
value
=
GenericFormattedValue
::
Percent
(
Some
(
42
));
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Percent(Some(42))"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Percent
\"
:42}"
.to_owned
(),
res
);
let
value
=
GenericFormattedValue
::
Other
(
Format
::
Percent
,
42
);
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Other(Percent, 42)"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Other
\"
:[
\"
Percent
\"
,42]}"
.to_owned
(),
res
);
let
value
=
GenericFormattedValue
::
Other
(
Format
::
__
Unknown
(
7
),
42
);
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Other(__Unknown(7), 42)"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Other
\"
:[{
\"
__Unknown
\"
:7},42]}"
.to_owned
(),
res
);
}
#[cfg(feature
=
"ser_de"
)]
#[test]
fn
test_deserialize
()
{
extern
crate
ron
;
extern
crate
serde_json
;
use
Format
;
use
GenericFormattedValue
;
::
init
()
.unwrap
();
let
format_ron
=
"Other(Percent, 42)"
;
let
format
:
GenericFormattedValue
=
ron
::
de
::
from_str
(
format_ron
)
.unwrap
();
assert_eq!
(
format
,
GenericFormattedValue
::
Other
(
Format
::
Percent
,
42
));
let
format_json
=
"{
\"
Other
\"
:[
\"
Percent
\"
,42]}"
;
let
format
:
GenericFormattedValue
=
serde_json
::
from_str
(
format_json
)
.unwrap
();
assert_eq!
(
format
,
GenericFormattedValue
::
Other
(
Format
::
Percent
,
42
));
}
}
gstreamer/src/format_serde.rs
0 → 100644
View file @
9f2f6841
// Copyright (C) 2018 François Laignel <fengalin@free.fr>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use
serde
::
de
::{
Deserialize
,
Deserializer
};
use
serde
::
ser
::{
Serialize
,
Serializer
};
use
format
::{
Buffers
,
Bytes
,
Default
};
// Manual implementation for some types that would otherwise yield representations such as:
// "Default((Some(42)))"
macro_rules!
impl_ser_de
(
(
$t:ident
)
=>
{
impl
Serialize
for
$t
{
fn
serialize
<
S
:
Serializer
>
(
&
self
,
serializer
:
S
)
->
Result
<
S
::
Ok
,
S
::
Error
>
{
self
.0
.serialize
(
serializer
)
}
}
impl
<
'de
>
Deserialize
<
'de
>
for
$t
{
fn
deserialize
<
D
:
Deserializer
<
'de
>>
(
deserializer
:
D
)
->
Result
<
Self
,
D
::
Error
>
{
Option
::
<
u64
>
::
deserialize
(
deserializer
)
.map
(|
value
|
$t
(
value
))
}
}
}
);
impl_ser_de!
(
Buffers
);
impl_ser_de!
(
Bytes
);
impl_ser_de!
(
Default
);
#[cfg(test)]
mod
tests
{
extern
crate
ron
;
extern
crate
serde_json
;
use
format
::
Default
;
use
Format
;
use
GenericFormattedValue
;
#[test]
fn
test_serialize
()
{
use
format
::
Buffers
;
use
format
::
Bytes
;
use
ClockTime
;
::
init
()
.unwrap
();
let
mut
pretty_config
=
ron
::
ser
::
PrettyConfig
::
default
();
pretty_config
.new_line
=
""
.to_string
();
let
value
=
GenericFormattedValue
::
Undefined
(
42
);
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Undefined(42)"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Undefined
\"
:42}"
.to_owned
(),
res
);
let
value
=
GenericFormattedValue
::
Default
(
Default
(
Some
(
42
)));
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Default(Some(42))"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Default
\"
:42}"
.to_owned
(),
res
);
let
value
=
GenericFormattedValue
::
Default
(
Default
(
None
));
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Default(None)"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Default
\"
:null}"
.to_owned
(),
res
);
let
value
=
GenericFormattedValue
::
Bytes
(
Bytes
(
Some
(
42
)));
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Bytes(Some(42))"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Bytes
\"
:42}"
.to_owned
(),
res
);
let
value
=
GenericFormattedValue
::
Time
(
ClockTime
::
from_nseconds
(
42_123_456_789
));
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Time(Some(42123456789))"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Time
\"
:42123456789}"
.to_owned
(),
res
);
let
value
=
GenericFormattedValue
::
Buffers
(
Buffers
(
Some
(
42
)));
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Buffers(Some(42))"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Buffers
\"
:42}"
.to_owned
(),
res
);
let
value
=
GenericFormattedValue
::
Percent
(
Some
(
42
));
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Percent(Some(42))"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Percent
\"
:42}"
.to_owned
(),
res
);
let
value
=
GenericFormattedValue
::
Other
(
Format
::
Percent
,
42
);
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Other(Percent, 42)"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Other
\"
:[
\"
Percent
\"
,42]}"
.to_owned
(),
res
);
let
value
=
GenericFormattedValue
::
Other
(
Format
::
__
Unknown
(
7
),
42
);
let
res
=
ron
::
ser
::
to_string_pretty
(
&
value
,
pretty_config
.clone
());
assert_eq!
(
Ok
(
"Other(__Unknown(7), 42)"
.to_owned
()),
res
);
let
res
=
serde_json
::
to_string
(
&
value
)
.unwrap
();
assert_eq!
(
"{
\"
Other
\"
:[{
\"
__Unknown
\"
:7},42]}"
.to_owned
(),
res
);
}
#[test]
fn
test_deserialize
()
{
::
init
()
.unwrap
();
let
format_ron
=
"Default(Some(42))"
;
let
format
:
GenericFormattedValue
=
ron
::
de
::
from_str
(
format_ron
)
.unwrap
();
assert_eq!
(
format
,
GenericFormattedValue
::
Default
(
Default
(
Some
(
42
))));
let
format_json
=
"{
\"
Default
\"
:42}"
;
let
format
:
GenericFormattedValue
=
serde_json
::
from_str
(
format_json
)
.unwrap
();
assert_eq!
(
format
,
GenericFormattedValue
::
Default
(
Default
(
Some
(
42
))));
let
format_ron
=
"Other(Percent, 42)"
;
let
format
:
GenericFormattedValue
=
ron
::
de
::
from_str
(
format_ron
)
.unwrap
();
assert_eq!
(
format
,
GenericFormattedValue
::
Other
(
Format
::
Percent
,
42
));
let
format_json
=
"{
\"
Other
\"
:[
\"
Percent
\"
,42]}"
;
let
format
:
GenericFormattedValue
=
serde_json
::
from_str
(
format_json
)
.unwrap
();
assert_eq!
(
format
,
GenericFormattedValue
::
Other
(
Format
::
Percent
,
42
));
}
}
gstreamer/src/lib.rs
View file @
9f2f6841
...
...
@@ -214,6 +214,8 @@ pub use typefind::*;
pub
mod
format
;
pub
use
format
::{
FormattedValue
,
GenericFormattedValue
,
SpecificFormattedValue
};
#[cfg(feature
=
"ser_de"
)]
pub
(
crate
)
mod
format_serde
;
mod
segment
;
pub
use
segment
::
*
;
...
...
gstreamer/src/value_serde.rs
View file @
9f2f6841
...
...
@@ -55,7 +55,7 @@ impl<'a> Serialize for Fraction {
impl
<
'de
>
Deserialize
<
'de
>
for
Fraction
{
fn
deserialize
<
D
:
Deserializer
<
'de
>>
(
deserializer
:
D
)
->
Result
<
Self
,
D
::
Error
>
{
Rational32
::
deserialize
(
deserializer
)
.
and_then
(|
rational
|
Ok
(
Fraction
::
new
(
*
rational
.numer
(),
*
rational
.denom
()))
)
.
map
(|
rational
|
Fraction
::
new
(
*
rational
.numer
(),
*
rational
.denom
()))
}
}
...
...
@@ -165,7 +165,7 @@ macro_rules! de_value(
{
let
value
=
$seq
.next_element
::
<
$t
>
()
?
.
and_then
(|
base_value
|
Some
(
base_value
.to_value
())
)
;
.
map
(|
base_value
|
base_value
.to_value
());
Ok
(
value
)
}
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment