Skip to content

Commit 83c644c

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

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ readme = "README.md"
1212
[dependencies]
1313
derivative = "1.0.3"
1414
libc = "0.2"
15+
serde_derive = { version = "1.0.104", optional = true }
1516

1617
[features]
1718
no_wrapper = []
1819
static = []
20+
default = ["serde_derive"]

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ pub use v4l2::pubconsts as consts;
4949

5050
pub type Result<T> = result::Result<T, Error>;
5151

52+
#[cfg(feature = "serde")]
53+
use serde::Serialize;
54+
5255
#[macro_use]
5356
extern crate derivative;
5457

@@ -107,6 +110,7 @@ impl From<io::Error> for Error {
107110
}
108111

109112
#[derive(Debug)]
113+
#[cfg_attr(feature = "serde", derive(Serialize))]
110114
pub struct Config<'a> {
111115
/// The mix of numerator and denominator. v4l2 uses frame intervals instead of frame rates.
112116
/// Default is `(1, 10)`.
@@ -138,6 +142,7 @@ impl<'a> Default for Config<'a> {
138142
}
139143
}
140144

145+
#[cfg_attr(feature = "serde", derive(Serialize))]
141146
pub struct FormatInfo {
142147
/// FourCC of format (e.g. `b"H264"`).
143148
pub format: [u8; 4],
@@ -250,6 +255,7 @@ impl fmt::Debug for IntervalInfo {
250255
}
251256

252257
#[derive(Debug)]
258+
#[cfg_attr(feature = "serde", derive(Serialize))]
253259
pub struct Frame {
254260
/// Width and height of the frame.
255261
pub resolution: (u32, u32),
@@ -293,6 +299,7 @@ enum State {
293299
}
294300

295301
#[derive(Debug)]
302+
#[cfg_attr(feature = "serde", derive(Serialize))]
296303
pub struct Camera {
297304
fd: RawFd,
298305
state: State,
@@ -705,6 +712,7 @@ impl Drop for Camera {
705712
}
706713

707714
#[derive(Debug)]
715+
#[cfg_attr(feature = "serde", derive(Serialize))]
708716
pub struct FormatIter<'a> {
709717
camera: &'a Camera,
710718
index: u32,
@@ -733,6 +741,7 @@ impl<'a> Iterator for FormatIter<'a> {
733741
}
734742

735743
#[derive(Debug)]
744+
#[cfg_attr(feature = "serde", derive(Serialize))]
736745
pub struct ControlIter<'a> {
737746
camera: &'a Camera,
738747
id: u32,
@@ -796,6 +805,7 @@ impl Settable for String {
796805
}
797806

798807
#[derive(Debug, Clone)]
808+
#[cfg_attr(feature = "serde", derive(Serialize))]
799809
pub struct Control {
800810
pub id: u32,
801811
pub name: String,
@@ -805,6 +815,7 @@ pub struct Control {
805815
}
806816

807817
#[derive(Debug, Clone)]
818+
#[cfg_attr(feature = "serde", derive(Serialize))]
808819
pub enum CtrlData {
809820
Integer {
810821
value: i32,
@@ -851,12 +862,14 @@ pub enum CtrlData {
851862
}
852863

853864
#[derive(Debug, Clone)]
865+
#[cfg_attr(feature = "serde", derive(Serialize))]
854866
pub struct CtrlMenuItem {
855867
pub index: u32,
856868
pub name: String,
857869
}
858870

859871
#[derive(Debug, Clone)]
872+
#[cfg_attr(feature = "serde", derive(Serialize))]
860873
pub struct CtrlIntMenuItem {
861874
pub index: u32,
862875
pub value: i64,

src/v4l2.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ pub fn mmap(length: usize, fd: RawFd, offset: usize) -> io::Result<MappedRegion>
135135

136136
#[derive(Derivative)]
137137
#[derivative(Debug)]
138+
#[cfg_attr(feature = "serde", derive(Serialize))]
138139
#[repr(C)]
139140
pub struct Format {
140141
pub ftype: u32,
@@ -167,6 +168,7 @@ impl Format {
167168
}
168169

169170
#[derive(Debug)]
171+
#[cfg_attr(feature = "serde", derive(Serialize))]
170172
#[repr(C)]
171173
pub struct PixFormat {
172174
pub width: u32,
@@ -194,6 +196,7 @@ impl PixFormat {
194196
}
195197

196198
#[derive(Debug)]
199+
#[cfg_attr(feature = "serde", derive(Serialize))]
197200
#[repr(C)]
198201
pub struct RequestBuffers {
199202
pub count: u32,
@@ -215,6 +218,7 @@ impl RequestBuffers {
215218

216219
#[derive(Derivative)]
217220
#[derivative(Debug)]
221+
#[cfg_attr(feature = "serde", derive(Serialize))]
218222
#[repr(C)]
219223
pub struct Buffer {
220224
pub index: u32,
@@ -243,6 +247,7 @@ impl Buffer {
243247
}
244248

245249
#[derive(Debug)]
250+
#[cfg_attr(feature = "serde", derive(Serialize))]
246251
#[repr(C)]
247252
pub struct TimeCode {
248253
pub ttype: u32,
@@ -255,6 +260,7 @@ pub struct TimeCode {
255260
}
256261

257262
#[derive(Debug)]
263+
#[cfg_attr(feature = "serde", derive(Serialize))]
258264
#[repr(C)]
259265
pub struct FmtDesc {
260266
pub index: u32,
@@ -275,6 +281,7 @@ impl FmtDesc {
275281

276282
#[derive(Derivative)]
277283
#[derivative(Debug)]
284+
#[cfg_attr(feature = "serde", derive(Serialize))]
278285
#[repr(C)]
279286
pub struct StreamParm {
280287
pub ptype: u32,
@@ -294,6 +301,7 @@ impl StreamParm {
294301
}
295302

296303
#[derive(Debug)]
304+
#[cfg_attr(feature = "serde", derive(Serialize))]
297305
#[repr(C)]
298306
pub struct CaptureParm {
299307
pub capability: u32,
@@ -305,13 +313,15 @@ pub struct CaptureParm {
305313
}
306314

307315
#[derive(Debug)]
316+
#[cfg_attr(feature = "serde", derive(Serialize))]
308317
#[repr(C)]
309318
pub struct Fract {
310319
pub numerator: u32,
311320
pub denominator: u32,
312321
}
313322

314323
#[derive(Debug)]
324+
#[cfg_attr(feature = "serde", derive(Serialize))]
315325
#[repr(C)]
316326
pub struct Frmsizeenum {
317327
pub index: u32,
@@ -338,13 +348,15 @@ impl Frmsizeenum {
338348
}
339349

340350
#[derive(Debug)]
351+
#[cfg_attr(feature = "serde", derive(Serialize))]
341352
#[repr(C)]
342353
pub struct FrmsizeDiscrete {
343354
pub width: u32,
344355
pub height: u32,
345356
}
346357

347358
#[derive(Debug)]
359+
#[cfg_attr(feature = "serde", derive(Serialize))]
348360
#[repr(C)]
349361
pub struct FrmsizeStepwise {
350362
pub min_width: u32,
@@ -356,6 +368,7 @@ pub struct FrmsizeStepwise {
356368
}
357369

358370
#[derive(Debug)]
371+
#[cfg_attr(feature = "serde", derive(Serialize))]
359372
#[repr(C)]
360373
pub struct Frmivalenum {
361374
pub index: u32,
@@ -386,6 +399,7 @@ impl Frmivalenum {
386399
}
387400

388401
#[derive(Debug)]
402+
#[cfg_attr(feature = "serde", derive(Serialize))]
389403
#[repr(C)]
390404
pub struct FrmivalStepwise {
391405
pub min: Fract,
@@ -394,6 +408,7 @@ pub struct FrmivalStepwise {
394408
}
395409

396410
#[derive(Debug)]
411+
#[cfg_attr(feature = "serde", derive(Serialize))]
397412
#[repr(C)]
398413
pub struct QueryCtrl {
399414
pub id: u32,
@@ -416,6 +431,7 @@ impl QueryCtrl {
416431
}
417432

418433
#[derive(Debug)]
434+
#[cfg_attr(feature = "serde", derive(Serialize))]
419435
#[repr(C)]
420436
pub struct QueryExtCtrl {
421437
pub id: u32,
@@ -444,6 +460,7 @@ impl QueryExtCtrl {
444460
}
445461

446462
#[derive(Debug)]
463+
#[cfg_attr(feature = "serde", derive(Serialize))]
447464
#[repr(C, packed)]
448465
pub struct QueryMenu {
449466
pub id: u32,
@@ -483,6 +500,7 @@ impl QueryMenuData {
483500
}
484501

485502
#[derive(Debug)]
503+
#[cfg_attr(feature = "serde", derive(Serialize))]
486504
#[repr(C)]
487505
pub struct Control {
488506
pub id: u32,
@@ -496,6 +514,7 @@ impl Control {
496514
}
497515

498516
#[derive(Debug)]
517+
#[cfg_attr(feature = "serde", derive(Serialize))]
499518
#[repr(C, packed)]
500519
pub struct ExtControl {
501520
pub id: u32,
@@ -516,6 +535,7 @@ impl ExtControl {
516535
}
517536

518537
#[derive(Debug)]
538+
#[cfg_attr(feature = "serde", derive(Serialize))]
519539
#[repr(C)]
520540
pub struct ExtControls<'a> {
521541
pub ctrl_class: u32,

0 commit comments

Comments
 (0)