hcl_edit/structure/
body.rs

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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
use crate::encode::{EncodeDecorated, EncodeState, NO_DECOR};
use crate::structure::{Attribute, AttributeMut, Block, Structure, StructureMut};
use crate::{parser, Decor};
use std::fmt;
use std::ops::Range;
use std::str::FromStr;

/// An owning iterator over the elements of a `Body`.
///
/// Values of this type are created by the [`into_iter`] method on [`Body`] (provided by the
/// [`IntoIterator`] trait). See its documentation for more.
///
/// [`into_iter`]: IntoIterator::into_iter
/// [`IntoIterator`]: core::iter::IntoIterator
pub type IntoIter = Box<dyn Iterator<Item = Structure>>;

/// An iterator over the elements of a `Body`.
///
/// Values of this type are created by the [`iter`] method on [`Body`]. See its documentation
/// for more.
///
/// [`iter`]: Body::iter
pub type Iter<'a> = Box<dyn Iterator<Item = &'a Structure> + 'a>;

/// A mutable iterator over the elements of a `Body`.
///
/// Values of this type are created by the [`iter_mut`] method on [`Body`]. See its
/// documentation for more.
///
/// [`iter_mut`]: Body::iter_mut
pub type IterMut<'a> = Box<dyn Iterator<Item = StructureMut<'a>> + 'a>;

/// An owning iterator over the `Attribute`s within a `Body`.
///
/// Values of this type are created by the [`into_attributes`] method on [`Body`]. See its
/// documentation for more.
///
/// [`into_attributes`]: Body::into_attributes
pub type IntoAttributes = Box<dyn Iterator<Item = Attribute>>;

/// An iterator over the `Attribute`s within a `Body`.
///
/// Values of this type are created by the [`attributes`] method on [`Body`]. See its documentation
/// for more.
///
/// [`attributes`]: Body::attributes
pub type Attributes<'a> = Box<dyn Iterator<Item = &'a Attribute> + 'a>;

/// A mutable iterator over the `Attribute`s within a `Body`.
///
/// Values of this type are created by the [`attributes_mut`] method on [`Body`]. See its
/// documentation for more.
///
/// [`attributes_mut`]: Body::attributes_mut
pub type AttributesMut<'a> = Box<dyn Iterator<Item = AttributeMut<'a>> + 'a>;

/// An owning iterator over the `Block`s within a `Body`.
///
/// Values of this type are created by the [`into_blocks`] method on [`Body`]. See its
/// documentation for more.
///
/// [`into_blocks`]: Body::into_blocks
pub type IntoBlocks = Box<dyn Iterator<Item = Block>>;

/// An iterator over the `Block`s within a `Body`.
///
/// Values of this type are created by the [`blocks`] method on [`Body`]. See its documentation
/// for more.
///
/// [`blocks`]: Body::blocks
pub type Blocks<'a> = Box<dyn Iterator<Item = &'a Block> + 'a>;

/// A mutable iterator over the `Block`s within a `Body`.
///
/// Values of this type are created by the [`blocks_mut`] method on [`Body`]. See its
/// documentation for more.
///
/// [`blocks_mut`]: Body::blocks_mut
pub type BlocksMut<'a> = Box<dyn Iterator<Item = &'a mut Block> + 'a>;

/// Represents an HCL config file body.
///
/// A `Body` consists of zero or more [`Attribute`] and [`Block`] HCL structures.
#[derive(Debug, Clone, Default, Eq)]
pub struct Body {
    structures: Vec<Structure>,
    prefer_oneline: bool,
    prefer_omit_trailing_newline: bool,
    decor: Decor,
    span: Option<Range<usize>>,
}

impl Body {
    /// Constructs a new, empty `Body`.
    #[inline]
    pub fn new() -> Self {
        Body::default()
    }

