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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
use fmt;
use PhantomData;
/*
use hexga_utils::non_empty_stack::NonEmptyStack;
use super::*;
pub type SerializerRon<T=WriteFmt2Io<Vec<u8>>> = ron::ser::Serializer<T>;
pub type SerializerJson<T=Vec<u8>> = serde_json::Serializer<T>;
//pub type SerializerXml<T=Vec<u8>> = serde_xml_rs::Serializer<T>;
#[doc(hidden)]
pub struct WriteFmt2Io<T> { writer : T }
impl std::fmt::Write for WriteFmt2Io<Vec<u8>>
{
fn write_str(&mut self, s: &str) -> std::fmt::Result
{
self.writer.extend_from_slice(s.as_bytes());
Ok(())
}
}
pub trait FsWriteExtension : FsWrite
{
fn serialize_with_flags<P: AsRef<Path>, T: ?Sized + Serialize>(&mut self, path: P, value: &T, flags: SerializerFlags) -> FileResult
{
SerializerFile
{
fs: todo!(),
path: todo!(),
flags,
serializer: todo!(),
}
}
}
impl<Fs> FsWriteExtension for Fs where Fs: FsWrite {}
pub struct SerializerFile<'a,FS> //= SerializerFileOf<'a,FS,&'a mut Option<SerializerMarkup>>;
{
fs: &'a mut FS,
path : Cow<'a,Path>,
flags : SerializerFlags,
serializer : &'a mut SerializerMarkup,
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SerializerFlags
{
pub multi_file: bool,
pub human_readable : bool,
}
// #[derive(Serialize, Deserialize)]
// #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Markup<Ron,Json>
{
Ron(Ron),
Json(Json),
}
pub type SerializerMarkup = Markup<SerializerRon,SerializerJson>;
impl SerializerMarkup
{
pub fn into_inner(self) -> Vec<u8>
{
match self
{
SerializerMarkup::Ron(serializer) => serializer.into_inner().writer,
SerializerMarkup::Json(serializer) => serializer.into_inner(),
//SerializerMarkup::Xml(serializer) => serializer.into_inner(),
}
}
pub fn new(format: FormatMarkup, bytes: Vec<u8>) -> Self
{
match format
{
FormatMarkup::Ron => Self::Ron(SerializerRon::new(WriteFmt2Io{writer: bytes}, Some(Default::default())).expect("failed to create ron serializer")),
FormatMarkup::Json => Self::Json(SerializerJson::new(bytes)),
_ => todo!()
//FormatMarkup::Xml => Self::Xml(SerializerXml::new_from_writer(bytes)),
}
}
}
impl<'a,FS> SerializerFile<'a,FS>
where FS: Fs
{
fn save(&mut self) -> FileResult
{
self.fs.write_bytes_at(self.path, &self.serializer.take().unwrap().into_inner()).map_err(|e| FileError::new(e).with_path(Some(self.path.to_path_buf())))
}
}
/*
impl<'a,FS,S> SerializerFileOf<'a,FS,S>
where FS: Fs
{
fn extract(self) -> (SerializerFileOf<'a,FS,()>, S)
{
let Self { fs, path, flags, serializer } = self;
(
SerializerFileOf
{
fs,
path,
flags,
serializer : (),
},
serializer
)
}
fn save(self, bytes: &[u8]) -> FileResult
{
self.fs.write_bytes_at(self.path, &bytes).map_err(|e| FileError::new(e).with_path(Some(self.path.to_path_buf())))
}
}
*/
type SerializerOk = ();
type SerializerErr = FileError;
/*
impl<'a,FS> SerializeSeq for SerializerFile<'a,FS>
where FS: Fs
{
type Ok = SerializerOk;
type Error = SerializerErr;
fn serialize_element<T>(&mut self, _: &T) -> Result<(), Self::Error>
where
T: ?Sized + Serialize,
{
Err(Default::default())
}
fn end(self) -> Result<Self::Ok, Self::Error> { Err(Default::default()) }
}
impl<'a,FS> SerializeTuple for SerializerFile<'a,FS>
where FS: Fs
{
type Ok = SerializerOk;
type Error = SerializerErr;
fn serialize_element<T>(&mut self, _: &T) -> Result<(), Self::Error>
where
T: ?Sized + Serialize,
{
Err(Default::default())
}
fn end(self) -> Result<Self::Ok, Self::Error> { Err(Default::default()) }
}
impl<'a,FS> SerializeTupleStruct for SerializerFile<'a,FS>
where FS: Fs
{
type Ok = SerializerOk;
type Error = SerializerErr;
fn serialize_field<T>(&mut self, _: &T) -> Result<(), Self::Error>
where
T: ?Sized + Serialize,
{
Err(Default::default())
}
fn end(self) -> Result<Self::Ok, Self::Error> { Err(Default::default()) }
}
impl<'a,FS> SerializeTupleVariant for SerializerFile<'a,FS>
where FS: Fs
{
type Ok = SerializerOk;
type Error = SerializerErr;
fn serialize_field<T>(&mut self, _value: &T) -> Result<(), Self::Error>
where
T: ?Sized + Serialize,
{
Err(Default::default())
}
fn end(self) -> Result<Self::Ok, Self::Error> { Err(Default::default()) }
}
impl<'a,FS> SerializeMap for SerializerFile<'a,FS>
where FS: Fs
{
type Ok = SerializerOk;
type Error = SerializerErr;
fn serialize_key<T>(&mut self, _key: &T) -> Result<(), Self::Error>
where
T: ?Sized + Serialize,
{
Err(Default::default())
}
fn serialize_value<T>(&mut self, _value: &T) -> Result<(), Self::Error>
where
T: ?Sized + Serialize,
{
Err(Default::default())
}
fn end(self) -> Result<Self::Ok, Self::Error> { Err(Default::default()) }
}
impl<'a,FS> SerializeStruct for SerializerFile<'a,FS>
where FS: Fs
{
type Ok = SerializerOk;
type Error = SerializerErr;
fn serialize_field<T>(&mut self, _key: &'static str, _value: &T) -> Result<(), Self::Error>
where
T: ?Sized + Serialize,
{
Err(Default::default())
}
fn end(self) -> Result<Self::Ok, Self::Error> { Err(Default::default()) }
}
impl<'a,FS> SerializeStructVariant for SerializerFile<'a,FS>
where FS: Fs
{
type Ok = SerializerOk;
type Error = SerializerErr;
fn serialize_field<T>(&mut self, _key: &'static str, _value: &T) -> Result<(), Self::Error>
where
T: ?Sized + Serialize,
{
Err(Default::default())
}
fn end(self) -> Result<Self::Ok, Self::Error> { Err(Default::default()) }
}
*/
impl<'a,FS> SerializerFile<'a,FS>
where FS: Fs
{
fn encoded(&mut self, r: Result<(), EncodeError>) -> Result<(), FileError>
{
let path = self.path;
match r
{
Ok(_) => self.save(),
Err(e) => Err(FileError::new(FileErrorKind::Encode(e)).with_path(Some(path.to_path_buf()))),
}
}
}
macro_rules! dispatch_ser {
($self:expr, $method_name:ident $(, $value:expr)*) => {{
match $self.serializer.as_mut().unwrap() {
SerializerMarkup::Ron(serializer) => {
serializer.$method_name($($value),*)
.map_err(|e| EncodeError::markup::<Self>(Extension::RON, e))
}
SerializerMarkup::Json(serializer) => {
serializer.$method_name($($value),*)
.map_err(|e| EncodeError::markup::<Self>(Extension::JSON, e))
}
/*
SerializerMarkup::Xml(serializer) => {
serializer.$method_name($($value),*)
.map_err(|e| EncodeError::markup::<Self>(Extension::XML, e))
}
*/
}
}};
}
macro_rules! impl_ser {
($self:expr, $method_name:ident $(, $value:expr)*) => {{
let r = dispatch_ser!($self, $method_name $(, $value)*);
$self.encoded(r)
}};
}
impl<'a,FS> Serializer for &'a mut SerializerFile<'a,FS>
where FS: Fs
{
type Ok = SerializerOk;
type Error = SerializerErr;
type SerializeSeq = SerializerFileSeq<'a,FS>;
type SerializeTuple = SerializerFileTuple<'a,FS>;
type SerializeTupleStruct = SerializerFileTupleStruct<'a,FS>;
type SerializeTupleVariant = SerializerFileTupleVariant<'a,FS>;
type SerializeMap = SerializerFileMap<'a,FS>;
type SerializeStruct = SerializerFileStruct<'a,FS>;
type SerializeStructVariant = SerializerFileStructVariant<'a,FS>;
fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> { impl_ser!(self, serialize_bool, v) }
fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> { impl_ser!(self, serialize_i8, v) }
fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> { impl_ser!(self, serialize_i16, v) }
fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> { impl_ser!(self, serialize_i32, v) }
fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> { impl_ser!(self, serialize_i64, v) }
fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> { impl_ser!(self, serialize_u8, v) }
fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> { impl_ser!(self, serialize_u16, v) }
fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> { impl_ser!(self, serialize_u32, v) }
fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> { impl_ser!(self, serialize_u64, v) }
fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> { impl_ser!(self, serialize_f32, v) }
fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> { impl_ser!(self, serialize_f64, v) }
fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> { impl_ser!(self, serialize_char, v) }
fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> { impl_ser!(self, serialize_str, v) }
fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
todo!("check for url / binary url (depending if binary is set here)");
impl_ser!(self, serialize_bytes, v)
}
fn serialize_none(self) -> Result<Self::Ok, Self::Error> { impl_ser!(self, serialize_none) }
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { impl_ser!(self, serialize_unit) }
fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> { impl_ser!(self, serialize_unit_struct, name) }
fn serialize_unit_variant(self, name: &'static str, variant_index: u32, variant: &'static str) -> Result<Self::Ok, Self::Error> { impl_ser!(self, serialize_unit_variant, name, variant_index, variant) }
fn serialize_newtype_struct<T>(self, name: &'static str, value: &T) -> Result<Self::Ok, Self::Error> where T: ?Sized + Serialize
{
todo!()
}
fn serialize_some<T>(mut self, v: &T) -> Result<Self::Ok, Self::Error> where T: ?Sized + Serialize
{
todo!()
}
fn serialize_newtype_variant<T>(mut self, name: &'static str, variant_index: u32, variant: &'static str, value: &T) -> Result<Self::Ok, Self::Error>
where
T: ?Sized + Serialize
{
todo!()
}
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error>
{
let seq = match self.serializer.as_mut().unwrap() {
SerializerMarkup::Ron(ser) => {
match ser.serialize_seq(len)
{
Ok(s) => Ok(SerializerMarkupSeq::Ron(s)),
Err(e) => Err(FileError::new(EncodeError::markup::<Self>(Extension::RON, e)).with_path(Some(self.path.to_path_buf()))),
}
}
SerializerMarkup::Json(ser) =>
{
match ser.serialize_seq(len)
{
Ok(s) => Ok(SerializerMarkupSeq::Json(s)),
Err(e) => Err(FileError::new(EncodeError::markup::<Self>(Extension::JSON, e)).with_path(Some(self.path.to_path_buf()))),
}
}
}?;
Ok(
SerializerFileSeq
{
file: self,
seq,
}
)
}
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> { Err(Default::default()) }
fn serialize_tuple_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeTupleStruct, Self::Error> { Err(Default::default()) }
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error>
{
Err(Default::default())
}
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { Err(Default::default()) }
fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct, Self::Error> { Err(Default::default()) }
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error>
{
Err(Default::default())
}
fn is_human_readable(&self) -> bool { self.flags.human_readable }
}
pub type SerializerRonSeq<'a,T=WriteFmt2Io<Vec<u8>>> = ::ron::ser::Compound<'a,T>;
pub type SerializerJsonSeq<'a,T=Vec<u8>> = ::serde_json::ser::Compound<'a,T,serde_json::ser::CompactFormatter>;
pub type SerializerMarkupSeq<'a> = Markup<SerializerRonSeq<'a>,SerializerJsonSeq<'a>>;
pub struct SerializerFileSeq<'a,FS>
{
file : &'a mut SerializerFile<'a,FS>,
seq : SerializerMarkupSeq<'a>
}
//pub type SerializerFileSeq<'a,FS> = SerializerFileOf<'a,FS,(&'a mut SerializerFile<'a,FS>, SerializerMarkupSeq<'a>)>;
impl<'a,FS> SerializeSeq for SerializerFileSeq<'a,FS>
where FS: Fs
{
type Ok = SerializerOk;
type Error=SerializerErr;
fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: ?Sized + Serialize {
match &mut self.serializer
{
Markup::Ron(s) =>
{
//SerializeSeq::serialize_element(s, value).map_err(|e| FileError::new(EncodeError::markup::<Self>(Extension::RON, e)).with_path(Some(self.path.to_path_buf()))),
}
Markup::Json(s) => SerializeSeq::serialize_element(s, value).map_err(|e| FileError::new(EncodeError::markup::<Self>(Extension::JSON, e)).with_path(Some(self.path.to_path_buf()))),
}
}
fn end(self) -> Result<Self::Ok, Self::Error> {
match self.serializer
{
Markup::Ron(s) => SerializeSeq::end(s).map_err(|e| FileError::new(EncodeError::markup::<Self>(Extension::RON, e)).with_path(Some(self.path.to_path_buf()))),
Markup::Json(s) => SerializeSeq::end(s).map_err(|e| FileError::new(EncodeError::markup::<Self>(Extension::JSON, e)).with_path(Some(self.path.to_path_buf()))),
}
}
}
pub type SerializerRonTuple<'a,T=WriteFmt2Io<Vec<u8>>> = ::ron::ser::Compound<'a,T>;
pub type SerializerJsonTuple<'a,T=Vec<u8>> = ::serde_json::ser::Compound<'a,T,serde_json::ser::CompactFormatter>;
pub type SerializerMarkupTuple<'a> = Markup<SerializerRonTuple<'a>,SerializerJsonTuple<'a>>;
pub type SerializerFileTuple<'a,FS> = SerializerFileOf<'a,FS,SerializerMarkupTuple<'a>>;
pub type SerializerRonTupleStruct<'a,T=WriteFmt2Io<Vec<u8>>> = ::ron::ser::Compound<'a,T>;
pub type SerializerJsonTupleStruct<'a,T=Vec<u8>> = ::serde_json::ser::Compound<'a,T,serde_json::ser::CompactFormatter>;
pub type SerializerMarkupTupleStruct<'a> = Markup<SerializerRonTupleStruct<'a>,SerializerJsonTupleStruct<'a>>;
pub type SerializerFileTupleStruct<'a,FS> = SerializerFileOf<'a,FS,SerializerMarkupTupleStruct<'a>>;
pub type SerializerRonTupleVariant<'a,T=WriteFmt2Io<Vec<u8>>> = ::ron::ser::Compound<'a,T>;
pub type SerializerJsonTupleVariant<'a,T=Vec<u8>> = ::serde_json::ser::Compound<'a,T,serde_json::ser::CompactFormatter>;
pub type SerializerMarkupTupleVariant<'a> = Markup<SerializerRonTupleVariant<'a>,SerializerJsonTupleVariant<'a>>;
pub type SerializerFileTupleVariant<'a,FS> = SerializerFileOf<'a,FS,SerializerMarkupTupleVariant<'a>>;
pub type SerializerRonMap<'a,T=WriteFmt2Io<Vec<u8>>> = ::ron::ser::Compound<'a,T>;
pub type SerializerJsonMap<'a,T=Vec<u8>> = ::serde_json::ser::Compound<'a,T,serde_json::ser::CompactFormatter>;
pub type SerializerMarkupMap<'a> = Markup<SerializerRonMap<'a>,SerializerJsonMap<'a>>;
pub type SerializerFileMap<'a,FS> = SerializerFileOf<'a,FS,SerializerMarkupMap<'a>>;
pub type SerializerRonStruct<'a,T=WriteFmt2Io<Vec<u8>>> = ::ron::ser::Compound<'a,T>;
pub type SerializerJsonStruct<'a,T=Vec<u8>> = ::serde_json::ser::Compound<'a,T,serde_json::ser::CompactFormatter>;
pub type SerializerMarkupStruct<'a> = Markup<SerializerRonStruct<'a>,SerializerJsonStruct<'a>>;
pub type SerializerFileStruct<'a,FS> = SerializerFileOf<'a,FS,SerializerMarkupStruct<'a>>;
pub type SerializerRonStructVariant<'a,T=WriteFmt2Io<Vec<u8>>> = ::ron::ser::Compound<'a,T>;
pub type SerializerJsonStructVariant<'a,T=Vec<u8>> = ::serde_json::ser::Compound<'a,T,serde_json::ser::CompactFormatter>;
pub type SerializerMarkupStructVariant<'a> = Markup<SerializerRonStructVariant<'a>,SerializerJsonStructVariant<'a>>;
pub type SerializerFileStructVariant<'a,FS> = SerializerFileOf<'a,FS,SerializerMarkupStructVariant<'a>>;
*/