pub struct Info {
pub object: Object,
pub request: Request,
pub session: Session,
pub status: Status,
pub custom: HashMap<String, Value>,
}Expand description
§Common Media Client Data
as defined by CTA-5004
§Example 1: Header Map
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
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
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();Fields§
§object: Objectkeys whose values vary with the object being requested
request: Requestkeys whose values vary with each request
session: Sessionkeys whose values are expected to be invariant over the life of the session
status: Statuskeys whose values do not vary with every request or object
custom: HashMap<String, Value>additional custom keys which are not reserved by CTA-5004
Implementations§
Source§impl Info
impl Info
Sourcepub fn from_query_string(qs: &str) -> Result<Self, QueryStringParseError>
pub fn from_query_string(qs: &str) -> Result<Self, QueryStringParseError>
Parses Info from an URL encoded query string qs.
The actual Query key is CMCD you will need to retrieve
the value of the said key and pass it into this
function.
Returns the parsed CMCD data.
Errors occur either when the query string was incorrectly URL encoded or when a specific value was invalid.
§Example
let query = urlencoding::encode(
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_query_string(&query).unwrap();Sourcepub fn from_raw_query_string(qs: &str) -> Result<Self, QueryStringParseError>
pub fn from_raw_query_string(qs: &str) -> Result<Self, QueryStringParseError>
Parses Info from an query string qs.
Works the same as Info::from_query_string, except it skips the URL decoded step and expects a decoded string directly.
§Example
let query =
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();Sourcepub fn from_header_map(
headers: &HashMap<String, String>,
) -> Result<Self, HeaderParseError>
pub fn from_header_map( headers: &HashMap<String, String>, ) -> Result<Self, HeaderParseError>
Parses Info from a Header Map headers.
Returns the parsed CMCD data.
Errors occur either when a Header a key that belongs to a different header or when a value is invalid.
§Example
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();Sourcepub fn from_json(s: &str) -> Result<Self, JsonParseError>
pub fn from_json(s: &str) -> Result<Self, JsonParseError>
Prase Info from JSON string using serde_json.
§Example
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();Sourcepub fn from_json_bytes(buf: &[u8]) -> Result<Self, JsonParseError>
pub fn from_json_bytes(buf: &[u8]) -> Result<Self, JsonParseError>
Prase Info from JSON bytes using serde_json.
§Example
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 json = json.as_bytes();
let info = Info::from_json(&json).unwrap();Sourcepub fn to_query_string(&self) -> String
pub fn to_query_string(&self) -> String
Sourcepub fn to_header_map(
&self,
custom: Option<HashMap<Header, Vec<String>>>,
) -> HashMap<String, String>
pub fn to_header_map( &self, custom: Option<HashMap<Header, Vec<String>>>, ) -> HashMap<String, String>
Encodes Info into Header Map.
Optionally allows assigning custom fields to specific headers. Otherwise sorts all custom fields to the CMCD-Session header.
§Example
let info = Info {
session: Session {
session_id: Some("6e2fb550-c457-11e9-bb97-0800200c9a66".to_string()),
stream_type: Some(StreamType::OverTime),
..Default::default()
},
custom: HashMap::from([
("custom-field".into(), Value::Boolean(true)),
("other-value".into(), Value::Number(123)),
("whatever-data".into(), Value::String("chicken".into())),
]),
..Default::default()
}
// This will sort the custom values `custom-field` and `other-value` into
// the CMCD-Status header and `whatever-data` is unspecified, so it'll
// default to the CMCD-Session header.
let header = info.to_header_map(Some(
HashMap::from([
("cmcd-status".into(), vec!["custom-field".into(), "other-value".into()]),
])
));
// or assign all to CMCD_Session by default
let header = info.to_header_map(None);Sourcepub fn to_json(&self) -> Result<String, SerdeJsonError>
pub fn to_json(&self) -> Result<String, SerdeJsonError>
Encodes Info into a JSON String using serde_json.
§Example
let info = Info {
session: Session {
session_id: Some("6e2fb550-c457-11e9-bb97-0800200c9a66".to_string()),
stream_type: Some(StreamType::OverTime),
..Default::default()
},
..Default::default()
}
let query = info.to_json();Sourcepub fn to_json_bytes(&self) -> Result<Vec<u8>, SerdeJsonError>
pub fn to_json_bytes(&self) -> Result<Vec<u8>, SerdeJsonError>
Encodes Info into JSON bytes using serde_json.
§Example
let info = Info {
session: Session {
session_id: Some("6e2fb550-c457-11e9-bb97-0800200c9a66".to_string()),
stream_type: Some(StreamType::OverTime),
..Default::default()
},
..Default::default()
}
let query = info.to_json_bytes();