oxideav-core 0.1.6

Core types and registries for oxideav — timestamps, packets, frames, codec/container/source/filter registries (pure Rust, no C deps)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Generic, schema-validated option bag for codec (and container) init.
//!
//! The over-the-wire form is an untyped string→string bag
//! ([`CodecOptions`]). Each codec defines a typed struct implementing
//! [`CodecOptionsStruct`], which declares a static [`OptionField`]
//! schema and an [`apply`](CodecOptionsStruct::apply) method that
//! writes one coerced value into the struct. [`parse_options`] drives
//! the whole thing: it walks the bag, looks up every key in the
//! schema, coerces the string to the declared [`OptionKind`], and
//! hands the resulting [`OptionValue`] to `apply`.
//!
//! Strict at init: unknown keys and malformed values return
//! [`Error::InvalidData`]. Consumers that want "ignore unknown keys"
//! should pre-filter the bag before calling [`parse_options`].
//!
//! All parsing happens once, at encoder/decoder construction — the
//! hot path never touches this module.
//!
//! Consumers have two entry points:
//! - **Dynamic / JSON** — build a [`CodecOptions`] via `.set(k, v)` or
//!   [`CodecOptions::from_json`] (feature `json-options`) and attach
//!   it to `CodecParameters::options`.
//! - **Typed** — skip the bag entirely: build the codec's options
//!   struct directly and pass it to a codec-specific typed entry point
//!   (e.g. `encode_single_with_options`). The bag only exists for
//!   consumers who can't know the typed struct at compile time.

use crate::error::{Error, Result};

/// Untyped string → string bag. The over-the-wire shape of options
/// as they travel from the caller (CLI / pipeline JSON / FFI) to a
/// codec factory.
///
/// Insertion order is preserved and [`iter`](Self::iter) walks keys in
/// the order they were set. Duplicate keys overwrite (last writer
/// wins).
#[derive(Debug, Clone, Default)]
pub struct CodecOptions {
    entries: Vec<(String, String)>,
}

impl CodecOptions {
    pub fn new() -> Self {
        Self::default()
    }

    /// Builder-style setter, useful for one-liners.
    /// `CodecOptions::new().set("interlace", "true")`.
    pub fn set(mut self, k: impl Into<String>, v: impl Into<String>) -> Self {
        self.insert(k, v);
        self
    }

    /// Mutating insert. Overwrites any existing entry with the same
    /// key.
    pub fn insert(&mut self, k: impl Into<String>, v: impl Into<String>) {
        let k = k.into();
        let v = v.into();
        if let Some(existing) = self.entries.iter_mut().find(|(kk, _)| kk == &k) {
            existing.1 = v;
        } else {
            self.entries.push((k, v));
        }
    }

    pub fn get(&self, k: &str) -> Option<&str> {
        self.entries
            .iter()
            .find(|(kk, _)| kk == k)
            .map(|(_, v)| v.as_str())
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    pub fn len(&self) -> usize {
        self.entries.len()
    }

    pub fn iter(&self) -> impl Iterator<Item = (&str, &str)> {
        self.entries.iter().map(|(k, v)| (k.as_str(), v.as_str()))
    }

    /// Build a bag from a JSON object. Scalar values (bool / number /
    /// string) are stringified into the bag; arrays and nested objects
    /// are rejected — keys with structured values don't map into the
    /// flat string bag.
    pub fn from_json(s: &str) -> Result<Self> {
        let v: serde_json::Value =
            serde_json::from_str(s).map_err(|e| Error::invalid(format!("options json: {e}")))?;
        Self::from_json_value(&v)
    }

    /// As [`from_json`](Self::from_json) but takes a pre-parsed value
    /// (the shape pipelines already use — `TrackSpec.codec_params`).
    pub fn from_json_value(v: &serde_json::Value) -> Result<Self> {
        use serde_json::Value;
        let obj = match v {
            Value::Null => return Ok(Self::default()),
            Value::Object(m) => m,
            other => {
                return Err(Error::invalid(format!(
                    "options json: expected object, got {}",
                    json_type_name(other)
                )))
            }
        };
        let mut out = Self::default();
        for (k, val) in obj {
            let s = match val {
                Value::Bool(b) => b.to_string(),
                Value::Number(n) => n.to_string(),
                Value::String(s) => s.clone(),
                Value::Null => continue, // null = "leave default"
                other => {
                    return Err(Error::invalid(format!(
                        "option '{k}': structured values ({}) are not supported",
                        json_type_name(other)
                    )))
                }
            };
            out.insert(k.clone(), s);
        }
        Ok(out)
    }
}

fn json_type_name(v: &serde_json::Value) -> &'static str {
    use serde_json::Value;
    match v {
        Value::Null => "null",
        Value::Bool(_) => "bool",
        Value::Number(_) => "number",
        Value::String(_) => "string",
        Value::Array(_) => "array",
        Value::Object(_) => "object",
    }
}

