Skip to content

Commit 2201395

Browse files
Add serde option
Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
1 parent 11947bf commit 2201395

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ edition = "2018"
1414
derivative = "1.0.3"
1515
libc = "0.2"
1616
thiserror = "1.0.9"
17+
serde = { version = "1.0.104", features = ["derive"], optional = true }
1718

1819
[features]
1920
no_wrapper = []
21+
default = ["serde"]

src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ mod v4l2;
4545

4646
pub type Result<T> = result::Result<T, Error>;
4747

48+
#[cfg(feature = "serde")]
49+
extern crate serde;
50+
4851
#[macro_use]
4952
extern crate derivative;
5053

@@ -62,7 +65,8 @@ pub enum Error {
6265
BadField,
6366
}
6467

65-
#[derive(Debug)]
68+
#[derive(Debug, Copy, Clone)]
69+
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
6670
pub struct Config<'a> {
6771
/// The mix of numerator and denominator. v4l2 uses frame intervals instead of frame rates.
6872
/// Default is `(1, 10)`.
@@ -94,6 +98,7 @@ impl<'a> Default for Config<'a> {
9498
}
9599
}
96100

101+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
97102
pub struct FormatInfo {
98103
/// FourCC of format (e.g. `b"H264"`).
99104
pub format: [u8; 4],
@@ -143,6 +148,7 @@ impl fmt::Debug for FormatInfo {
143148
}
144149
}
145150

