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