flash-lso 0.7.0

Fast and safe SOL/AMF0/AMF3 parsing. Supports serde, Adobe flex and cyclic references
Documentation
use crate::amf0::writer::strict_array_writer::StrictArrayWriter;
use crate::types::{Reference, Value};

use super::{ArrayWriter, CacheKey, ObjectWriter, TypedObjectWriter};

/// A trait of common functions between writers
pub trait ObjWriter<'a> {
    /// Add an element to this object
    fn add_element(&mut self, name: &str, s: Value, inc_ref: bool);

    /// Create a writer that can serialize an object
    ///
    /// If an object with the same `cache_key` has already been written, then this will return `None` for the Writer and the existing reference
    /// If this key is unique, then both a Writer and a Reference will be returned
    fn object<'c: 'a, 'd>(
        &'d mut self,
        cache_key: CacheKey,
    ) -> (Option<ObjectWriter<'d, 'c>>, Reference)
    where
        'a: 'c,
        'a: 'd;

    /// Create a writer that can serialize an array
    ///
    /// If an object with the same `cache_key` has already been written, then this will return `None` for the Writer and the existing reference
    /// If this key is unique, then both a Writer and a Reference will be returned
    fn array<'c: 'a, 'd>(
        &'d mut self,
        cache_key: CacheKey,
    ) -> (Option<ArrayWriter<'d, 'c>>, Reference)
    where
        'a: 'c,
        'a: 'd;

    /// Create a writer that can serialize a strict array
    ///
    /// If an object with the same `cache_key` has already been written, then this will return `None` for the Writer and the existing reference
    /// If this key is unique, then both a Writer and a Reference will be returned
    fn strict_array<'c: 'a, 'd>(
        &'d mut self,
        cache_key: CacheKey,
    ) -> (Option<StrictArrayWriter<'d, 'c>>, Reference)
    where
        'a: 'c,
        'a: 'd;

    /// Typed objects can also be sent cached
    /// Create a writer that can serialize a typed object
    ///
    /// If an object with the same `cache_key` has already been written, then this will return `None` for the Writer and the existing reference
    /// If this key is unique, then both a Writer and a Reference will be returned
    fn typed_object<'c: 'a, 'd>(
        &'d mut self,
        class_name: &str,
        cache_key: CacheKey,
    ) -> (Option<TypedObjectWriter<'d, 'c>>, Reference)
    where
        'a: 'c,
        'a: 'd;

    /// Write a string
    fn string(&mut self, name: &str, s: &str) {
        self.add_element(name, Value::String(s.to_string()), true);
    }

    /// Write a number
    fn number(&mut self, name: &str, s: f64) {
        self.add_element(name, Value::Number(s), true);
    }

    /// Write a reference
    fn reference(&mut self, name: &str, v: Reference) {
        self.add_element(name, Value::Reference(v), false);
    }

    /// Write an undefined
    fn undefined(&mut self, name: &str) {
        self.add_element(name, Value::Undefined, true);
    }

    /// Write a null
    fn null(&mut self, name: &str) {
        self.add_element(name, Value::Null, true);
    }

    /// Write a bool
    fn bool(&mut self, name: &str, v: bool) {
        self.add_element(name, Value::Bool(v), true);
    }

    /// Write a date
    fn date(&mut self, name: &str, time: f64, timezone_or_utc: Option<u16>) {
        self.add_element(
            name,
            Value::Date {
                time,
                timezone_or_utc,
            },
            true,
        )
    }

    /// Write an XML
    fn xml(&mut self, name: &str, v: &str, is_string: bool) {
        self.add_element(
            name,
            Value::XML {
                value: v.to_string(),
                is_string,
            },
            true,
        );
    }

    /// Create a reference in the root
    fn make_reference(&mut self) -> Reference;

    /// Retrieve a reference from the cache
    fn cache_get(&mut self, cache_key: &CacheKey) -> Option<Reference>;

    /// Add a reference to the reference cache
    fn cache_add(&mut self, cache_key: CacheKey, reference: Reference);
}