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
use std::{path::PathBuf, sync::Arc};
use anyhow::Result;
use parking_lot::Mutex;
use wild_doc_script::{IncludeAdaptor, Stack, WildDocScript, WildDocValue, async_trait};
pub struct Var {}
#[async_trait(?Send)]
impl<I: IncludeAdaptor + Send> WildDocScript<I> for Var {
fn new(_: Arc<Mutex<I>>, _: PathBuf, _: &Stack) -> Result<Self>
where
Self: Sized,
{
Ok(Self {})
}
async fn evaluate_module(&mut self, _: &str, _: &str, _: &Stack) -> Result<()> {
Ok(())
}
async fn eval(&mut self, code: &str, stack: &Stack) -> Result<WildDocValue> {
let mut splited = code.split(".");
if let Some(root) = splited.next() {
if let Some(mut next_value) = stack.get(&Arc::new(root.into())) {
loop {
if let Some(next) = splited.next() {
match next_value {
WildDocValue::Object(map) => {
if let Some(v) = map.get(&Arc::new(next.into())) {
next_value = v;
} else {
break;
}
}
WildDocValue::Array(map) => {
let mut found = false;
if let Ok(v) = next.parse::<usize>() {
if let Some(v) = map.get(v) {
found = true;
next_value = v;
}
}
if !found {
break;
}
}
WildDocValue::SearchResult(result) => {
if next == "rows" {
if let Some(next) = splited.next() {
if next == "len" {
return Ok(WildDocValue::Number(
result.rows().len().into(),
));
}
} else {
return Ok(WildDocValue::Array(
result
.rows()
.iter()
.map(|v| WildDocValue::Number(v.get().into()))
.collect(),
));
}
}
}
WildDocValue::SessionSearchResult(result) => {
if next == "rows" {
if let Some(next) = splited.next() {
if next == "len" {
return Ok(WildDocValue::Number(
result.rows().len().into(),
));
}
} else {
return Ok(WildDocValue::Array(
result
.rows()
.iter()
.map(|v| WildDocValue::Number(v.get().into()))
.collect(),
));
}
}
}
_ => break,
}
} else {
return Ok(next_value.clone());
}
}
}
}
Ok(WildDocValue::Null)
}
}