pub struct Template { /* private fields */ }
Expand description
The main type to represent the HCL template sub-languange.
A template behaves like an expression that always returns a string value. The different elements of the template are evaluated and combined into a single string to return.
Implementations§
Source§impl Template
impl Template
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Constructs a new, empty Template
with at least the specified capacity.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the template, also referred to as its ‘length’.
Sourcepub fn get(&self, index: usize) -> Option<&Element>
pub fn get(&self, index: usize) -> Option<&Element>
Returns a reference to the element at the given index, or None
if the index is out of
bounds.
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut Element>
pub fn get_mut(&mut self, index: usize) -> Option<&mut Element>
Returns a mutable reference to the element at the given index, or None
if the index is
out of bounds.
Sourcepub fn insert(&mut self, index: usize, element: impl Into<Element>)
pub fn insert(&mut self, index: usize, element: impl Into<Element>)
Inserts an element at position index
within the template, shifting all elements after it
to the right.
§Panics
Panics if index > len
.
Sourcepub fn push(&mut self, element: impl Into<Element>)
pub fn push(&mut self, element: impl Into<Element>)
Appends an element to the back of the template.
§Panics
Panics if the new capacity exceeds isize::MAX
bytes.
Sourcepub fn pop(&mut self) -> Option<Element>
pub fn pop(&mut self) -> Option<Element>
Removes the last element from the template and returns it, or None
if it is empty.
Sourcepub fn remove(&mut self, index: usize) -> Element
pub fn remove(&mut self, index: usize) -> Element
Removes and returns the element at position index
within the template, shifting all
elements after it to the left.
Like Vec::remove
, the element is removed by shifting all of the elements that follow it,
preserving their relative order. This perturbs the index of all of those elements!
§Panics
Panics if index
is out of bounds.
Sourcepub fn iter(&self) -> Iter<'_>
pub fn iter(&self) -> Iter<'_>
An iterator visiting all template elements in insertion order. The iterator element type
is &'a Element
.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_>
pub fn iter_mut(&mut self) -> IterMut<'_>
An iterator visiting all template elements in insertion order, with mutable references to
the values. The iterator element type is &'a mut Element
.
Sourcepub fn as_single_element(&self) -> Option<&Element>
pub fn as_single_element(&self) -> Option<&Element>
If the template consists of a single Element
, returns a reference to it, otherwise
None
.
§Example
use hcl_edit::template::{Element, Template};
let mut template = Template::new();
template.push("one");
assert_eq!(template.as_single_element(), Some(&Element::from("one")));
template.push("two");
assert_eq!(template.as_single_element(), None);
Sourcepub fn as_single_element_mut(&mut self) -> Option<&mut Element>
pub fn as_single_element_mut(&mut self) -> Option<&mut Element>
If the template consists of a single Element
, returns a mutable reference to it,
otherwise None
.
§Example
use hcl_edit::template::{Element, Template};
let mut template = Template::new();
template.push("one");
if let Some(element) = template.as_single_element_mut() {
*element = Element::from("two");
}
template.push("three");
assert_eq!(template.as_single_element(), None);
assert_eq!(template, Template::from_iter(["two", "three"]));
Trait Implementations§
Source§impl<T> Extend<T> for Template
impl<T> Extend<T> for Template
Source§fn extend<I>(&mut self, iterable: I)where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iterable: I)where
I: IntoIterator<Item = T>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)