/// Declared type of a single option. Used at parse time to coerce a
/// raw string (or JSON scalar) into a typed [`OptionValue`] and to
/// reject malformed values up front.
#[derive(Clone, Copy, Debug)]
pub enum OptionKind {
    Bool,
    U32,
    I32,
    F32,
    String,
    /// Enumeration: the only accepted values are the strings in this
    /// slice. Matching is case-sensitive.
    Enum(&'static [&'static str]),
}

/// Coerced value handed to a codec's `apply` method. Codec code
/// chooses the appropriate `as_*` accessor based on the field name.
#[derive(Clone, Debug)]
pub enum OptionValue {
    Bool(bool),
    U32(u32),
    I32(i32),
    F32(f32),
    String(String),
}

impl OptionValue {
    pub fn as_bool(&self) -> Result<bool> {
        match self {
            OptionValue::Bool(b) => Ok(*b),
            other => Err(Error::invalid(format!("expected bool, got {other:?}"))),
        }
    }
    pub fn as_u32(&self) -> Result<u32> {
        match self {
            OptionValue::U32(n) => Ok(*n),
            other => Err(Error::invalid(format!("expected u32, got {other:?}"))),
        }
    }
    pub fn as_i32(&self) -> Result<i32> {
        match self {
            OptionValue::I32(n) => Ok(*n),
            other => Err(Error::invalid(format!("expected i32, got {other:?}"))),
        }
    }
    pub fn as_f32(&self) -> Result<f32> {
        match self {
            OptionValue::F32(n) => Ok(*n),
            other => Err(Error::invalid(format!("expected f32, got {other:?}"))),
        }
    }
    pub fn as_str(&self) -> Result<&str> {
        match self {
            OptionValue::String(s) => Ok(s.as_str()),
            other => Err(Error::invalid(format!("expected string, got {other:?}"))),
        }
    }
}

/// Schema entry describing one recognised option. Codec crates declare
/// a `&'static [OptionField]` listing every key their options struct
/// consumes.
#[derive(Debug)]
pub struct OptionField {
    pub name: &'static str,
    pub kind: OptionKind,
    pub default: OptionValue,
    pub help: &'static str,
}

/// Trait implemented by each codec's typed options struct.
///
/// Typical hand-written implementation:
///
/// ```ignore
/// impl CodecOptionsStruct for PngEncoderOptions {
///     const SCHEMA: &'static [OptionField] = &[
///         OptionField {
///             name: "interlace",
///             kind: OptionKind::Bool,
///             default: OptionValue::Bool(false),
///             help: "Adam7 interlaced encode",
///         },
///     ];
///     fn apply(&mut self, key: &str, v: &OptionValue) -> Result<()> {
///         match key {
///             "interlace" => self.interlace = v.as_bool()?,
///             _ => unreachable!("guarded by SCHEMA"),
///         }
///         Ok(())
///     }
/// }
/// ```
pub trait CodecOptionsStruct: Default + 'static {
    const SCHEMA: &'static [OptionField];
    fn apply(&mut self, key: &str, value: &OptionValue) -> Result<()>;
}

/// Parse a [`CodecOptions`] bag into a typed options struct.
///
/// Strict: unknown keys return [`Error::InvalidData`]; malformed values
/// do the same. The returned struct is seeded from
/// `T::default()` — any key not set in the bag keeps the struct's
/// default value.
pub fn parse_options<T: CodecOptionsStruct>(opts: &CodecOptions) -> Result<T> {
    let mut out = T::default();
    for (k, v_str) in opts.iter() {
        let field = T::SCHEMA
            .iter()
            .find(|f| f.name == k)
            .ok_or_else(|| Error::invalid(format!("unknown option '{k}'")))?;
        let v = coerce(k, field.kind, v_str)?;
        out.apply(k, &v)?;
    }
    Ok(out)
}

/// Shorthand: parse straight from a JSON-object source.
pub fn parse_options_json<T: CodecOptionsStruct>(s: &str) -> Result<T> {
    parse_options::<T>(&CodecOptions::from_json(s)?)
}