    /// Constructs a new, empty `Body` with at least the specified capacity.
    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        Body {
            structures: Vec::with_capacity(capacity),
            ..Default::default()
        }
    }

    /// Creates a new [`BodyBuilder`] to start building a new `Body`.
    #[inline]
    pub fn builder() -> BodyBuilder {
        BodyBuilder::default()
    }

    /// Returns `true` if the body contains no structures.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.structures.is_empty()
    }

    /// Returns the number of structures in the body, also referred to as its 'length'.
    #[inline]
    pub fn len(&self) -> usize {
        self.structures.len()
    }

    /// Clears the body, removing all structures.
    #[inline]
    pub fn clear(&mut self) {
        self.structures.clear();
    }

    /// Returns a reference to the structure at the given index, or `None` if the index is out of
    /// bounds.
    #[inline]
    pub fn get(&self, index: usize) -> Option<&Structure> {
        self.structures.get(index)
    }

    /// Returns a mutable reference to the structure at the given index, or `None` if the index is
    /// out of bounds.
    #[inline]
    pub fn get_mut(&mut self, index: usize) -> Option<&mut Structure> {
        self.structures.get_mut(index)
    }

    /// Returns `true` if the body contains an attribute with given key.
    ///
    /// # Example
    ///
    /// ```
    /// use hcl_edit::structure::{Attribute, Body};
    /// use hcl_edit::Ident;
    ///
    /// let body = Body::from_iter([Attribute::new(Ident::new("foo"), "bar")]);
    /// assert!(body.has_attribute("foo"));
    /// assert!(!body.has_attribute("bar"));
    /// ```
    #[inline]
    pub fn has_attribute(&self, key: &str) -> bool {
        self.get_attribute(key).is_some()
    }

    /// Returns `true` if the body contains blocks with given identifier.
    ///
    /// # Example
    ///
    /// ```
    /// use hcl_edit::structure::{Block, Body};
    /// use hcl_edit::Ident;
    ///
    /// let body = Body::from_iter([Block::new(Ident::new("foo"))]);
    /// assert!(body.has_blocks("foo"));
    /// assert!(!body.has_blocks("bar"));
    /// ```
    #[inline]
    pub fn has_blocks(&self, ident: &str) -> bool {
        self.get_blocks(ident).next().is_some()
    }

    /// Returns a reference to the `Attribute` with given key if it exists, otherwise `None`.
    ///
    /// # Example
    ///
    /// ```
    /// use hcl_edit::structure::{Attribute, Body};
    /// use hcl_edit::Ident;
    ///
    /// let mut body = Body::new();
    ///
    /// assert!(body.get_attribute("foo").is_none());
    ///
    /// let foo = Attribute::new(Ident::new("foo"), "bar");
    ///
    /// body.push(foo.clone());
    ///
    /// assert_eq!(body.get_attribute("foo"), Some(&foo));
    /// ```
    pub fn get_attribute(&self, key: &str) -> Option<&Attribute> {
        self.structures
            .iter()
            .filter_map(Structure::as_attribute)
            .find(|attr| attr.has_key(key))
    }

    /// Returns a mutable reference to the `Attribute` with given key if it exists, otherwise
    /// `None`.
    ///
    /// # Example
    ///
    /// ```
    /// use hcl_edit::expr::Expression;
    /// use hcl_edit::structure::{Attribute, Body};
    /// use hcl_edit::Ident;
    ///
    /// let mut body = Body::new();
    ///
    /// assert!(body.get_attribute("foo").is_none());
    ///
    /// let foo = Attribute::new(Ident::new("foo"), "bar");
    ///
    /// body.push(foo.clone());
    ///
    /// if let Some(mut attr) = body.get_attribute_mut("foo") {
    ///     *attr.value_mut() = Expression::from("baz");
    /// }
    ///
    /// assert_eq!(body.get_attribute("foo"), Some(&Attribute::new(Ident::new("foo"), "baz")));
    /// ```
    pub fn get_attribute_mut(&mut self, key: &str) -> Option<AttributeMut<'_>> {
        self.structures
            .iter_mut()
            .filter_map(Structure::as_attribute_mut)
            .find(|attr| attr.has_key(key))
            .map(AttributeMut::new)
    }

    /// Returns an iterator visiting all `Block`s with the given identifier. The iterator element
    /// type is `&'a Block`.
    ///
    /// # Example
    ///
    /// ```
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use hcl_edit::structure::Body;
    ///
    /// let input = r#"
    /// resource "aws_s3_bucket" "bucket" {}
    ///
    /// variable "name" {}
    ///
    /// resource "aws_instance" "instance" {}
    /// "#;
    ///
    /// let body: Body = input.parse()?;
    ///
    /// let resources: Body = body.get_blocks("resource").cloned().collect();
    ///
    /// let expected = r#"
    /// resource "aws_s3_bucket" "bucket" {}
    ///
    /// resource "aws_instance" "instance" {}
    /// "#;
    ///
    /// assert_eq!(resources.to_string(), expected);
    /// #   Ok(())
    /// # }
    /// ```
    pub fn get_blocks<'a>(&'a self, ident: &'a str) -> Blocks<'a> {
        Box::new(
            self.structures
                .iter()
                .filter_map(Structure::as_block)
                .filter(|block| block.has_ident(ident)),
        )
    }

    /// Returns an iterator visiting all `Block`s with the given identifier. The iterator element
    /// type is `&'a mut Block`.
    ///
    /// # Example
    ///
    /// ```
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use hcl_edit::expr::{Traversal, TraversalOperator};
    /// use hcl_edit::structure::{Attribute, Body};
    /// use hcl_edit::Ident;
    ///
    /// let input = r#"
    /// resource "aws_s3_bucket" "bucket" {}
    ///
    /// variable "name" {}
    ///
    /// resource "aws_db_instance" "db_instance" {}
    /// "#;
    ///
    /// let mut body: Body = input.parse()?;
    ///
    /// for block in body.get_blocks_mut("resource") {
    ///     let operators = vec![TraversalOperator::GetAttr(Ident::new("name").into()).into()];
    ///     let value = Traversal::new(Ident::new("var"), operators);
    ///     block.body.push(Attribute::new(Ident::new("name"), value));
    /// }
    ///
    /// let expected = r#"
    /// resource "aws_s3_bucket" "bucket" { name = var.name }
    ///
    /// variable "name" {}
    ///
    /// resource "aws_db_instance" "db_instance" { name = var.name }
    /// "#;
    ///
    /// assert_eq!(body.to_string(), expected);
    /// #   Ok(())
    /// # }
    /// ```
    pub fn get_blocks_mut<'a>(&'a mut self, ident: &'a str) -> BlocksMut<'a> {
        Box::new(
            self.structures
                .iter_mut()
                .filter_map(Structure::as_block_mut)
                .filter(|block| block.has_ident(ident)),
        )
    }

    /// Inserts a structure at position `index` within the body, shifting all structures after it
    /// to the right.
    ///
    /// If it is attempted to insert an `Attribute` which already exists in the body, it is ignored
    /// and not inserted. For a fallible variant of this function see [`Body::try_insert`].
    ///
    /// # Panics
    ///
    /// Panics if `index > len`.
    #[inline]
    pub fn insert(&mut self, index: usize, structure: impl Into<Structure>) {
        _ = self.try_insert(index, structure);
    }

    /// Inserts a structure at position `index` within the body, shifting all structures after it
    /// to the right.
    ///
    /// # Errors
    ///
    /// If it is attempted to insert an `Attribute` which already exists in the body, it is not
    /// inserted and returned as the `Result`'s `Err` variant instead.
    ///
    /// # Panics
    ///
    /// Panics if `index > len`.
    ///
    /// # Example
    ///
    /// ```
    /// use hcl_edit::structure::{Attribute, Body};
    /// use hcl_edit::Ident;
    ///
    /// let mut body = Body::new();
    ///
    /// body.push(Attribute::new(Ident::new("foo"), "bar"));
    /// assert!(body.try_insert(0, Attribute::new(Ident::new("bar"), "baz")).is_ok());
    /// assert_eq!(body.len(), 2);
    ///
    /// let duplicate_attr = Attribute::new(Ident::new("foo"), "baz");
    ///
    /// assert_eq!(body.try_insert(0, duplicate_attr.clone()), Err(duplicate_attr));
    /// ```
    #[inline]
    pub fn try_insert(
        &mut self,
        index: usize,
        structure: impl Into<Structure>,
    ) -> Result<(), Attribute> {
        match structure.into() {
            Structure::Attribute(attr) if self.has_attribute(&attr.key) => Err(attr),
            structure => {
                self.structures.insert(index, structure);
                Ok(())
            }
        }
    }

    /// Appends a structure to the back of the body.
    ///
    /// If it is attempted to append an `Attribute` which already exists in the body, it is ignored
    /// and not appended. For a fallible variant of this function see [`Body::try_push`].
    ///
    /// # Panics
    ///
    /// Panics if the new capacity exceeds `isize::MAX` bytes.
    #[inline]
    pub fn push(&mut self, structure: impl Into<Structure>) {
        _ = self.try_push(structure);
    }

    /// Appends a structure to the back of the body.
    ///
    /// # Errors
    ///
    /// If it is attempted to append an `Attribute` which already exists in the body, it is not
    /// appended and returned as the `Result`'s `Err` variant instead.
    ///
    /// # Panics
    ///
    /// Panics if the new capacity exceeds `isize::MAX` bytes.
    ///
    /// # Example
    ///
    /// ```
    /// use hcl_edit::structure::{Attribute, Body};
    /// use hcl_edit::Ident;
    ///
    /// let mut body = Body::new();
    ///
    /// assert!(body.try_push(Attribute::new(Ident::new("foo"), "bar")).is_ok());
    /// assert!(body.try_push(Attribute::new(Ident::new("bar"), "baz")).is_ok());
    /// assert_eq!(body.len(), 2);
    ///
    /// let duplicate_attr = Attribute::new(Ident::new("foo"), "baz");
    ///
    /// assert_eq!(body.try_push(duplicate_attr.clone()), Err(duplicate_attr));
    /// ```
    #[inline]
    pub fn try_push(&mut self, structure: impl Into<Structure>) -> Result<(), Attribute> {
        match structure.into() {
            Structure::Attribute(attr) if self.has_attribute(&attr.key) => Err(attr),
            structure => {
                self.structures.push(structure);
                Ok(())
            }
        }
    }

    /// Removes the last structure from the body and returns it, or [`None`] if it is empty.
    #[inline]
    pub fn pop(&mut self) -> Option<Structure> {
        self.structures.pop()
    }

    /// Removes and returns the structure at position `index` within the body, shifting all
    /// elements after it to the left.
    ///
    /// Like `Vec::remove`, the structure is removed by shifting all of the structures that follow
    /// it, preserving their relative order. **This perturbs the index of all of those elements!**
    ///
    /// # Panics
    ///
    /// Panics if `index` is out of bounds.
    #[inline]
    pub fn remove(&mut self, index: usize) -> Structure {
        self.structures.remove(index)
    }

    /// Removes and returns the attribute with given `key`.
    ///
    /// # Example
    ///
    /// ```
    /// use hcl_edit::structure::{Attribute, Block, Body};
    /// use hcl_edit::Ident;
    ///
    /// let mut body = Body::new();
    /// body.push(Block::new(Ident::new("block")));
    ///
    /// assert!(body.remove_attribute("foo").is_none());
    ///
    /// let foo = Attribute::new(Ident::new("foo"), "bar");
    ///
    /// body.push(foo.clone());
    ///
    /// assert_eq!(body.len(), 2);
    /// assert_eq!(body.remove_attribute("foo"), Some(foo));
    /// assert_eq!(body.len(), 1);
    /// ```
    pub fn remove_attribute(&mut self, key: &str) -> Option<Attribute> {
        self.structures
            .iter()
            .position(|structure| {
                structure
                    .as_attribute()
                    .map_or(false, |attr| attr.has_key(key))
            })
            .and_then(|index| self.remove(index).into_attribute().ok())
    }

    /// Removes and returns all blocks with given `ident`.
    ///
    /// # Example
    ///
    /// ```
    /// use hcl_edit::structure::{Attribute, Block, Body};
    /// use hcl_edit::Ident;
    ///
    /// let mut body = Body::builder()
    ///     .attribute(Attribute::new(Ident::new("foo"), "bar"))
    ///     .block(
    ///         Block::builder(Ident::new("resource"))
    ///             .labels(["aws_s3_bucket", "bucket"])
    ///     )
    ///     .block(Block::builder(Ident::new("variable")).label("name"))
    ///     .block(
    ///         Block::builder(Ident::new("resource"))
    ///             .labels(["aws_db_instance", "db_instance"])
    ///     )
    ///     .build();
    ///
    /// let resources = body.remove_blocks("resource");
    ///
    /// assert_eq!(
    ///     resources,
    ///     vec![
    ///         Block::builder(Ident::new("resource"))
    ///             .labels(["aws_s3_bucket", "bucket"])
    ///             .build(),
    ///         Block::builder(Ident::new("resource"))
    ///             .labels(["aws_db_instance", "db_instance"])
    ///             .build()
    ///     ]
    /// );
    ///
    /// assert_eq!(
    ///     body,
    ///     Body::builder()
    ///         .attribute(Attribute::new(Ident::new("foo"), "bar"))
    ///         .block(Block::builder(Ident::new("variable")).label("name"))
    ///         .build()
    /// );
    /// ```
    pub fn remove_blocks(&mut self, ident: &str) -> Vec<Block> {
        let mut removed = Vec::new();

        while let Some(block) = self.remove_block(ident) {
            removed.push(block);
        }

        removed
    }

    fn remove_block(&mut self, ident: &str) -> Option<Block> {
        self.structures
            .iter()
            .position(|structure| {
                structure
                    .as_block()
                    .map_or(false, |block| block.has_ident(ident))
            })
            .and_then(|index| self.remove(index).into_block().ok())
    }

    /// An iterator visiting all body structures in insertion order. The iterator element type is
    /// `&'a Structure`.
    #[inline]
    pub fn iter(&self) -> Iter<'_> {
        Box::new(self.structures.iter())
    }

    /// An iterator visiting all body structures in insertion order, with mutable references to the
    /// values. The iterator element type is `StructureMut<'a>`.
    #[inline]
    pub fn iter_mut(&mut self) -> IterMut<'_> {
        Box::new(self.structures.iter_mut().map(StructureMut::new))
    }

    /// An owning iterator visiting all `Attribute`s within the body in insertion order. The
    /// iterator element type is `Attribute`.
    #[inline]
    pub fn into_attributes(self) -> IntoAttributes {
        Box::new(
            self.structures
                .into_iter()
                .filter_map(|s| s.into_attribute().ok()),
        )
    }

    /// An iterator visiting all `Attribute`s within the body in insertion order. The iterator
    /// element type is `&'a Attribute`.
    #[inline]
    pub fn attributes(&self) -> Attributes<'_> {
        Box::new(self.structures.iter().filter_map(Structure::as_attribute))
    }

    /// An iterator visiting all `Attribute`s within the body in insertion order, with mutable
    /// references to the values. The iterator element type is `AttributeMut<'a>`.
    #[inline]
    pub fn attributes_mut(&mut self) -> AttributesMut<'_> {
        Box::new(
            self.structures
                .iter_mut()
                .filter_map(Structure::as_attribute_mut)
                .map(AttributeMut::new),
        )
    }

    /// An owning iterator visiting all `Block`s within the body in insertion order. The iterator
    /// element type is `Block`.
    #[inline]
    pub fn into_blocks(self) -> IntoBlocks {
        Box::new(
            self.structures
                .into_iter()
                .filter_map(|s| s.into_block().ok()),
        )
    }

    /// An iterator visiting all `Block`s within the body in insertion order. The iterator element
    /// type is `&'a Block`.
    #[inline]
    pub fn blocks(&self) -> Blocks<'_> {
        Box::new(self.structures.iter().filter_map(Structure::as_block))
    }

    /// An iterator visiting all `Block`s within the body in insertion order, with mutable
    /// references to the values. The iterator element type is `&'a mut Block`.
    #[inline]
    pub fn blocks_mut(&mut self) -> BlocksMut<'_> {
        Box::new(
            self.structures
                .iter_mut()
                .filter_map(Structure::as_block_mut),
        )
    }

    /// Configures whether the body should be displayed on a single line.
    ///
    /// This is only a hint which will be applied if the `Body` is part of a `Block` (that is: not
    /// the document root) and only if either of these conditions meet:
    ///
    /// - The body is empty. In this case, the opening (`{`) and closing (`}`) braces will be
    ///   places on the same line.
    /// - The body only consist of a single `Attribute`, which will be placed on the same
    ///   line as the opening and closing braces.
    ///
    /// In all other cases this hint is ignored.
    #[inline]
    pub fn set_prefer_oneline(&mut self, yes: bool) {
        self.prefer_oneline = yes;
    }

    /// Returns `true` if the body should be displayed on a single line.
    ///
    /// See the documentation of [`Body::set_prefer_oneline`] for more.
    #[inline]
    pub fn prefer_oneline(&self) -> bool {
        self.prefer_oneline
    }

    /// Configures whether the trailing newline after the last structure in the body should be
    /// omitted.
    ///
    /// This is only a hint which will be applied if this is the top-level `Body` of a HCL
    /// document and is ignored if the `Body` is part of a [`Block`].
    ///
    /// The default is to always emit a trailing newline after the last body structure.
    #[inline]
    pub fn set_prefer_omit_trailing_newline(&mut self, yes: bool) {
        self.prefer_omit_trailing_newline = yes;
    }

    /// Returns `true` if the trailing newline after the last structure in the body should be
    /// omitted.
    ///
    /// See the documentation of [`Body::set_prefer_omit_trailing_newline`] for more.
    #[inline]
    pub fn prefer_omit_trailing_newline(&self) -> bool {
        self.prefer_omit_trailing_newline
    }

    /// Returns `true` if the body only consist of a single `Attribute`.
    #[inline]
    pub(crate) fn has_single_attribute(&self) -> bool {
        self.len() == 1 && self.get(0).map_or(false, Structure::is_attribute)
    }

    pub(crate) fn from_vec_unchecked(structures: Vec<Structure>) -> Self {
        Body {
            structures,
            ..Default::default()
        }
    }

    pub(crate) fn despan(&mut self, input: &str) {
        self.decor.despan(input);
        for structure in &mut self.structures {
            structure.despan(input);
        }
    }
}

