1#[cfg(test)]
4mod tests;
5
6use super::{Attribute, Block, BlockLabel, Body, Structure};
7use crate::expr::ser::{
8 ExpressionSerializer, SerializeExpressionMap, SerializeExpressionStruct,
9 SerializeExpressionStructVariant, SerializeExpressionTupleVariant, EXPR_HANDLES,
10 EXPR_HANDLE_MARKER,
11};
12use crate::ser::{
13 blocks::{BLOCK_MARKER, LABELED_BLOCK_MARKER},
14 in_internal_serialization, IdentifierSerializer, InternalHandles,
15 SerializeInternalHandleStruct, StringSerializer,
16};
17use crate::{Error, Expression, Identifier, ObjectKey, Result};
18use serde::ser::{self, Serialize, SerializeMap, SerializeStruct};
19use std::fmt;
20
21const STRUCTURE_HANDLE_MARKER: &str = "\x00$hcl::StructureHandle";
22
23thread_local! {
24 static STRUCTURE_HANDLES: InternalHandles<Structure> = InternalHandles::new(STRUCTURE_HANDLE_MARKER);
25}
26
27impl ser::Serialize for Attribute {
28 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
29 where
30 S: ser::Serializer,
31 {
32 if in_internal_serialization() {
33 STRUCTURE_HANDLES.with(|sh| sh.serialize(self.clone(), serializer))
34 } else {
35 let mut s = serializer.serialize_struct("Attribute", 2)?;
36 s.serialize_field("key", &self.key)?;
37 s.serialize_field("expr", &self.expr)?;
38 s.end()
39 }
40 }
41}
42
43impl ser::Serialize for Block {
44 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
45 where
46 S: ser::Serializer,
47 {
48 if in_internal_serialization() {
49 STRUCTURE_HANDLES.with(|sh| sh.serialize(self.clone(), serializer))
50 } else {
51 let mut s = serializer.serialize_struct("Block", 3)?;
52 s.serialize_field("identifier", &self.identifier)?;
53 s.serialize_field("labels", &self.labels)?;
54 s.serialize_field("body", &self.body)?;
55 s.end()
56 }
57 }
58}
59
60impl ser::Serialize for Structure {
61 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
62 where
63 S: ser::Serializer,
64 {
65 if in_internal_serialization() {
66 return STRUCTURE_HANDLES.with(|sh| sh.serialize(self.clone(), serializer));
67 }
68
69 match self {
70 Structure::Attribute(attr) => attr.serialize(serializer),
71 Structure::Block(block) => block.serialize(serializer),
72 }
73 }
74}
75
76pub(crate) enum Structures {
77 Single(Structure),
78 Multiple(Vec<Structure>),
79}
80
81impl From<Structures> for Body {
82 fn from(value: Structures) -> Self {
83 Body::from_iter(value)
84 }
85}
86
87impl From<Attribute> for Structures {
88 fn from(attr: Attribute) -> Self {
89 Structures::Single(Structure::Attribute(attr))
90 }
91}
92
93impl From<Block> for Structures {
94 fn from(block: Block) -> Self {
95 Structures::Single(Structure::Block(block))
96 }
97}
98
99impl From<Vec<Structure>> for Structures {
100 fn from(structures: Vec<Structure>) -> Self {
101 Structures::Multiple(structures)
102 }
103}
104
105impl IntoIterator for Structures {
106 type Item = Structure;
107 type IntoIter = std::vec::IntoIter<Structure>;
108
109 fn into_iter(self) -> Self::IntoIter {
110 match self {
111 Structures::Single(single) => vec![single].into_iter(),
112 Structures::Multiple(multiple) => multiple.into_iter(),
113 }
114 }
115}
116
117pub(crate) struct BodySerializer;
118
119impl ser::Serializer for BodySerializer {
120 type Ok = Body;
121 type Error = Error;
122
123 type SerializeSeq = SerializeBodySeq;
124 type SerializeTuple = SerializeBodySeq;
125 type SerializeTupleStruct = SerializeBodySeq;
126 type SerializeTupleVariant = SerializeBodyTupleVariant;
127 type SerializeMap = SerializeBodyMap;
128 type SerializeStruct = SerializeBodyStruct;
129 type SerializeStructVariant = SerializeBodyStructVariant;
130
131 serialize_unsupported! {
132 bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64
133 char str bytes none unit unit_struct unit_variant
134 }
135 serialize_self! { some newtype_struct }
136 forward_to_serialize_seq! { tuple tuple_struct }
137
138 fn serialize_newtype_variant<T>(
139 self,
140 _name: &'static str,
141 _variant_index: u32,
142 variant: &'static str,
143 value: &T,
144 ) -> Result<Self::Ok>
145 where
146 T: ?Sized + Serialize,
147 {
148 let ident = Identifier::new(variant)?;
149
150 value
151 .serialize(StructureSerializer::new(ident))
152 .map(Into::into)
153 }
154
155 fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
156 Ok(SerializeBodySeq::new(len))
157 }
158
159 fn serialize_tuple_variant(
160 self,
161 _name: &'static str,
162 _variant_index: u32,
163 variant: &'static str,
164 len: usize,
165 ) -> Result<Self::SerializeTupleVariant> {
166 Ok(SerializeBodyTupleVariant::new(
167 Identifier::new(variant)?,
168 len,
169 ))
170 }
171
172 fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
173 Ok(SerializeBodyMap::new(len))
174 }
175
176 fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
177 Ok(SerializeBodyStruct::new(name, len))
178 }
179
180 fn serialize_struct_variant(
181 self,
182 _name: &'static str,
183 _variant_index: u32,
184 variant: &'static str,
185 len: usize,
186 ) -> Result<Self::SerializeStructVariant> {
187 Ok(SerializeBodyStructVariant::new(
188 Identifier::new(variant)?,
189 len,
190 ))
191 }
192}
193
194pub(crate) struct SerializeBodySeq {
195 vec: Vec<Structure>,
196}
197
198impl SerializeBodySeq {
199 fn new(len: Option<usize>) -> Self {
200 SerializeBodySeq {
201 vec: Vec::with_capacity(len.unwrap_or(0)),
202 }
203 }
204}
205
206impl ser::SerializeSeq for SerializeBodySeq {
207 type Ok = Body;
208 type Error = Error;
209
210 fn serialize_element<T>(&mut self, value: &T) -> Result<()>
211 where
212 T: ?Sized + ser::Serialize,
213 {
214 self.vec.extend(value.serialize(BodySerializer)?);
215 Ok(())
216 }
217
218 fn end(self) -> Result<Self::Ok> {
219 Ok(Body(self.vec))
220 }
221}
222
223impl ser::SerializeTuple for SerializeBodySeq {
224 impl_forward_to_serialize_seq!(serialize_element, Body);
225}
226
227impl ser::SerializeTupleStruct for SerializeBodySeq {
228 impl_forward_to_serialize_seq!(serialize_field, Body);
229}
230
231pub(crate) struct SerializeBodyTupleVariant {
232 ident: Identifier,
233 elements: Vec<Expression>,
234}
235
236impl SerializeBodyTupleVariant {
237 fn new(ident: Identifier, len: usize) -> Self {
238 SerializeBodyTupleVariant {
239 ident,
240 elements: Vec::with_capacity(len),
241 }
242 }
243}
244
245impl ser::SerializeTupleVariant for SerializeBodyTupleVariant {
246 type Ok = Body;
247 type Error = Error;
248
249 fn serialize_field<T>(&mut self, value: &T) -> Result<()>
250 where
251 T: ?Sized + ser::Serialize,
252 {
253 self.elements.push(value.serialize(ExpressionSerializer)?);
254 Ok(())
255 }
256
257 fn end(self) -> Result<Self::Ok> {
258 Ok(Attribute::new(self.ident, self.elements).into())
259 }
260}
261
262pub(crate) struct SerializeBodyMap {
263 structures: Vec<Structure>,
264 next_key: Option<Identifier>,
265}
266
267impl SerializeBodyMap {
268 fn new(len: Option<usize>) -> Self {
269 SerializeBodyMap {
270 structures: Vec::with_capacity(len.unwrap_or(0)),
271 next_key: None,
272 }
273 }
274}
275
276impl ser::SerializeMap for SerializeBodyMap {
277 type Ok = Body;
278 type Error = Error;
279
280 fn serialize_key<T>(&mut self, key: &T) -> Result<()>
281 where
282 T: ?Sized + ser::Serialize,
283 {
284 self.next_key = Some(key.serialize(IdentifierSerializer)?);
285 Ok(())
286 }
287
288 fn serialize_value<T>(&mut self, value: &T) -> Result<()>
289 where
290 T: ?Sized + ser::Serialize,
291 {
292 let key = self.next_key.take();
293 let key = key.expect("serialize_value called before serialize_key");
294
295 self.structures
296 .extend(value.serialize(StructureSerializer::new(key))?);
297 Ok(())
298 }
299
300 fn end(self) -> Result<Self::Ok> {
301 Ok(Body(self.structures))
302 }
303}
304
305pub(crate) enum SerializeBodyStruct {
306 InternalStructureHandle(SerializeInternalHandleStruct),
307 InternalExprHandle(SerializeInternalHandleStruct),
308 Map(SerializeBodyMap),
309}
310
311impl SerializeBodyStruct {
312 fn new(name: &'static str, len: usize) -> Self {
313 if name == STRUCTURE_HANDLE_MARKER {
314 SerializeBodyStruct::InternalStructureHandle(SerializeInternalHandleStruct::new())
315 } else if name == EXPR_HANDLE_MARKER {
316 SerializeBodyStruct::InternalExprHandle(SerializeInternalHandleStruct::new())
317 } else {
318 SerializeBodyStruct::Map(SerializeBodyMap::new(Some(len)))
319 }
320 }
321}
322
323impl ser::SerializeStruct for SerializeBodyStruct {
324 type Ok = Body;
325 type Error = Error;
326
327 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
328 where
329 T: ?Sized + ser::Serialize,
330 {
331 match self {
332 SerializeBodyStruct::Map(ser) => ser.serialize_entry(key, value),
333 SerializeBodyStruct::InternalStructureHandle(ser)
334 | SerializeBodyStruct::InternalExprHandle(ser) => ser.serialize_field(key, value),
335 }
336 }
337
338 fn end(self) -> Result<Self::Ok> {
339 match self {
340 SerializeBodyStruct::InternalStructureHandle(ser) => ser
341 .end()
342 .map(|handle| STRUCTURE_HANDLES.with(|sh| sh.remove(handle)).into()),
343 SerializeBodyStruct::InternalExprHandle(ser) => {
344 let handle = ser.end()?;
345
346 match EXPR_HANDLES.with(|sh| sh.remove(handle)) {
347 Expression::Object(object) => {
348 let attrs = object
349 .into_iter()
350 .map(|(key, value)| {
351 let key = match key {
352 ObjectKey::Identifier(ident) => ident,
353 ObjectKey::Expression(Expression::String(s)) => s.into(),
354 ObjectKey::Expression(expr) => {
355 return Err(ser::Error::custom(format!(
356 "encountered invalid HCL attribute key `{expr}`",
357 )))
358 }
359 };
360
361 Ok(Attribute::new(key, value))
362 })
363 .collect::<Result<Vec<_>>>()?;
364
365 Ok(attrs.into())
366 }
367 _ => Err(ser::Error::custom(
368 "non-object HCL expressions are not permitted in this context",
369 )),
370 }
371 }
372 SerializeBodyStruct::Map(ser) => ser.end(),
373 }
374 }
375}
376
377pub(crate) struct SerializeBodyStructVariant {
378 ident: Identifier,
379 structures: Vec<Structure>,
380}
381
382impl SerializeBodyStructVariant {
383 fn new(ident: Identifier, len: usize) -> Self {
384 SerializeBodyStructVariant {
385 ident,
386 structures: Vec::with_capacity(len),
387 }
388 }
389}
390
391impl ser::SerializeStructVariant for SerializeBodyStructVariant {
392 type Ok = Body;
393 type Error = Error;
394
395 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
396 where
397 T: ?Sized + ser::Serialize,
398 {
399 let ident = Identifier::new(key)?;
400
401 self.structures
402 .extend(value.serialize(StructureSerializer::new(ident))?);
403 Ok(())
404 }
405
406 fn end(self) -> Result<Self::Ok> {
407 Ok(Block::builder(self.ident)
408 .add_structures(self.structures)
409 .build()
410 .into())
411 }
412}
413
414pub(crate) struct StructureSerializer {
415 ident: Identifier,
416}
417
418impl StructureSerializer {
419 fn new(ident: Identifier) -> Self {
420 StructureSerializer { ident }
421 }
422
423 fn into_attr(self, expr: Expression) -> Structures {
424 Attribute::new(self.ident, expr).into()
425 }
426}
427
428impl ser::Serializer for StructureSerializer {
429 type Ok = Structures;
430 type Error = Error;
431
432 type SerializeSeq = SerializeStructureSeq;
433 type SerializeTuple = SerializeStructureSeq;
434 type SerializeTupleStruct = SerializeStructureSeq;
435 type SerializeTupleVariant = SerializeStructureTupleVariant;
436 type SerializeMap = SerializeStructureMap;
437 type SerializeStruct = SerializeStructureStruct;
438 type SerializeStructVariant = SerializeStructureStructVariant;
439
440 serialize_self! { some }
441 forward_to_serialize_seq! { tuple tuple_struct }
442
443 fn serialize_bool(self, value: bool) -> Result<Self::Ok> {
444 ExpressionSerializer
445 .serialize_bool(value)
446 .map(|expr| self.into_attr(expr))
447 }
448
449 fn serialize_i8(self, value: i8) -> Result<Self::Ok> {
450 ExpressionSerializer
451 .serialize_i8(value)
452 .map(|expr| self.into_attr(expr))
453 }
454
455 fn serialize_i16(self, value: i16) -> Result<Self::Ok> {
456 ExpressionSerializer
457 .serialize_i16(value)
458 .map(|expr| self.into_attr(expr))
459 }
460
461 fn serialize_i32(self, value: i32) -> Result<Self::Ok> {
462 ExpressionSerializer
463 .serialize_i32(value)
464 .map(|expr| self.into_attr(expr))
465 }
466
467 fn serialize_i64(self, value: i64) -> Result<Self::Ok> {
468 ExpressionSerializer
469 .serialize_i64(value)
470 .map(|expr| self.into_attr(expr))
471 }
472
473 fn serialize_u8(self, value: u8) -> Result<Self::Ok> {
474 ExpressionSerializer
475 .serialize_u8(value)
476 .map(|expr| self.into_attr(expr))
477 }
478
479 fn serialize_u16(self, value: u16) -> Result<Self::Ok> {
480 ExpressionSerializer
481 .serialize_u16(value)
482 .map(|expr| self.into_attr(expr))
483 }
484
485 fn serialize_u32(self, value: u32) -> Result<Self::Ok> {
486 ExpressionSerializer
487 .serialize_u32(value)
488 .map(|expr| self.into_attr(expr))
489 }
490
491 fn serialize_u64(self, value: u64) -> Result<Self::Ok> {
492 ExpressionSerializer
493 .serialize_u64(value)
494 .map(|expr| self.into_attr(expr))
495 }
496
497 fn serialize_f32(self, value: f32) -> Result<Self::Ok> {
498 ExpressionSerializer
499 .serialize_f32(value)
500 .map(|expr| self.into_attr(expr))
501 }
502
503 fn serialize_f64(self, value: f64) -> Result<Self::Ok> {
504 ExpressionSerializer
505 .serialize_f64(value)
506 .map(|expr| self.into_attr(expr))
507 }
508
509 fn serialize_char(self, value: char) -> Result<Self::Ok> {
510 ExpressionSerializer
511 .serialize_char(value)
512 .map(|expr| self.into_attr(expr))
513 }
514
515 fn serialize_str(self, value: &str) -> Result<Self::Ok> {
516 ExpressionSerializer
517 .serialize_str(value)
518 .map(|expr| self.into_attr(expr))
519 }
520
521 fn serialize_bytes(self, value: &[u8]) -> Result<Self::Ok> {
522 ExpressionSerializer
523 .serialize_bytes(value)
524 .map(|expr| self.into_attr(expr))
525 }
526
527 fn serialize_unit(self) -> Result<Self::Ok> {
528 ExpressionSerializer
529 .serialize_unit()
530 .map(|expr| self.into_attr(expr))
531 }
532
533 fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok> {
534 ExpressionSerializer
535 .serialize_unit_struct(name)
536 .map(|expr| self.into_attr(expr))
537 }
538
539 fn serialize_unit_variant(
540 self,
541 name: &'static str,
542 variant_index: u32,
543 variant: &'static str,
544 ) -> Result<Self::Ok> {
545 ExpressionSerializer
546 .serialize_unit_variant(name, variant_index, variant)
547 .map(|expr| self.into_attr(expr))
548 }
549
550 fn serialize_newtype_struct<T>(self, name: &'static str, value: &T) -> Result<Self::Ok>
551 where
552 T: ?Sized + ser::Serialize,
553 {
554 if name == BLOCK_MARKER {
555 BlockSerializer::new(self.ident).serialize_newtype_struct(name, value)
556 } else if name == LABELED_BLOCK_MARKER {
557 LabeledBlockSerializer::new(self.ident).serialize_newtype_struct(name, value)
558 } else {
559 ExpressionSerializer
560 .serialize_newtype_struct(name, value)
561 .map(|expr| self.into_attr(expr))
562 }
563 }
564
565 fn serialize_newtype_variant<T>(
566 self,
567 name: &'static str,
568 variant_index: u32,
569 variant: &'static str,
570 value: &T,
571 ) -> Result<Self::Ok>
572 where
573 T: ?Sized + ser::Serialize,
574 {
575 ExpressionSerializer
576 .serialize_newtype_variant(name, variant_index, variant, value)
577 .map(|expr| self.into_attr(expr))
578 }
579
580 fn serialize_none(self) -> Result<Self::Ok> {
581 ExpressionSerializer
582 .serialize_none()
583 .map(|expr| self.into_attr(expr))
584 }
585
586 fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
587 Ok(SerializeStructureSeq::new(self.ident, len))
588 }
589
590 fn serialize_tuple_variant(
591 self,
592 _name: &'static str,
593 _variant_index: u32,
594 variant: &'static str,
595 len: usize,
596 ) -> Result<Self::SerializeTupleVariant> {
597 Ok(SerializeStructureTupleVariant::new(
598 self.ident, variant, len,
599 ))
600 }
601
602 fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
603 Ok(SerializeStructureMap::new(self.ident, len))
604 }
605
606 fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
607 Ok(SerializeStructureStruct::new(self.ident, name, len))
608 }
609
610 fn serialize_struct_variant(
611 self,
612 _name: &'static str,
613 _variant_index: u32,
614 variant: &'static str,
615 len: usize,
616 ) -> Result<Self::SerializeStructVariant> {
617 Ok(SerializeStructureStructVariant::new(
618 self.ident, variant, len,
619 ))
620 }
621
622 fn collect_str<T>(self, value: &T) -> Result<Self::Ok>
623 where
624 T: ?Sized + fmt::Display,
625 {
626 ExpressionSerializer
627 .collect_str(value)
628 .map(|expr| self.into_attr(expr))
629 }
630}
631
632pub(crate) struct SerializeStructureSeq {
633 ident: Identifier,
634 elements: Vec<Expression>,
635}
636
637impl SerializeStructureSeq {
638 fn new(ident: Identifier, len: Option<usize>) -> Self {
639 SerializeStructureSeq {
640 ident,
641 elements: Vec::with_capacity(len.unwrap_or(0)),
642 }
643 }
644}
645
646impl ser::SerializeSeq for SerializeStructureSeq {
647 type Ok = Structures;
648 type Error = Error;
649
650 fn serialize_element<T>(&mut self, value: &T) -> Result<()>
651 where
652 T: ?Sized + ser::Serialize,
653 {
654 self.elements.push(value.serialize(ExpressionSerializer)?);
655 Ok(())
656 }
657
658 fn end(self) -> Result<Self::Ok> {
659 Ok(Attribute::new(self.ident, self.elements).into())
660 }
661}
662
663impl ser::SerializeTuple for SerializeStructureSeq {
664 impl_forward_to_serialize_seq!(serialize_element, Structures);
665}
666
667impl ser::SerializeTupleStruct for SerializeStructureSeq {
668 impl_forward_to_serialize_seq!(serialize_field, Structures);
669}
670
671pub(crate) struct SerializeStructureTupleVariant {
672 ident: Identifier,
673 inner: SerializeExpressionTupleVariant,
674}
675
676impl SerializeStructureTupleVariant {
677 fn new(ident: Identifier, variant: &'static str, len: usize) -> Self {
678 SerializeStructureTupleVariant {
679 ident,
680 inner: SerializeExpressionTupleVariant::new(variant, len),
681 }
682 }
683}
684
685impl ser::SerializeTupleVariant for SerializeStructureTupleVariant {
686 type Ok = Structures;
687 type Error = Error;
688
689 fn serialize_field<T>(&mut self, value: &T) -> Result<()>
690 where
691 T: ?Sized + ser::Serialize,
692 {
693 self.inner.serialize_field(value)
694 }
695
696 fn end(self) -> Result<Self::Ok> {
697 self.inner
698 .end()
699 .map(|expr| Attribute::new(self.ident, expr).into())
700 }
701}
702
703pub(crate) struct SerializeStructureMap {
704 ident: Identifier,
705 inner: SerializeExpressionMap,
706}
707
708impl SerializeStructureMap {
709 fn new(ident: Identifier, len: Option<usize>) -> Self {
710 SerializeStructureMap {
711 ident,
712 inner: SerializeExpressionMap::new(len),
713 }
714 }
715}
716
717impl ser::SerializeMap for SerializeStructureMap {
718 type Ok = Structures;
719 type Error = Error;
720
721 fn serialize_key<T>(&mut self, key: &T) -> Result<()>
722 where
723 T: ?Sized + ser::Serialize,
724 {
725 self.inner.serialize_key(key)
726 }
727
728 fn serialize_value<T>(&mut self, value: &T) -> Result<()>
729 where
730 T: ?Sized + ser::Serialize,
731 {
732 self.inner.serialize_value(value)
733 }
734
735 fn end(self) -> Result<Self::Ok> {
736 self.inner
737 .end()
738 .map(|expr| Attribute::new(self.ident, expr).into())
739 }
740}
741
742pub(crate) struct SerializeStructureStruct {
743 ident: Identifier,
744 inner: SerializeExpressionStruct,
745}
746
747impl SerializeStructureStruct {
748 fn new(ident: Identifier, name: &'static str, len: usize) -> Self {
749 SerializeStructureStruct {
750 ident,
751 inner: SerializeExpressionStruct::new(name, len),
752 }
753 }
754}
755
756impl ser::SerializeStruct for SerializeStructureStruct {
757 type Ok = Structures;
758 type Error = Error;
759
760 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
761 where
762 T: ?Sized + ser::Serialize,
763 {
764 self.inner.serialize_field(key, value)
765 }
766
767 fn end(self) -> Result<Self::Ok> {
768 self.inner
769 .end()
770 .map(|expr| Attribute::new(self.ident, expr).into())
771 }
772}
773
774pub(crate) struct SerializeStructureStructVariant {
775 ident: Identifier,
776 inner: SerializeExpressionStructVariant,
777}
778
779impl SerializeStructureStructVariant {
780 fn new(ident: Identifier, variant: &'static str, len: usize) -> Self {
781 SerializeStructureStructVariant {
782 ident,
783 inner: SerializeExpressionStructVariant::new(variant, len),
784 }
785 }
786}
787
788impl ser::SerializeStructVariant for SerializeStructureStructVariant {
789 type Ok = Structures;
790 type Error = Error;
791
792 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
793 where
794 T: ?Sized + ser::Serialize,
795 {
796 self.inner.serialize_field(key, value)
797 }
798
799 fn end(self) -> Result<Self::Ok> {
800 self.inner
801 .end()
802 .map(|expr| Attribute::new(self.ident, expr).into())
803 }
804}
805
806pub(crate) struct BlockSerializer {
807 ident: Identifier,
808}
809
810impl BlockSerializer {
811 fn new(ident: Identifier) -> Self {
812 BlockSerializer { ident }
813 }
814}
815
816impl ser::Serializer for BlockSerializer {
817 type Ok = Structures;
818 type Error = Error;
819
820 type SerializeSeq = SerializeBlockSeq;
821 type SerializeTuple = SerializeBlockSeq;
822 type SerializeTupleStruct = SerializeBlockSeq;
823 type SerializeTupleVariant = SerializeBlockTupleVariant;
824 type SerializeMap = SerializeBlockMap;
825 type SerializeStruct = SerializeBlockStruct;
826 type SerializeStructVariant = SerializeBlockStructVariant;
827
828 serialize_unsupported! {
829 bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64
830 char str bytes none unit unit_struct unit_variant
831 }
832 serialize_self! { some }
833 forward_to_serialize_seq! { tuple tuple_struct }
834
835 fn serialize_newtype_struct<T>(self, name: &'static str, value: &T) -> Result<Self::Ok>
836 where
837 T: ?Sized + ser::Serialize,
838 {
839 if name == LABELED_BLOCK_MARKER {
840 LabeledBlockSerializer::new(self.ident).serialize_newtype_struct(name, value)
841 } else {
842 value.serialize(self)
843 }
844 }
845
846 fn serialize_newtype_variant<T>(
847 self,
848 _name: &'static str,
849 _variant_index: u32,
850 variant: &'static str,
851 value: &T,
852 ) -> Result<Self::Ok>
853 where
854 T: ?Sized + Serialize,
855 {
856 let variant = Identifier::new(variant)?;
857
858 Ok(Block::builder(self.ident)
859 .add_structures(value.serialize(StructureSerializer::new(variant))?)
860 .build()
861 .into())
862 }
863
864 fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
865 Ok(SerializeBlockSeq::new(self.ident, len))
866 }
867
868 fn serialize_tuple_variant(
869 self,
870 _name: &'static str,
871 _variant_index: u32,
872 variant: &'static str,
873 len: usize,
874 ) -> Result<Self::SerializeTupleVariant> {
875 Ok(SerializeBlockTupleVariant::new(
876 self.ident,
877 Identifier::new(variant)?,
878 len,
879 ))
880 }
881
882 fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
883 Ok(SerializeBlockMap::new(self.ident, len))
884 }
885
886 fn serialize_struct(self, _name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
887 Ok(SerializeBlockStruct::new(self.ident, len))
888 }
889
890 fn serialize_struct_variant(
891 self,
892 _name: &'static str,
893 _variant_index: u32,
894 variant: &'static str,
895 len: usize,
896 ) -> Result<Self::SerializeStructVariant> {
897 Ok(SerializeBlockStructVariant::new(
898 self.ident,
899 Identifier::new(variant)?,
900 len,
901 ))
902 }
903}
904
905pub(crate) struct SerializeBlockSeq {
906 ident: Identifier,
907 structures: Vec<Structure>,
908}
909
910impl SerializeBlockSeq {
911 fn new(ident: Identifier, len: Option<usize>) -> Self {
912 SerializeBlockSeq {
913 ident,
914 structures: Vec::with_capacity(len.unwrap_or(0)),
915 }
916 }
917}
918
919impl ser::SerializeSeq for SerializeBlockSeq {
920 type Ok = Structures;
921 type Error = Error;
922
923 fn serialize_element<T>(&mut self, value: &T) -> Result<()>
924 where
925 T: ?Sized + ser::Serialize,
926 {
927 self.structures
928 .extend(value.serialize(BlockSerializer::new(self.ident.clone()))?);
929 Ok(())
930 }
931
932 fn end(self) -> Result<Self::Ok> {
933 Ok(self.structures.into())
934 }
935}
936
937impl ser::SerializeTuple for SerializeBlockSeq {
938 impl_forward_to_serialize_seq!(serialize_element, Structures);
939}
940
941impl ser::SerializeTupleStruct for SerializeBlockSeq {
942 impl_forward_to_serialize_seq!(serialize_field, Structures);
943}
944
945pub(crate) struct SerializeBlockTupleVariant {
946 ident: Identifier,
947 variant: Identifier,
948 structures: Vec<Structure>,
949}
950
951impl SerializeBlockTupleVariant {
952 fn new(ident: Identifier, variant: Identifier, len: usize) -> Self {
953 SerializeBlockTupleVariant {
954 ident,
955 variant,
956 structures: Vec::with_capacity(len),
957 }
958 }
959}
960
961impl ser::SerializeTupleVariant for SerializeBlockTupleVariant {
962 type Ok = Structures;
963 type Error = Error;
964
965 fn serialize_field<T>(&mut self, value: &T) -> Result<()>
966 where
967 T: ?Sized + ser::Serialize,
968 {
969 self.structures
970 .extend(value.serialize(BlockSerializer::new(self.variant.clone()))?);
971 Ok(())
972 }
973
974 fn end(self) -> Result<Self::Ok> {
975 Ok(Block::builder(self.ident)
976 .add_structures(self.structures)
977 .build()
978 .into())
979 }
980}
981
982pub(crate) struct SerializeBlockMap {
983 ident: Identifier,
984 next_key: Option<Identifier>,
985 structures: Vec<Structure>,
986}
987
988impl SerializeBlockMap {
989 fn new(ident: Identifier, len: Option<usize>) -> Self {
990 SerializeBlockMap {
991 ident,
992 next_key: None,
993 structures: Vec::with_capacity(len.unwrap_or(0)),
994 }
995 }
996}
997
998impl ser::SerializeMap for SerializeBlockMap {
999 type Ok = Structures;
1000 type Error = Error;
1001
1002 fn serialize_key<T>(&mut self, key: &T) -> Result<()>
1003 where
1004 T: ?Sized + ser::Serialize,
1005 {
1006 self.next_key = Some(key.serialize(IdentifierSerializer)?);
1007 Ok(())
1008 }
1009
1010 fn serialize_value<T>(&mut self, value: &T) -> Result<()>
1011 where
1012 T: ?Sized + ser::Serialize,
1013 {
1014 let key = self.next_key.take();
1015 let key = key.expect("serialize_value called before serialize_key");
1016
1017 self.structures
1018 .extend(value.serialize(StructureSerializer::new(key))?);
1019 Ok(())
1020 }
1021
1022 fn end(self) -> Result<Self::Ok> {
1023 Ok(Block::builder(self.ident)
1024 .add_structures(self.structures)
1025 .build()
1026 .into())
1027 }
1028}
1029
1030pub(crate) struct SerializeBlockStruct {
1031 ident: Identifier,
1032 structures: Vec<Structure>,
1033}
1034
1035impl SerializeBlockStruct {
1036 fn new(ident: Identifier, len: usize) -> Self {
1037 SerializeBlockStruct {
1038 ident,
1039 structures: Vec::with_capacity(len),
1040 }
1041 }
1042}
1043
1044impl ser::SerializeStruct for SerializeBlockStruct {
1045 type Ok = Structures;
1046 type Error = Error;
1047
1048 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
1049 where
1050 T: ?Sized + ser::Serialize,
1051 {
1052 let ident = Identifier::new(key)?;
1053
1054 self.structures
1055 .extend(value.serialize(StructureSerializer::new(ident))?);
1056 Ok(())
1057 }
1058
1059 fn end(self) -> Result<Self::Ok> {
1060 Ok(Block::builder(self.ident)
1061 .add_structures(self.structures)
1062 .build()
1063 .into())
1064 }
1065}
1066
1067pub(crate) struct SerializeBlockStructVariant {
1068 ident: Identifier,
1069 variant: Identifier,
1070 structures: Vec<Structure>,
1071}
1072
1073impl SerializeBlockStructVariant {
1074 fn new(ident: Identifier, variant: Identifier, len: usize) -> Self {
1075 SerializeBlockStructVariant {
1076 ident,
1077 variant,
1078 structures: Vec::with_capacity(len),
1079 }
1080 }
1081}
1082
1083impl ser::SerializeStructVariant for SerializeBlockStructVariant {
1084 type Ok = Structures;
1085 type Error = Error;
1086
1087 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
1088 where
1089 T: ?Sized + ser::Serialize,
1090 {
1091 let ident = Identifier::new(key)?;
1092
1093 self.structures
1094 .extend(value.serialize(StructureSerializer::new(ident))?);
1095 Ok(())
1096 }
1097
1098 fn end(self) -> Result<Self::Ok> {
1099 Ok(Block::builder(self.ident)
1100 .add_block(
1101 Block::builder(self.variant)
1102 .add_structures(self.structures)
1103 .build(),
1104 )
1105 .build()
1106 .into())
1107 }
1108}
1109
1110pub(crate) struct LabeledBlockSerializer {
1111 ident: Identifier,
1112}
1113
1114impl LabeledBlockSerializer {
1115 fn new(ident: Identifier) -> Self {
1116 LabeledBlockSerializer { ident }
1117 }
1118}
1119
1120impl ser::Serializer for LabeledBlockSerializer {
1121 type Ok = Structures;
1122 type Error = Error;
1123
1124 type SerializeSeq = SerializeLabeledBlockSeq;
1125 type SerializeTuple = SerializeLabeledBlockSeq;
1126 type SerializeTupleStruct = SerializeLabeledBlockSeq;
1127 type SerializeTupleVariant = SerializeLabeledBlockTupleVariant;
1128 type SerializeMap = SerializeLabeledBlockMap;
1129 type SerializeStruct = SerializeLabeledBlockStruct;
1130 type SerializeStructVariant = SerializeLabeledBlockStructVariant;
1131
1132 serialize_unsupported! {
1133 bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64
1134 char str bytes none unit unit_struct unit_variant
1135 }
1136 serialize_self! { some newtype_struct }
1137 forward_to_serialize_seq! { tuple tuple_struct }
1138
1139 fn serialize_newtype_variant<T>(
1140 self,
1141 _name: &'static str,
1142 _variant_index: u32,
1143 variant: &'static str,
1144 value: &T,
1145 ) -> Result<Self::Ok>
1146 where
1147 T: ?Sized + Serialize,
1148 {
1149 let mut structures = Vec::with_capacity(1);
1150
1151 serialize_labeled_blocks(self.ident, value, &mut structures, |labels| {
1152 labels.insert(0, variant.into());
1153 })?;
1154
1155 Ok(structures.into())
1156 }
1157
1158 fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
1159 Ok(SerializeLabeledBlockSeq::new(self.ident, len))
1160 }
1161
1162 fn serialize_tuple_variant(
1163 self,
1164 _name: &'static str,
1165 _variant_index: u32,
1166 variant: &'static str,
1167 len: usize,
1168 ) -> Result<Self::SerializeTupleVariant> {
1169 Ok(SerializeLabeledBlockTupleVariant::new(
1170 self.ident, variant, len,
1171 ))
1172 }
1173
1174 fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
1175 Ok(SerializeLabeledBlockMap::new(self.ident, len))
1176 }
1177
1178 fn serialize_struct(self, _name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
1179 Ok(SerializeLabeledBlockStruct::new(self.ident, len))
1180 }
1181
1182 fn serialize_struct_variant(
1183 self,
1184 _name: &'static str,
1185 _variant_index: u32,
1186 variant: &'static str,
1187 len: usize,
1188 ) -> Result<Self::SerializeStructVariant> {
1189 Ok(SerializeLabeledBlockStructVariant::new(
1190 self.ident, variant, len,
1191 ))
1192 }
1193}
1194
1195pub(crate) struct SerializeLabeledBlockSeq {
1196 ident: Identifier,
1197 structures: Vec<Structure>,
1198}
1199
1200impl SerializeLabeledBlockSeq {
1201 fn new(ident: Identifier, len: Option<usize>) -> Self {
1202 SerializeLabeledBlockSeq {
1203 ident,
1204 structures: Vec::with_capacity(len.unwrap_or(0)),
1205 }
1206 }
1207}
1208
1209impl ser::SerializeSeq for SerializeLabeledBlockSeq {
1210 type Ok = Structures;
1211 type Error = Error;
1212
1213 fn serialize_element<T>(&mut self, value: &T) -> Result<()>
1214 where
1215 T: ?Sized + ser::Serialize,
1216 {
1217 self.structures
1218 .extend(value.serialize(LabeledBlockSerializer::new(self.ident.clone()))?);
1219 Ok(())
1220 }
1221
1222 fn end(self) -> Result<Self::Ok> {
1223 Ok(self.structures.into())
1224 }
1225}
1226
1227impl ser::SerializeTuple for SerializeLabeledBlockSeq {
1228 impl_forward_to_serialize_seq!(serialize_element, Structures);
1229}
1230
1231impl ser::SerializeTupleStruct for SerializeLabeledBlockSeq {
1232 impl_forward_to_serialize_seq!(serialize_field, Structures);
1233}
1234
1235pub(crate) struct SerializeLabeledBlockTupleVariant {
1236 ident: Identifier,
1237 variant: &'static str,
1238 structures: Vec<Structure>,
1239}
1240
1241impl SerializeLabeledBlockTupleVariant {
1242 fn new(ident: Identifier, variant: &'static str, len: usize) -> Self {
1243 SerializeLabeledBlockTupleVariant {
1244 ident,
1245 variant,
1246 structures: Vec::with_capacity(len),
1247 }
1248 }
1249}
1250
1251impl ser::SerializeTupleVariant for SerializeLabeledBlockTupleVariant {
1252 type Ok = Structures;
1253 type Error = Error;
1254
1255 fn serialize_field<T>(&mut self, value: &T) -> Result<()>
1256 where
1257 T: ?Sized + ser::Serialize,
1258 {
1259 serialize_labeled_blocks(self.ident.clone(), value, &mut self.structures, |labels| {
1260 labels.insert(0, self.variant.into());
1261 })
1262 }
1263
1264 fn end(self) -> Result<Self::Ok> {
1265 Ok(self.structures.into())
1266 }
1267}
1268
1269pub(crate) struct SerializeLabeledBlockMap {
1270 ident: Identifier,
1271 next_key: Option<String>,
1272 structures: Vec<Structure>,
1273}
1274
1275impl SerializeLabeledBlockMap {
1276 fn new(ident: Identifier, len: Option<usize>) -> Self {
1277 SerializeLabeledBlockMap {
1278 ident,
1279 next_key: None,
1280 structures: Vec::with_capacity(len.unwrap_or(0)),
1281 }
1282 }
1283}
1284
1285impl ser::SerializeMap for SerializeLabeledBlockMap {
1286 type Ok = Structures;
1287 type Error = Error;
1288
1289 fn serialize_key<T>(&mut self, key: &T) -> Result<()>
1290 where
1291 T: ?Sized + ser::Serialize,
1292 {
1293 self.next_key = Some(key.serialize(StringSerializer)?);
1294 Ok(())
1295 }
1296
1297 fn serialize_value<T>(&mut self, value: &T) -> Result<()>
1298 where
1299 T: ?Sized + ser::Serialize,
1300 {
1301 let key = self.next_key.take();
1302 let key = key.expect("serialize_value called before serialize_key");
1303
1304 serialize_labeled_blocks(self.ident.clone(), value, &mut self.structures, |labels| {
1305 labels.insert(0, BlockLabel::from(&key));
1306 })
1307 }
1308
1309 fn end(self) -> Result<Self::Ok> {
1310 Ok(self.structures.into())
1311 }
1312}
1313
1314pub(crate) struct SerializeLabeledBlockStruct {
1315 ident: Identifier,
1316 structures: Vec<Structure>,
1317}
1318
1319impl SerializeLabeledBlockStruct {
1320 fn new(ident: Identifier, len: usize) -> Self {
1321 SerializeLabeledBlockStruct {
1322 ident,
1323 structures: Vec::with_capacity(len),
1324 }
1325 }
1326}
1327
1328impl ser::SerializeStruct for SerializeLabeledBlockStruct {
1329 type Ok = Structures;
1330 type Error = Error;
1331
1332 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
1333 where
1334 T: ?Sized + ser::Serialize,
1335 {
1336 serialize_labeled_blocks(self.ident.clone(), value, &mut self.structures, |labels| {
1337 labels.insert(0, key.into());
1338 })
1339 }
1340
1341 fn end(self) -> Result<Self::Ok> {
1342 Ok(self.structures.into())
1343 }
1344}
1345
1346pub(crate) struct SerializeLabeledBlockStructVariant {
1347 ident: Identifier,
1348 variant: &'static str,
1349 structures: Vec<Structure>,
1350}
1351
1352impl SerializeLabeledBlockStructVariant {
1353 fn new(ident: Identifier, variant: &'static str, len: usize) -> Self {
1354 SerializeLabeledBlockStructVariant {
1355 ident,
1356 variant,
1357 structures: Vec::with_capacity(len),
1358 }
1359 }
1360}
1361
1362impl ser::SerializeStructVariant for SerializeLabeledBlockStructVariant {
1363 type Ok = Structures;
1364 type Error = Error;
1365
1366 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
1367 where
1368 T: ?Sized + ser::Serialize,
1369 {
1370 serialize_labeled_blocks(self.ident.clone(), value, &mut self.structures, |labels| {
1371 labels.insert(0, self.variant.into());
1372 labels.insert(1, key.into());
1373 })
1374 }
1375
1376 fn end(self) -> Result<Self::Ok> {
1377 Ok(self.structures.into())
1378 }
1379}
1380
1381fn serialize_labeled_blocks<T, F>(
1382 ident: Identifier,
1383 value: &T,
1384 structures: &mut Vec<Structure>,
1385 f: F,
1386) -> Result<()>
1387where
1388 T: ?Sized + Serialize,
1389 F: Fn(&mut Vec<BlockLabel>),
1390{
1391 value
1392 .serialize(BlockSerializer::new(ident))?
1393 .into_iter()
1394 .filter_map(Structure::into_block)
1395 .for_each(|mut block| {
1396 f(&mut block.labels);
1397 structures.push(block.into());
1398 });
1399
1400 Ok(())
1401}