hcl_edit/structure/
attribute.rs1use crate::expr::Expression;
2use crate::{Decor, Decorate, Decorated, Ident, Span};
3use std::ops::{self, Range};
4
5#[derive(Debug, Clone, Eq)]
16pub struct Attribute {
17 pub key: Decorated<Ident>,
19 pub value: Expression,
21
22 decor: Decor,
23 span: Option<Range<usize>>,
24}
25
26impl Attribute {
27 pub fn new(key: impl Into<Decorated<Ident>>, value: impl Into<Expression>) -> Attribute {
29 Attribute {
30 key: key.into(),
31 value: value.into(),
32 decor: Decor::default(),
33 span: None,
34 }
35 }
36
37 #[inline]
49 pub fn has_key(&self, key: &str) -> bool {
50 self.key.as_str() == key
51 }
52
53 pub(crate) fn despan(&mut self, input: &str) {
54 self.decor.despan(input);
55 self.key.decor_mut().despan(input);
56 self.value.despan(input);
57 }
58}
59
60impl PartialEq for Attribute {
61 fn eq(&self, other: &Self) -> bool {
62 self.key == other.key && self.value == other.value
63 }
64}
65
66decorate_impl!(Attribute);
67span_impl!(Attribute);
68
69pub struct AttributeMut<'a> {
76 attr: &'a mut Attribute,
77}
78
79impl<'a> AttributeMut<'a> {
80 pub(crate) fn new(attr: &'a mut Attribute) -> AttributeMut<'a> {
81 AttributeMut { attr }
82 }
83
84 pub fn get(&self) -> &Attribute {
86 self.attr
87 }
88
89 pub fn key_decor_mut(&mut self) -> &mut Decor {
91 self.attr.key.decor_mut()
92 }
93
94 pub fn value_mut(&mut self) -> &mut Expression {
96 &mut self.attr.value
97 }
98}
99
100impl<'a> ops::Deref for AttributeMut<'a> {
101 type Target = Attribute;
102
103 #[inline]
104 fn deref(&self) -> &Self::Target {
105 self.get()
106 }
107}
108
109impl<'a> Decorate for AttributeMut<'a> {
110 fn decor(&self) -> &Decor {
111 self.attr.decor()
112 }
113
114 fn decor_mut(&mut self) -> &mut Decor {
115 self.attr.decor_mut()
116 }
117}
118
119impl<'a> Span for AttributeMut<'a> {
120 fn span(&self) -> Option<Range<usize>> {
121 self.attr.span()
122 }
123}