impl PartialEq for Body {
    fn eq(&self, other: &Self) -> bool {
        self.structures == other.structures
    }
}

impl fmt::Display for Body {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut state = EncodeState::new(f);
        self.encode_decorated(&mut state, NO_DECOR)
    }
}

impl FromStr for Body {
    type Err = parser::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        parser::parse_body(s)
    }
}

impl From<Vec<Structure>> for Body {
    fn from(structures: Vec<Structure>) -> Self {
        Body::from_iter(structures)
    }
}

impl<T> Extend<T> for Body
where
    T: Into<Structure>,
{
    fn extend<I>(&mut self, iterable: I)
    where
        I: IntoIterator<Item = T>,
    {
        let iter = iterable.into_iter();
        let reserve = if self.is_empty() {
            iter.size_hint().0
        } else {
            (iter.size_hint().0 + 1) / 2
        };
        self.structures.reserve(reserve);
        iter.for_each(|v| self.push(v));
    }
}

impl<T> FromIterator<T> for Body
where
    T: Into<Structure>,
{
    fn from_iter<I>(iterable: I) -> Self
    where
        I: IntoIterator<Item = T>,
    {
        let iter = iterable.into_iter();
        let lower = iter.size_hint().0;
        let mut body = Body::with_capacity(lower);
        body.extend(iter);
        body
    }
}

