use cookie_factory::lib::std::fmt::Formatter;
use core::fmt;
use derive_try_from_primitive::TryFromPrimitive;
use enumset::EnumSet;
use enumset::EnumSetType;
use std::cell::RefCell;
use std::rc::Rc;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq)]
pub struct Sol {
pub header: SolHeader,
pub body: Vec<SolElement>,
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(TryFromPrimitive, Eq, PartialEq, Debug, Copy, Clone)]
#[repr(u8)]
pub enum AMFVersion {
AMF0 = 0,
AMF3 = 3,
}
impl fmt::Display for AMFVersion {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
AMFVersion::AMF0 => f.write_str("AMF0"),
AMFVersion::AMF3 => f.write_str("AMF3"),
}
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq)]
pub struct SolHeader {
pub length: u32,
pub name: String,
pub format_version: AMFVersion,
}
pub type Element = Rc<RefCell<SolValue>>;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct SolElement {
pub name: String,
pub value: Element,
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub enum SolValue {
Number(f64),
Bool(bool),
String(String),
Object(Vec<SolElement>, Option<ClassDefinition>),
Null,
Undefined,
ECMAArray(Vec<Element>, Vec<SolElement>, u32),
StrictArray(Vec<Element>),
Date(f64, Option<u16>),
Unsupported,
XML(String, bool),
AMF3(Element),
Integer(i32),
ByteArray(Vec<u8>),
VectorInt(Vec<i32>, bool),
VectorUInt(Vec<u32>, bool),
VectorDouble(Vec<f64>, bool),
VectorObject(Vec<Element>, String, bool),
Dictionary(Vec<(Element, Element)>, bool),
Custom(Vec<SolElement>, Vec<SolElement>, Option<ClassDefinition>),
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct ClassDefinition {
pub name: String,
pub attributes: EnumSet<Attribute>,
pub attribute_count: u32,
pub static_properties: Vec<String>,
}
impl Default for ClassDefinition {
fn default() -> Self {
Self {
name: "Object".to_string(),
attributes: EnumSet::empty(),
attribute_count: 0,
static_properties: Vec::new(),
}
}
}
impl ClassDefinition {
pub fn default_with_name(name: String) -> Self {
Self {
name,
attributes: EnumSet::empty(),
attribute_count: 0,
static_properties: Vec::new(),
}
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(EnumSetType, Debug)]
pub enum Attribute {
DYNAMIC,
EXTERNAL,
}
pub mod amf0 {
use derive_try_from_primitive::TryFromPrimitive;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(TryFromPrimitive, Eq, PartialEq, Debug, Copy, Clone)]
#[repr(u8)]
pub enum TypeMarker {
Number = 0,
Boolean = 1,
String = 2,
Object = 3,
MovieClip = 4,
Null = 5,
Undefined = 6,
Reference = 7,
MixedArrayStart = 8,
ObjectEnd = 9,
Array = 10,
Date = 11,
LongString = 12,
Unsupported = 13,
RecordSet = 14,
XML = 15,
TypedObject = 16,
AMF3 = 17,
}
}
pub mod amf3 {
use derive_try_from_primitive::TryFromPrimitive;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(TryFromPrimitive, Eq, PartialEq, Debug, Copy, Clone)]
#[repr(u8)]
pub enum TypeMarker {
Undefined = 0x00,
Null = 0x01,
False = 0x02,
True = 0x03,
Integer = 0x04,
Number = 0x05,
String = 0x06,
XML = 0x07,
Date = 0x08,
Array = 0x09,
Object = 0x0A,
XmlString = 0x0B,
ByteArray = 0x0C,
VectorInt = 0x0D,
VectorUInt = 0x0E,
VectorDouble = 0x0F,
VectorObject = 0x10,
Dictionary = 0x11,
}
}