hcl/structure/attribute.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
//! Types to represent and build HCL attributes.
use crate::expr::Expression;
use crate::{Identifier, Value};
use serde::Deserialize;
use std::iter;
/// Represents an HCL attribute which consists of an attribute key and a value expression.
///
/// In HCL syntax this is represented as:
///
/// ```hcl
/// key = value
/// ```
///
/// Use [`Attribute::new`] to construct an [`Attribute`] from a value that is convertible to this
/// crate's [`Expression`] type.
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Attribute {
/// The HCL attribute's key.
pub key: Identifier,
/// The value expression of the HCL attribute.
pub expr: Expression,
}
impl Attribute {
/// Creates a new `Attribute` from an attribute key that is convertible into a `String` and an
/// attribute value that is convertible into an `Expression`.
pub fn new<K, V>(key: K, expr: V) -> Attribute
where
K: Into<Identifier>,
V: Into<Expression>,
{
Attribute {
key: key.into(),
expr: expr.into(),
}
}
/// Returns a reference to the attribute key.
pub fn key(&self) -> &str {
&self.key
}
/// Returns a reference to the attribute value expression.
pub fn expr(&self) -> &Expression {
&self.expr
}
}
impl From<Attribute> for Value {
fn from(attr: Attribute) -> Value {
iter::once((attr.key.into_inner(), attr.expr)).collect()
}
}
impl<K, V> From<(K, V)> for Attribute
where
K: Into<Identifier>,
V: Into<Expression>,
{
fn from((key, expr): (K, V)) -> Attribute {
Attribute::new(key, expr)
}
}