Crate cmcd

Crate cmcd 

Source
Expand description

§Common Media Client Data (CMCD – CTA-5004)

This cmcd crate provides functionality and types to read CMCD data from HTTP Header, Query Strings and JSON payloads, as specified by CTA-5004.

All you really need to checkout is the Info type. The following examples are taken from its documentation.

§Example 1: Header Map

For now HTTP Headers are expected to be Key-Value String pairs. Check out the Planned Features if that may not satisfy your needs right now.

let header: HashMap<String, String> = HashMap::from([
    ("cmcd-object".into(), "br=123,d=456,ot=i".into()),
    ("CMCD-Session".into(), r#"sid="6e2fb550-c457-11e9-bb97-0800200c9a66",st=l,pr=1.5"#.into()),
    ("Cmcd-Status".into(), "custom-field=1.69"),
]);

let info = Info::from_header_map(&header).unwrap();

§Example 2: Query String

Query parsing requires you to extract the query value from the query key CMCD and then pass it to either Info::from_query_string if the string is URL encoded or to Info::from_raw_query_string if it wasn’t encoded.

let query: String =
    String::from(r#"br=123,custom-field=1.69,d=456,pr=1.5,ot=i,sid="6e2fb550-c457-11e9-bb97-0800200c9a66",st=l"#);

let info = Info::from_raw_query_string(&query).unwrap();
// or with an URL encoded query
let encoded = urlencoding::encode(&query);
let info = Info::from_query_string(&encoded).unwrap();

§Example 3: JSON

JSON payloads can be parsed from either string with Info::from_json or from bytes with Info::from_json_bytes.

let json = serde_json::json!(
    {
        "br": 123,
        "d": 456,
        "pr": 1.5,
        "ot": "i",
        "sid": "6e2fb550-c457-11e9-bb97-0800200c9a66",
        "st": "l",
        "custom-field": 1.69
    }
)
.to_string();

let info = Info::from_json(&json).unwrap();

§Planned Features

Here is a list of additions I intend to add to this crate in the future:

  • Support for specific HTTP framework type, like
    • axum
    • pingora
    • others if demand is there
    • each crate would be locked behind a feature, so you would be able to exclude those you have no need for
  • Provide a builder pattern for creating new Info data

I am open to any other feature request and naturally contributions to the code base.

Modules§

error
Error types when parsing Info from Header, JSON or Query
header
Predefined CTA-5004 HTTP Header Keys.
key
Predefined CTA-5004 Value Keys
values
Definitions of the data structures of CMCD

Structs§

Info
Common Media Client Data

Traits§

Encoder
Encoder is used by Object, Request, Session and Status to encode their data.