use super::iter::{
Attributes, AttributesMut, Blocks, BlocksMut, IntoAttributes, IntoBlocks, Iter, IterMut,
};
use super::ser::BodySerializer;
use super::{Attribute, Block, Structure};
use crate::ser::with_internal_serialization;
use crate::{Error, Result};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Default, Clone)]
#[serde(rename = "$hcl::Body")]
pub struct Body(pub Vec<Structure>);
impl Body {
#[doc(hidden)]
pub fn from_serializable<T>(value: &T) -> Result<Body>
where
T: ?Sized + Serialize,
{
with_internal_serialization(|| value.serialize(BodySerializer))
}
pub fn into_inner(self) -> Vec<Structure> {
self.0
}
pub fn builder() -> BodyBuilder {
BodyBuilder::default()
}
pub fn iter(&self) -> Iter<'_> {
Iter::new(self)
}
pub fn iter_mut(&mut self) -> IterMut<'_> {
IterMut::new(self)
}
pub fn attributes(&self) -> Attributes<'_> {
Attributes::new(self)
}
pub fn attributes_mut(&mut self) -> AttributesMut<'_> {
AttributesMut::new(self)
}
pub fn into_attributes(self) -> IntoAttributes {
IntoAttributes::new(self)
}
pub fn blocks(&self) -> Blocks<'_> {
Blocks::new(self)
}
pub fn blocks_mut(&mut self) -> BlocksMut<'_> {
BlocksMut::new(self)
}
pub fn into_blocks(self) -> IntoBlocks {
IntoBlocks::new(self)
}
}
impl FromStr for Body {
type Err = Error;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
let body: hcl_edit::structure::Body = s.parse()?;
Ok(body.into())
}
}
impl<T> From<T> for Body
where
T: Into<Structure>,
{
fn from(value: T) -> Body {
Body(vec![value.into()])
}
}
impl<T> From<Vec<T>> for Body
where
T: Into<Structure>,
{
fn from(vec: Vec<T>) -> Self {
Body::from_iter(vec)
}
}
impl<T> From<&[T]> for Body
where
T: Clone + Into<Structure>,
{
fn from(slice: &[T]) -> Self {
Body::from_iter(slice.to_vec())
}
}
impl<T> From<&mut [T]> for Body
where
T: Clone + Into<Structure>,
{
fn from(slice: &mut [T]) -> Self {
Body::from_iter(slice.to_vec())
}
}
impl<T, const N: usize> From<[T; N]> for Body
where
T: Into<Structure>,
{
fn from(arr: [T; N]) -> Self {
Body::from_iter(arr)
}
}
#[derive(Debug, Default)]
pub struct BodyBuilder(Vec<Structure>);
impl BodyBuilder {
pub fn add_attribute<A>(self, attr: A) -> BodyBuilder
where
A: Into<Attribute>,
{
self.add_structure(attr.into())
}
pub fn add_attributes<I>(self, iter: I) -> BodyBuilder
where
I: IntoIterator,
I::Item: Into<Attribute>,
{
self.add_structures(iter.into_iter().map(Into::into))
}
pub fn add_block<B>(self, block: B) -> BodyBuilder
where
B: Into<Block>,
{
self.add_structure(block.into())
}
pub fn add_blocks<I>(self, iter: I) -> BodyBuilder
where
I: IntoIterator,
I::Item: Into<Block>,
{
self.add_structures(iter.into_iter().map(Into::into))
}
pub fn add_structure<S>(mut self, structure: S) -> BodyBuilder
where
S: Into<Structure>,
{
self.0.push(structure.into());
self
}
pub fn add_structures<I>(mut self, iter: I) -> BodyBuilder
where
I: IntoIterator,
I::Item: Into<Structure>,
{
self.0.extend(iter.into_iter().map(Into::into));
self
}
pub fn build(self) -> Body {
Body::from_iter(self.0)
}
}