Skip to content

Commit 3c4d909

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

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
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: 15 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::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::Serialize))]
146152
pub enum ResolutionInfo {
147153
Discretes(Vec<(u32, u32)>),
148154
Stepwise {
@@ -242,18 +248,21 @@ impl Drop for Frame {
242248
}
243249

244250
#[derive(Debug, PartialEq)]
251+
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
245252
enum State {
246253
Idle,
247254
Streaming,
248255
Aborted,
249256
}
250257

251258
#[derive(Debug)]
259+
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
252260
pub struct Camera {
253261
fd: RawFd,
254262
state: State,
255263
resolution: (u32, u32),
256264
format: [u8; 4],
265+
#[cfg_attr(feature = "serde", serde(skip_serializing))]
257266
buffers: Vec<Arc<MappedRegion>>,
258267
}
259268

@@ -661,6 +670,7 @@ impl Drop for Camera {
661670
}
662671

663672
#[derive(Debug)]
673+
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
664674
pub struct FormatIter<'a> {
665675
camera: &'a Camera,
666676
index: u32,
@@ -752,6 +762,7 @@ impl Settable for String {
752762
}
753763

754764
#[derive(Debug, Clone)]
765+
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
755766
pub struct Control {
756767
pub id: u32,
757768
pub name: String,
@@ -761,6 +772,7 @@ pub struct Control {
761772
}
762773

763774
#[derive(Debug, Clone)]
775+
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
764776
pub enum CtrlData {
765777
Integer {
766778
value: i32,
@@ -807,12 +819,14 @@ pub enum CtrlData {
807819
}
808820

809821
#[derive(Debug, Clone)]
822+
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
810823
pub struct CtrlMenuItem {
811824
pub index: u32,
812825
pub name: String,
813826
}
814827

815828
#[derive(Debug, Clone)]
829+
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
816830
pub struct CtrlIntMenuItem {
817831
pub index: u32,
818832
pub value: i64,

src/v4l2.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ impl Format {
166166
}
167167

168168
#[derive(Debug)]
169+
#[cfg_attr(feature = "serde", derive(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::Serialize))]
196198
#[repr(C)]
197199
pub struct RequestBuffers {
198200
pub count: u32,
@@ -242,6 +244,7 @@ impl Buffer {
242244
}
243245

244246
#[derive(Debug)]
247+
#[cfg_attr(feature = "serde", derive(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::Serialize))]
257261
#[repr(C)]
258262
pub struct FmtDesc {
259263
pub index: u32,
@@ -293,6 +297,7 @@ impl StreamParm {
293297
}
294298

295299
#[derive(Debug)]
300+
#[cfg_attr(feature = "serde", derive(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::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::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::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::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::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::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::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::Serialize))]
418431
#[repr(C)]
419432
pub struct QueryExtCtrl {
420433
pub id: u32,
@@ -442,11 +455,14 @@ impl QueryExtCtrl {
442455
}
443456
}
444457

445-
#[derive(Debug, Copy, Clone)]
458+
#[derive(Derivative, Debug, Copy, Clone)]
459+
#[cfg_attr(feature = "serde", derive(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)]
450466
pub data: QueryMenuData,
451467
reserved: u32,
452468
}
@@ -483,6 +499,7 @@ impl QueryMenuData {
483499
}
484500

485501
#[derive(Debug)]
502+
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
486503
#[repr(C)]
487504
pub struct Control {
488505
pub id: u32,
@@ -496,6 +513,7 @@ impl Control {
496513
}
497514

498515
#[derive(Debug, Copy, Clone)]
516+
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
499517
#[repr(C, packed)]
500518
pub struct ExtControl {
501519
pub id: u32,
@@ -516,6 +534,7 @@ impl ExtControl {
516534
}
517535

518536
#[derive(Debug)]
537+
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
519538
#[repr(C)]
520539
pub struct ExtControls<'a> {
521540
pub ctrl_class: u32,

0 commit comments

Comments
 (0)