impl IntoIterator for Body {
    type Item = Structure;
    type IntoIter = IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        Box::new(self.structures.into_iter())
    }
}

impl<'a> IntoIterator for &'a Body {
    type Item = &'a Structure;
    type IntoIter = Iter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a> IntoIterator for &'a mut Body {
    type Item = StructureMut<'a>;
    type IntoIter = IterMut<'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

decorate_impl!(Body);
span_impl!(Body);

/// `BodyBuilder` builds a HCL [`Body`].
///
/// The builder allows to build the `Body` by adding attributes and other nested blocks via chained
/// method calls. A call to [`.build()`](BodyBuilder::build) produces the final `Body`.
///
/// ## Example
///
/// ```
/// use hcl_edit::structure::{Attribute, Block, Body};
/// use hcl_edit::Ident;
///
/// let body = Body::builder()
///     .block(
///         Block::builder(Ident::new("resource"))
///             .labels(["aws_s3_bucket", "mybucket"])
///             .attribute(Attribute::new(Ident::new("name"), "mybucket"))
///     )
///     .build();
/// ```
#[derive(Debug, Default)]
pub struct BodyBuilder {
    body: Body,
}

impl BodyBuilder {
    /// Adds an `Attribute` to the body.
    ///
    /// Consumes `self` and returns a new `BodyBuilder`.
    #[inline]
    pub fn attribute(self, attr: impl Into<Attribute>) -> BodyBuilder {
        self.structure(attr.into())
    }