151+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
146152
pub enum ResolutionInfo {
147153
Discretes(Vec<(u32, u32)>),
148154
Stepwise {
@@ -173,6 +179,7 @@ impl fmt::Debug for ResolutionInfo {
173179
}
174180
}
175181

182+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
176183
pub enum IntervalInfo {
177184
Discretes(Vec<(u32, u32)>),
178185
Stepwise {
@@ -242,18 +249,21 @@ impl Drop for Frame {
242249
}
243250

244251
#[derive(Debug, PartialEq)]
252+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
245253
enum State {
246254
Idle,
247255
Streaming,
248256
Aborted,
249257
}
250258

251259
#[derive(Debug)]
260+
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
252261
pub struct Camera {
253262
fd: RawFd,
254263
state: State,
255264
resolution: (u32, u32),
256265
format: [u8; 4],
266+
#[cfg_attr(feature = "serde", serde(skip_serializing))]
257267
buffers: Vec<Arc<MappedRegion>>,
258268
}
259269

@@ -661,6 +671,7 @@ impl Drop for Camera {
661671
}
662672

663673
#[derive(Debug)]
674+
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
664675
pub struct FormatIter<'a> {
665676
camera: &'a Camera,
666677
index: u32,
@@ -752,6 +763,7 @@ impl Settable for String {
752763
}
753764

754765
#[derive(Debug, Clone)]
766+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
755767
pub struct Control {
756768
pub id: u32,
757769
pub name: String,
@@ -761,6 +773,7 @@ pub struct Control {
761773
}
762774

763775
#[derive(Debug, Clone)]
776+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
764777
pub enum CtrlData {
765778
Integer {
766779
value: i32,
@@ -807,12 +820,14 @@ pub enum CtrlData {
807820
}
808821

809822
#[derive(Debug, Clone)]
823+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
810824
pub struct CtrlMenuItem {
811825
pub index: u32,
812826
pub name: String,
813827
}
814828

815829
#[derive(Debug, Clone)]
830+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
816831
pub struct CtrlIntMenuItem {
817832
pub index: u32,
818833
pub value: i64,

src/v4l2.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub struct Format {
140140
#[cfg(target_pointer_width = "64")]
141141
padding: u32,
142142
pub fmt: PixFormat,
143-
#[derivative(Debug="ignore")]
143+
#[derivative(Debug = "ignore")]
144144
space: [u8; 156],
145145
}
146146

@@ -166,6 +166,7 @@ impl Format {
166166
}
167167

168168
#[derive(Debug)]
169+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
169170
#[repr(C)]
170171
pub struct PixFormat {
171172
pub width: u32,
@@ -193,6 +194,7 @@ impl PixFormat {
193194
}
194195

195196
#[derive(Debug)]
197+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
196198
#[repr(C)]
197199
pub struct RequestBuffers {
198200
pub count: u32,
@@ -221,7 +223,7 @@ pub struct Buffer {
221223
pub bytesused: u32,
222224
pub flags: u32,
223225
pub field: u32,
224-
#[derivative(Debug="ignore")]
226+
#[derivative(Debug = "ignore")]
225227
pub timestamp: Timeval,
226228
pub timecode: TimeCode,
227229
pub sequence: u32,
@@ -242,6 +244,7 @@ impl Buffer {
242244
}
243245

244246
#[derive(Debug)]
247+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
245248
#[repr(C)]
246249
pub struct TimeCode {
247250
pub ttype: u32,
@@ -254,6 +257,7 @@ pub struct TimeCode {
254257
}
255258

256259
#[derive(Debug)]
260+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
257261
#[repr(C)]
258262
pub struct FmtDesc {
259263
pub index: u32,
@@ -278,7 +282,7 @@ impl FmtDesc {
278282
pub struct StreamParm {
279283
pub ptype: u32,
280284
pub parm: CaptureParm,
281-
#[derivative(Debug="ignore")]
285+
#[derivative(Debug = "ignore")]
282286
space: [u8; 160],
283287
}
284288

@@ -293,6 +297,7 @@ impl StreamParm {
293297
}
294298

295299
#[derive(Debug)]
300+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
296301
#[repr(C)]
297302
pub struct CaptureParm {
298303
pub capability: u32,
@@ -304,13 +309,15 @@ pub struct CaptureParm {
304309
}
305310

306311
#[derive(Debug)]
312+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
307313
#[repr(C)]
308314
pub struct Fract {
309315
pub numerator: u32,
310316
pub denominator: u32,
311317
}
312318

313319
#[derive(Debug)]
320+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
314321
#[repr(C)]
315322
pub struct Frmsizeenum {
316323
pub index: u32,
@@ -337,13 +344,15 @@ impl Frmsizeenum {
337344
}
338345

339346
#[derive(Debug)]
347+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
340348
#[repr(C)]
341349
pub struct FrmsizeDiscrete {
342350
pub width: u32,
343351
pub height: u32,
344352
}
345353

346354
#[derive(Debug)]
355+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
347356
#[repr(C)]
348357
pub struct FrmsizeStepwise {
349358
pub min_width: u32,
@@ -355,6 +364,7 @@ pub struct FrmsizeStepwise {
355364
}
356365

357366
#[derive(Debug)]
367+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
358368
#[repr(C)]
359369
pub struct Frmivalenum {
360370
pub index: u32,
@@ -385,6 +395,7 @@ impl Frmivalenum {
385395
}
386396

387397
#[derive(Debug)]
398+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
388399
#[repr(C)]
389400
pub struct FrmivalStepwise {
390401
pub min: Fract,
@@ -393,6 +404,7 @@ pub struct FrmivalStepwise {
393404
}
394405

395406
#[derive(Debug)]
407+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
396408
#[repr(C)]
397409
pub struct QueryCtrl {
398410
pub id: u32,
@@ -415,6 +427,7 @@ impl QueryCtrl {
415427
}
416428

417429
#[derive(Debug)]
430+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
418431
#[repr(C)]
419432
pub struct QueryExtCtrl {
420433
pub id: u32,
@@ -442,11 +455,15 @@ impl QueryExtCtrl {
442455
}
443456
}
444457

445-
#[derive(Debug, Copy, Clone)]
458+
#[derive(Derivative, Debug, Copy, Clone)]
459+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
446460
#[repr(C, packed)]
447461
pub struct QueryMenu {
448462
pub id: u32,
449463
pub index: u32,
464+
//#[derivative(Debug="ignore")]
465+
#[serde(skip_serializing)]
466+
#[serde(skip_deserializing)]
450467
pub data: QueryMenuData,
451468
reserved: u32,
452469
}
@@ -458,6 +475,12 @@ pub union QueryMenuData {
458475
value: i64,
459476
}
460477

478+
impl Default for QueryMenuData {
479+
fn default() -> Self {
480+
QueryMenuData { value: 0 }
481+
}
482+
}
483+
461484
impl fmt::Debug for QueryMenuData {
462485
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
463486
write!(f, "{:?}", self.name())
@@ -483,6 +506,7 @@ impl QueryMenuData {
483506
}
484507

485508
#[derive(Debug)]
509+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
486510
#[repr(C)]
487511
pub struct Control {
488512
pub id: u32,
@@ -496,6 +520,7 @@ impl Control {
496520
}
497521

498522
#[derive(Debug, Copy, Clone)]
523+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
499524
#[repr(C, packed)]
500525
pub struct ExtControl {
501526
pub id: u32,
@@ -516,6 +541,7 @@ impl ExtControl {
516541
}
517542

518543
#[derive(Debug)]
544+
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
519545
#[repr(C)]
520546
pub struct ExtControls<'a> {
521547
pub ctrl_class: u32,

0 commit comments

Comments
 (0)