moqtap_codec/fields/
params.rs1use crate::fields::FieldValue as Value;
2use crate::kvp::{KeyValuePair, KvpValue};
3use crate::varint::VarInt;
4
5#[cfg(feature = "draft07")]
7fn d07_setup_param_name(key: u64) -> Option<&'static str> {
8 match key {
9 0x00 => Some("role"),
10 0x01 => Some("path"),
11 0x02 => Some("max_subscribe_id"),
12 _ => None,
13 }
14}
15
16fn d07_message_param_name(key: u64) -> Option<&'static str> {
18 match key {
19 0x02 => Some("authorization_info"),
20 0x03 => Some("delivery_timeout"),
21 0x04 => Some("max_cache_duration"),
22 _ => None,
23 }
24}
25
26#[cfg(feature = "draft07")]
28fn d07_setup_is_varint(key: u64) -> bool {
29 matches!(key, 0x00 | 0x02) }
31
32#[cfg(any(feature = "draft08", feature = "draft09", feature = "draft10"))]
40fn d08_setup_param_name(key: u64) -> Option<&'static str> {
41 match key {
42 0x01 => Some("path"),
43 0x02 => Some("max_subscribe_id"),
44 _ => None,
45 }
46}
47
48#[cfg(any(feature = "draft08", feature = "draft09", feature = "draft10"))]
50fn d08_setup_is_varint(key: u64) -> bool {
51 key == 0x02 }
53
54fn d07_msg_is_varint(key: u64) -> bool {
56 matches!(key, 0x03 | 0x04) }
58
59fn kvp_to_json_d07_inner(
62 params: &[KeyValuePair],
63 name_fn: fn(u64) -> Option<&'static str>,
64 is_varint_fn: fn(u64) -> bool,
65) -> Value {
66 crate::fields::kvp_entries(params, |key, value| {
67 let Some(name) = name_fn(key) else {
68 return (None, None);
69 };
70 let rendered = match value {
71 KvpValue::Bytes(b) if is_varint_fn(key) => {
72 match VarInt::decode(&mut &b[..]) {
84 Ok(v) => Some(Value::Uint(v.into_inner())),
85 Err(_) => None,
86 }
87 }
88 KvpValue::Bytes(b) => Some(Value::Text(String::from_utf8_lossy(b).into_owned())),
89 KvpValue::Varint(v) => Some(Value::Uint(v.into_inner())),
90 };
91 (Some(name), rendered)
92 })
93}
94
95#[cfg(feature = "draft07")]
96pub fn kvp_to_json_d07_setup(params: &[KeyValuePair]) -> Value {
97 kvp_to_json_d07_inner(params, d07_setup_param_name, d07_setup_is_varint)
98}
99
100pub fn kvp_to_json_d07(params: &[KeyValuePair]) -> Value {
101 kvp_to_json_d07_inner(params, d07_message_param_name, d07_msg_is_varint)
102}
103
104#[cfg(any(feature = "draft08", feature = "draft09", feature = "draft10"))]
106pub fn kvp_to_json_d08_setup(params: &[KeyValuePair]) -> Value {
107 kvp_to_json_d07_inner(params, d08_setup_param_name, d08_setup_is_varint)
108}