Skip to main content

flash_lso/amf0/writer/
obj_writer.rs

1use crate::amf0::writer::strict_array_writer::StrictArrayWriter;
2use crate::types::{Reference, Value};
3
4use super::{ArrayWriter, CacheKey, ObjectWriter, TypedObjectWriter};
5
6/// A trait of common functions between writers
7pub trait ObjWriter<'a> {
8    /// Add an element to this object
9    fn add_element(&mut self, name: &str, s: Value, inc_ref: bool);
10
11    /// Create a writer that can serialize an object
12    ///
13    /// If an object with the same `cache_key` has already been written, then this will return `None` for the Writer and the existing reference
14    /// If this key is unique, then both a Writer and a Reference will be returned
15    fn object<'c: 'a, 'd>(
16        &'d mut self,
17        cache_key: CacheKey,
18    ) -> (Option<ObjectWriter<'d, 'c>>, Reference)
19    where
20        'a: 'c,
21        'a: 'd;
22
23    /// Create a writer that can serialize an array
24    ///
25    /// If an object with the same `cache_key` has already been written, then this will return `None` for the Writer and the existing reference
26    /// If this key is unique, then both a Writer and a Reference will be returned
27    fn array<'c: 'a, 'd>(
28        &'d mut self,
29        cache_key: CacheKey,
30    ) -> (Option<ArrayWriter<'d, 'c>>, Reference)
31    where
32        'a: 'c,
33        'a: 'd;
34
35    /// Create a writer that can serialize a strict array
36    ///
37    /// If an object with the same `cache_key` has already been written, then this will return `None` for the Writer and the existing reference
38    /// If this key is unique, then both a Writer and a Reference will be returned
39    fn strict_array<'c: 'a, 'd>(
40        &'d mut self,
41        cache_key: CacheKey,
42    ) -> (Option<StrictArrayWriter<'d, 'c>>, Reference)
43    where
44        'a: 'c,
45        'a: 'd;
46
47    /// Typed objects can also be sent cached
48    /// Create a writer that can serialize a typed object
49    ///
50    /// If an object with the same `cache_key` has already been written, then this will return `None` for the Writer and the existing reference
51    /// If this key is unique, then both a Writer and a Reference will be returned
52    fn typed_object<'c: 'a, 'd>(
53        &'d mut self,
54        class_name: &str,
55        cache_key: CacheKey,
56    ) -> (Option<TypedObjectWriter<'d, 'c>>, Reference)
57    where
58        'a: 'c,
59        'a: 'd;
60
61    /// Write a string
62    fn string(&mut self, name: &str, s: &str) {
63        self.add_element(name, Value::String(s.to_string()), true);
64    }
65
66    /// Write a number
67    fn number(&mut self, name: &str, s: f64) {
68        self.add_element(name, Value::Number(s), true);
69    }
70
71    /// Write a reference
72    fn reference(&mut self, name: &str, v: Reference) {
73        self.add_element(name, Value::Reference(v), false);
74    }
75
76    /// Write an undefined
77    fn undefined(&mut self, name: &str) {
78        self.add_element(name, Value::Undefined, true);
79    }
80
81    /// Write a null
82    fn null(&mut self, name: &str) {
83        self.add_element(name, Value::Null, true);
84    }
85
86    /// Write a bool
87    fn bool(&mut self, name: &str, v: bool) {
88        self.add_element(name, Value::Bool(v), true);
89    }
90
91    /// Write a date
92    fn date(&mut self, name: &str, time: f64, timezone_or_utc: Option<u16>) {
93        self.add_element(
94            name,
95            Value::Date {
96                time,
97                timezone_or_utc,
98            },
99            true,
100        )
101    }
102
103    /// Write an XML
104    fn xml(&mut self, name: &str, v: &str, is_string: bool) {
105        self.add_element(
106            name,
107            Value::XML {
108                value: v.to_string(),
109                is_string,
110            },
111            true,
112        );
113    }
114
115    /// Create a reference in the root
116    fn make_reference(&mut self) -> Reference;
117
118    /// Retrieve a reference from the cache
119    fn cache_get(&mut self, cache_key: &CacheKey) -> Option<Reference>;
120
121    /// Add a reference to the reference cache
122    fn cache_add(&mut self, cache_key: CacheKey, reference: Reference);
123}