-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrec_ref.rs
94 lines (81 loc) · 2.23 KB
/
rec_ref.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
use std::marker::PhantomData;
use crate::{
prim_ref::{
PrimExpr,
},
StackShared,
list_ref::{
RecToListMappable,
},
MapKV,
Ref,
};
pub trait ToObjMappable {
type O;
fn do_map_rec(self, base: String, k: PrimExpr<String>) -> Self::O;
}
pub trait ListToRecMappable {
type O;
fn do_map_rec(self, base: String, k: PrimExpr<String>) -> Self::O;
}
pub struct RecRef<T: Ref> {
pub(crate) shared: StackShared,
pub(crate) base: String,
_pd: PhantomData<T>,
}
impl<T: Ref> Ref for RecRef<T> {
fn new(shared: StackShared, base: String) -> Self {
RecRef {
shared: shared,
base: base,
_pd: Default::default(),
}
}
}
impl<T: Ref> RecRef<T> {
pub fn get(&self, key: impl ToString) -> T {
T::new(self.shared.clone(), format!("{}[\"{}\"]", &self.base, key.to_string()))
}
pub fn map<O: RecToListMappable>(&self, inner: impl FnOnce(MapKV<T>) -> O) -> O::O {
let out = inner(MapKV::new(self.shared.clone()));
out.do_map(self.base.clone())
}
pub fn map_rec<O: ToObjMappable>(&self, inner: impl FnOnce(MapKV<T>) -> (PrimExpr<String>, O)) -> O::O {
let (k, out) = inner(MapKV::new(self.shared.clone()));
out.do_map_rec(self.base.clone(), k)
}
}
pub struct MapRecRef<T> {
pub(crate) shared: StackShared,
pub(crate) base: String,
pub(crate) map_base_key: String,
pub(crate) map_base: String,
_pd: PhantomData<T>,
}
impl<T> MapRecRef<T> {
pub(crate) fn new(shared: StackShared, base: String, map_base_key: String, map_base: String) -> Self {
MapRecRef {
shared: shared,
base: base,
map_base_key: map_base_key,
map_base: map_base,
_pd: Default::default(),
}
}
}
pub struct MapRecRefToList<T> {
pub(crate) shared: StackShared,
pub(crate) base: String,
pub(crate) map_base: String,
_pd: PhantomData<T>,
}
impl<T> MapRecRefToList<T> {
pub(crate) fn new(shared: StackShared, base: String, map_base: String) -> Self {
MapRecRefToList {
shared: shared,
base: base,
map_base: map_base,
_pd: Default::default(),
}
}
}