sheave_core/messages/amf/v0/object.rs
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
use std::io::Result as IOResult;
use crate::{
Decoder,
Encoder,
ByteBuffer,
messages::amf::{
ensure_marker,
v0::Marker
}
};
use super::Properties;
/// The anonymous object type of AMF.
/// This consists of pairs of string keys and any AMF data types.
///
/// * Key
///
/// The string which doesn't have its marker.
/// This type is named as `UnmarkedString` in this crate.
/// Also this occurs the panic if its length exceeds the range of 16 bits.
///
/// * Value
///
/// The pointer for AMF data types, which is wrapped into `Arc`.
/// This is because of avoiding to be deallocated its value unexpectedly.
///
/// You can access to properties which this constains, as the `HashMap`.
///
/// # Example
///
/// ```rust
/// use sheave_core::{
/// messages::amf::v0::AmfString,
/// object
/// };
///
/// let mut object = object!(
/// "app" => AmfString::from("ondemand")
/// );
/// object.get_properties().get("app");
/// &object.get_properties()["app"];
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Object(Properties);
impl Object {
/// Constrcuts a new object.
pub fn new(properties: Properties) -> Self {
Self(properties)
}
/// Gets immutable properties from this object.
pub fn get_properties(&self) -> &Properties {
&self.0
}
/// Gets mutable properties from this object.
pub fn get_properties_mut(&mut self) -> &mut Properties {
&mut self.0
}
}
impl Decoder<Object> for ByteBuffer {
/// Decodes bytes into an AMF's Object type.
///
/// # Errors
///
/// * [`InsufficientBufferLength`]
///
/// When buffer isn't remained at least 2 bytes. (non-empty object contains at least one pair of key and value)
///
/// * [`InconsistentMarker`]
///
/// When a marker byte doesn't indicate the AMF Object.
///
/// * [`InvalidString`]
///
/// When key bytes are invalid for a UTF-8 string.
///
/// # Examples
///
/// ```rust
/// use sheave_core::{
/// ByteBuffer,
/// Decoder,
/// messages::amf::v0::{
/// Marker,
/// Object
/// }
/// };
///
/// let mut buffer = ByteBuffer::default();
/// buffer.put_u8(Marker::Object as u8);
/// // AMF's Object type is required a marker of object end (0x09) which is associated with an empty key.
/// buffer.put_u16_be(0);
/// buffer.put_u8(Marker::ObjectEnd as u8);
/// assert!(Decoder::<Object>::decode(&mut buffer).is_ok());
///
/// let mut buffer = ByteBuffer::default();
/// buffer.put_u8(Marker::Number as u8);
/// buffer.put_u16_be(0);
/// buffer.put_u8(Marker::ObjectEnd as u8);
/// assert!(Decoder::<Object>::decode(&mut buffer).is_err());
///
/// // This is a missing sequence of the "sparkle heart(💖)".
/// let mut bytes = vec![0, 159, 146, 150];
/// let mut buffer = ByteBuffer::default();
/// buffer.put_u8(Marker::Object as u8);
/// buffer.put_u16_be(4);
/// buffer.put_bytes(&bytes);
/// buffer.put_u8(Marker::Number as u8);
/// buffer.put_f64(0.0);
/// buffer.put_u16_be(0);
/// buffer.put_u8(Marker::ObjectEnd as u8);
/// assert!(Decoder::<Object>::decode(&mut buffer).is_err());
///
/// let mut buffer = ByteBuffer::default();
/// assert!(Decoder::<Object>::decode(&mut buffer).is_err())
/// ```
///
/// [`InsufficientBufferLength`]: crate::byte_buffer::InsufficientBufferLength
/// [`InconsistentMarker`]: crate::messages::amf::InconsistentMarker
/// [`InvalidString`]: crate::messages::amf::InvalidString
fn decode(&mut self) -> IOResult<Object> {
self.get_u8().and_then(
|marker| ensure_marker(Marker::Object as u8, marker)
)?;
let properties: Properties = self.decode()?;
Ok(Object(properties))
}
}
impl Encoder<Object> for ByteBuffer {
/// Encodes an AMF's Object into bytes.
fn encode(&mut self, object: &Object) {
self.put_u8(Marker::Object as u8);
self.encode(&object.0);
}
}
/// Constructs an AMF's Object.
///
/// # Examples
///
/// ```rust
/// use sheave_core::{
/// // Note the macro is exported from the top of crate.
/// object,
/// messages::amf::v0::{
/// AmfString,
/// Object
/// }
/// };
///
/// let mut command_object = Object::default();
/// command_object.get_properties_mut().insert("app", AmfString::from("ondemand"));
/// command_object.get_properties_mut().insert("type", AmfString::from("nonprivate"));
/// command_object.get_properties_mut().insert("flashVer", AmfString::from("FMLE/3.0 (compatible; Lavf 60.10.100)"));
/// command_object.get_properties_mut().insert("tcUrl", AmfString::from("rtmp://localhost"));
/// assert_eq!(
/// command_object,
/// object!(
/// "app" => AmfString::from("ondemand"),
/// "type" => AmfString::from("nonprivate"),
/// "flashVer" => AmfString::from("FMLE/3.0 (compatible; Lavf 60.10.100)"),
/// "tcUrl" => AmfString::from("rtmp://localhost")
/// )
/// )
/// ```
#[macro_export]
macro_rules! object {
($($key:expr => $value:expr),*) => {
{
use $crate::messages::amf::v0::{
Object,
Properties
};
let mut properties = Properties::default();
$(properties.insert($key, $value);)*
Object::new(properties)
}
}
}
#[cfg(test)]
mod tests {
use crate::messages::amf::v0::UnmarkedString;
use super::*;
#[test]
fn decode_object() {
let mut buffer = ByteBuffer::default();
buffer.put_u8(Marker::Object as u8);
buffer.encode(&UnmarkedString::from(""));
buffer.put_u8(Marker::ObjectEnd as u8);
let result: IOResult<Object> = buffer.decode();
assert!(result.is_ok());
let actual = result.unwrap();
assert_eq!(Object::default(), actual)
}
#[test]
fn encode_object() {
let mut buffer = ByteBuffer::default();
buffer.encode(&Object::default());
let result: Vec<u8> = buffer.into();
assert_eq!(Marker::Object as u8, result[0]);
assert_eq!(&0u16.to_be_bytes(), &result[1..3]);
assert_eq!(Marker::ObjectEnd as u8, result[3])
}
}