-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvariable.rs
113 lines (99 loc) · 2.53 KB
/
variable.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use std::{
cell::{
RefCell,
},
fmt::Display,
marker::PhantomData,
rc::Rc,
};
use serde::{
Serialize,
};
use serde_json::{
Value,
};
use crate::{
PrimField,
PrimType,
prim_ref::PrimExpr,
StackShared,
expr::{
Expr,
},
manual_expr_impls,
SerdeSkipDefault,
Stack,
};
pub(crate) trait VariableTrait {
fn extract_tf_id(&self) -> String;
fn extract_value(&self) -> Value;
}
#[derive(Serialize)]
struct VariableImplData {
#[serde(skip_serializing_if = "SerdeSkipDefault::is_default")]
pub r#type: String,
#[serde(skip_serializing_if = "SerdeSkipDefault::is_not_default")]
pub nullable: PrimField<bool>,
#[serde(skip_serializing_if = "SerdeSkipDefault::is_default")]
pub sensitive: PrimField<bool>,
}
struct Variable_<T: PrimType> {
shared: StackShared,
tf_id: String,
data: RefCell<VariableImplData>,
_p: PhantomData<T>,
}
pub struct Variable<T: PrimType>(Rc<Variable_<T>>);
impl<T: PrimType> VariableTrait for Variable_<T> {
fn extract_tf_id(&self) -> String {
self.tf_id.clone()
}
fn extract_value(&self) -> Value {
let data = self.data.borrow();
serde_json::to_value(&*data).unwrap()
}
}
impl<T: PrimType> Variable<T> {
pub fn set_nullable(self, v: impl Into<PrimField<bool>>) -> Self {
self.0.data.borrow_mut().nullable = v.into();
self
}
pub fn set_sensitive(self, v: impl Into<PrimField<bool>>) -> Self {
self.0.data.borrow_mut().sensitive = v.into();
self
}
}
impl<T: PrimType> Expr<T> for Variable<T> {
fn expr_raw(&self) -> (&StackShared, String) {
(&self.0.shared, format!("var.{}", self.0.tf_id))
}
fn expr_sentinel(&self) -> String {
let (shared, raw) = self.expr_raw();
shared.add_sentinel(&raw)
}
}
impl<T: PrimType> Variable<T> {
pub fn raw(&self) -> String {
self.expr_raw().1
}
}
manual_expr_impls!(Variable);
pub struct BuildVariable {
pub tf_id: String,
}
impl BuildVariable {
pub fn build<T: PrimType + 'static>(self, stack: &mut Stack) -> Variable<T> {
let out = Variable(Rc::new(Variable_ {
shared: stack.shared.clone(),
tf_id: self.tf_id,
data: RefCell::new(VariableImplData {
r#type: T::extract_variable_type(),
nullable: false.into(),
sensitive: false.into(),
}),
_p: Default::default(),
}));
stack.variables.push(out.0.clone());
out
}
}