fn coerce(name: &str, kind: OptionKind, raw: &str) -> Result<OptionValue> {
    match kind {
        OptionKind::Bool => match raw {
            "true" | "1" | "yes" | "on" => Ok(OptionValue::Bool(true)),
            "false" | "0" | "no" | "off" => Ok(OptionValue::Bool(false)),
            other => Err(Error::invalid(format!(
                "option '{name}' expects bool, got {other:?}"
            ))),
        },
        OptionKind::U32 => raw
            .parse::<u32>()
            .map(OptionValue::U32)
            .map_err(|_| Error::invalid(format!("option '{name}' expects u32, got {raw:?}"))),
        OptionKind::I32 => raw
            .parse::<i32>()
            .map(OptionValue::I32)
            .map_err(|_| Error::invalid(format!("option '{name}' expects i32, got {raw:?}"))),
        OptionKind::F32 => raw
            .parse::<f32>()
            .map(OptionValue::F32)
            .map_err(|_| Error::invalid(format!("option '{name}' expects f32, got {raw:?}"))),
        OptionKind::String => Ok(OptionValue::String(raw.to_owned())),
        OptionKind::Enum(allowed) => {
            if allowed.contains(&raw) {
                Ok(OptionValue::String(raw.to_owned()))
            } else {
                Err(Error::invalid(format!(
                    "option '{name}' must be one of {:?}, got {raw:?}",
                    allowed
                )))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Default, Debug, PartialEq)]
    struct Demo {
        interlace: bool,
        level: u32,
        mode: String,
    }

    impl CodecOptionsStruct for Demo {
        const SCHEMA: &'static [OptionField] = &[
            OptionField {
                name: "interlace",
                kind: OptionKind::Bool,
                default: OptionValue::Bool(false),
                help: "",
            },
            OptionField {
                name: "level",
                kind: OptionKind::U32,
                default: OptionValue::U32(6),
                help: "",
            },
            OptionField {
                name: "mode",
                kind: OptionKind::Enum(&["fast", "slow"]),
                default: OptionValue::String(String::new()),
                help: "",
            },
        ];
        fn apply(&mut self, key: &str, v: &OptionValue) -> Result<()> {
            match key {
                "interlace" => self.interlace = v.as_bool()?,
                "level" => self.level = v.as_u32()?,
                "mode" => self.mode = v.as_str()?.to_owned(),
                _ => unreachable!("guarded by SCHEMA"),
            }
            Ok(())
        }
    }

    #[test]
    fn bag_preserves_order_and_overwrites() {
        let opts = CodecOptions::new()
            .set("a", "1")
            .set("b", "2")
            .set("a", "3");
        assert_eq!(opts.get("a"), Some("3"));
        let collected: Vec<_> = opts.iter().collect();
        assert_eq!(collected, vec![("a", "3"), ("b", "2")]);
    }

    #[test]
    fn parse_empty_returns_default() {
        let opts = CodecOptions::new();
        let d = parse_options::<Demo>(&opts).unwrap();
        assert_eq!(d, Demo::default());
    }

    #[test]
    fn parse_typed_values() {
        let opts = CodecOptions::new()
            .set("interlace", "true")
            .set("level", "9")
            .set("mode", "fast");
        let d = parse_options::<Demo>(&opts).unwrap();
        assert!(d.interlace);
        assert_eq!(d.level, 9);
        assert_eq!(d.mode, "fast");
    }

    #[test]
    fn parse_rejects_unknown_key() {
        let opts = CodecOptions::new().set("nope", "1");
        let err = parse_options::<Demo>(&opts).unwrap_err();
        assert!(matches!(err, Error::InvalidData(ref s) if s.contains("unknown option 'nope'")));
    }

    #[test]
    fn parse_rejects_bad_bool() {
        let opts = CodecOptions::new().set("interlace", "maybe");
        let err = parse_options::<Demo>(&opts).unwrap_err();
        assert!(matches!(err, Error::InvalidData(ref s) if s.contains("expects bool")));
    }

    #[test]
    fn parse_rejects_bad_u32() {
        let opts = CodecOptions::new().set("level", "-1");
        assert!(parse_options::<Demo>(&opts).is_err());
    }

    #[test]
    fn parse_rejects_enum_miss() {
        let opts = CodecOptions::new().set("mode", "medium");
        let err = parse_options::<Demo>(&opts).unwrap_err();
        assert!(matches!(err, Error::InvalidData(ref s) if s.contains("must be one of")));
    }

    #[test]
    fn bool_accepts_common_synonyms() {
        for (raw, want) in [
            ("true", true),
            ("1", true),
            ("yes", true),
            ("on", true),
            ("false", false),
            ("0", false),
            ("no", false),
            ("off", false),
        ] {
            let opts = CodecOptions::new().set("interlace", raw);
            let d = parse_options::<Demo>(&opts).unwrap();
            assert_eq!(d.interlace, want, "raw = {raw}");
        }
    }

    #[test]
    fn from_json_object() {
        let bag =
            CodecOptions::from_json(r#"{"interlace": true, "level": 9, "mode": "fast"}"#).unwrap();
        let d = parse_options::<Demo>(&bag).unwrap();
        assert!(d.interlace);
        assert_eq!(d.level, 9);
        assert_eq!(d.mode, "fast");
    }

    #[test]
    fn from_json_null_is_empty() {
        let bag = CodecOptions::from_json("null").unwrap();
        assert!(bag.is_empty());
    }

    #[test]
    fn from_json_rejects_nested() {
        let err = CodecOptions::from_json(r#"{"k": [1, 2]}"#).unwrap_err();
        assert!(matches!(err, Error::InvalidData(ref s) if s.contains("structured")));
    }

    #[test]
    fn parse_options_json_shortcut() {
        let d = parse_options_json::<Demo>(r#"{"level": 3}"#).unwrap();
        assert_eq!(d.level, 3);
    }
}