Info

Struct Info 

Source
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: Object

keys whose values vary with the object being requested

§request: Request

keys whose values vary with each request

§session: Session

keys whose values are expected to be invariant over the life of the session

§status: Status

keys 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

Source

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();
Source

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();
Source

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();
Source

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();
Source

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();
Source

pub fn to_query_string(&self) -> String

Encodes Info into an URL encoded Query String.

§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_query_string();
Source

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);
Source

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();
Source

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();

Trait Implementations§

Source§

impl Debug for Info

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Info

Source§

fn default() -> Info

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Info

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Info

Source§

fn eq(&self, other: &Info) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Info

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Info

Auto Trait Implementations§

§

impl Freeze for Info

§

impl RefUnwindSafe for Info

§

impl Send for Info

§

impl Sync for Info

§

impl Unpin for Info

§

impl UnwindSafe for Info

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Serialize for T
where T: Serialize + ?Sized,

Source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>

Source§

fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,