    /// Adds `Attribute`s to the body from an iterator.
    ///
    /// Consumes `self` and returns a new `BodyBuilder`.
    #[inline]
    pub fn attributes<I>(self, iter: I) -> BodyBuilder
    where
        I: IntoIterator,
        I::Item: Into<Attribute>,
    {
        self.structures(iter.into_iter().map(Into::into))
    }

    /// Adds a `Block` to the body.
    ///
    /// Consumes `self` and returns a new `BodyBuilder`.
    #[inline]
    pub fn block(self, block: impl Into<Block>) -> BodyBuilder {
        self.structure(block.into())
    }

    /// Adds `Block`s to the body from an iterator.
    ///
    /// Consumes `self` and returns a new `BodyBuilder`.
    #[inline]
    pub fn blocks<I>(self, iter: I) -> BodyBuilder
    where
        I: IntoIterator,
        I::Item: Into<Block>,
    {
        self.structures(iter.into_iter().map(Into::into))
    }

    /// Adds a `Structure` to the body.
    ///
    /// Consumes `self` and returns a new `BodyBuilder`.
    #[inline]
    pub fn structure(mut self, structure: impl Into<Structure>) -> BodyBuilder {
        self.body.push(structure.into());
        self
    }

    /// Adds `Structure`s to the body from an iterator.
    ///
    /// Consumes `self` and returns a new `BodyBuilder`.
    #[inline]
    pub fn structures<I>(mut self, iter: I) -> BodyBuilder
    where
        I: IntoIterator,
        I::Item: Into<Structure>,
    {
        self.body.extend(iter);
        self
    }

    /// Consumes `self` and builds the [`Body`] from the structures added via the builder methods.
    #[inline]
    pub fn build(self) -> Body {
        self.body
    }
}

impl From<BodyBuilder> for Body {
    #[inline]
    fn from(builder: BodyBuilder) -> Self {
        builder.build()
    }
}