hcl_edit::template

Struct StringTemplate

Source
pub struct StringTemplate { /* private fields */ }
Expand description

A type representing the HCL template sub-languange in the context of a quoted string literal.

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 StringTemplate

Source

pub fn new() -> Self

Constructs a new, empty StringTemplate.

Source

pub fn with_capacity(capacity: usize) -> Self

Constructs a new, empty StringTemplate with at least the specified capacity.

Methods from Deref<Target = Template>§

Source

pub fn is_empty(&self) -> bool

Returns true if the template contains no elements.

Source

pub fn len(&self) -> usize

Returns the number of elements in the template, also referred to as its ‘length’.

Source

pub fn clear(&mut self)

Clears the template, removing all elements.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn pop(&mut self) -> Option<Element>

Removes the last element from the template and returns it, or None if it is empty.

Source

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.

Source

pub fn iter(&self) -> Iter<'_>

An iterator visiting all template elements in insertion order. The iterator element type is &'a Element.

Source

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.

Source

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

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 Clone for StringTemplate

Source§

fn clone(&self) -> StringTemplate

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for StringTemplate

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Decorate for StringTemplate

Source§

fn decor(&self) -> &Decor

Returns a reference to the object’s Decor.
Source§

fn decor_mut(&mut self) -> &mut Decor

Returns a mutable reference to the object’s Decor.
Source§

fn decorate(&mut self, decor: impl Into<Decor>)

Decorate the object with decor in-place.
Source§

fn decorated(self, decor: impl Into<Decor>) -> Self
where Self: Sized,

Decorate the object with decor and return the modified value.
Source§

impl Default for StringTemplate

Source§

fn default() -> StringTemplate

Returns the “default value” for a type. Read more
Source§

impl Deref for StringTemplate

Source§

type Target = Template

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for StringTemplate

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T> Extend<T> for StringTemplate
where T: Into<Element>,

Source§

fn extend<I>(&mut self, iterable: I)
where I: IntoIterator<Item = T>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl From<StringTemplate> for Expression

Source§

fn from(value: StringTemplate) -> Self

Converts to this type from the input type.
Source§

impl From<StringTemplate> for Template

Source§

fn from(template: StringTemplate) -> Self

Converts to this type from the input type.
Source§

impl From<Template> for StringTemplate

Source§

fn from(template: Template) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<Element>> for StringTemplate

Source§

fn from(elements: Vec<Element>) -> Self

Converts to this type from the input type.
Source§

impl<T> FromIterator<T> for StringTemplate
where T: Into<Element>,

Source§

fn from_iter<I>(iterable: I) -> Self
where I: IntoIterator<Item = T>,

Creates a value from an iterator. Read more
Source§

impl<'a> IntoIterator for &'a StringTemplate

Source§

type Item = &'a Element

The type of the elements being iterated over.
Source§

type IntoIter = Box<dyn Iterator<Item = &'a Element> + 'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a> IntoIterator for &'a mut StringTemplate

Source§

type Item = &'a mut Element

The type of the elements being iterated over.
Source§

type IntoIter = Box<dyn Iterator<Item = &'a mut Element> + 'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for StringTemplate

Source§

type Item = Element

The type of the elements being iterated over.
Source§

type IntoIter = Box<dyn Iterator<Item = Element>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for StringTemplate

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Span for StringTemplate

Source§

fn span(&self) -> Option<Range<usize>>

Obtains the span information. This only returns Some if the value was emitted by the parser. Read more
Source§

impl Eq for StringTemplate

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.