fhir_rs/fhirpath4/
collection.rs1use std::ops::{Add, BitOr, BitXor, Div, Mul, Not, Sub};
2use crate::datatype::Boolean;
3use crate::prelude::{Result, FhirError, Integer, Decimal, DateTime};
4use super::*;
5
6#[derive(Debug)]
7pub struct Collection(Vec<Box<dyn Executor>>);
8
9impl Collection {
10 pub fn new() -> Self {
12 Self(vec![])
13 }
14
15 pub fn new_string(value: String) -> Self {
16 Self(vec![Box::new(value)])
17 }
18
19 pub fn new_integer(value: Integer) -> Self {
20 Self(vec![Box::new(value)])
21 }
22
23 pub fn new_decimal(value: Decimal) -> Self {
24 Self(vec![Box::new(value)])
25 }
26
27 pub fn new_datetime(value: DateTime) -> Self {
28 Self(vec![Box::new(value)])
29 }
30
31 pub fn new_boolean(value: Boolean) -> Self {
32 Self(vec![Box::new(value)])
33 }
34
35 pub fn new_any(value: Box<dyn Executor>) -> Self {
36 Self(vec![value])
37 }
38
39 pub fn first(&self) -> Option<&Box<dyn Executor>> {
40 self.0.get(0)
41 }
42
43 pub fn count(&self) -> usize {
44 self.0.len()
45 }
46
47 pub fn combine(&mut self, other: Collection) {
48 self.0.extend(other.0)
49 }
50
51 pub fn push(&mut self, value: Box<dyn Executor>) {
52 self.0.push(value)
53 }
54
55 pub fn empty(&self) -> bool {
56 self.count() == 0
57 }
58
59 pub fn exists(&self) -> bool {
60 self.count() > 0
61 }
62
63 pub fn to_integer(&self) -> Result<Option<Integer>> {
64 match self.count() {
65 0 => Ok(None),
66 1 => Ok(Some(self.0[0].to_integer()?)),
67 _ => Err(FhirError::error("集合内元素数量大于1")),
68 }
69 }
70
71 pub fn to_boolean(&self) -> Result<Option<bool>> {
72 match self.count() {
73 0 => Ok(None),
74 1 => Ok(Some(self.0[0].to_boolean()?)),
75 other => Err(FhirError::Message(format!("集合元素数量[{}]大于1", other)))
76 }
77 }
78
79 pub fn all_true(&self) -> Result<bool> {
80 Ok(true)
84 }
85
86 pub fn element(self, symbol: &String, index: &Option<usize>) -> Result<Collection> {
87 let mut collection = Collection::new();
88 for part in self.0 {
89 let children = part.element(symbol, index)?;
90 collection.combine(children)
91 }
92
93 Ok(collection)
94 }
95
96 pub fn filter(self, criteria: &Expr) -> Result<Collection> {
97 let mut collection = Collection::new();
98
99 for part in self.0 {
100 let col = criteria.eval(part.as_ref())?;
101 let bl = col.to_boolean()?;
102 let bl = bl.unwrap_or_else(|| false);
103 if bl {collection.push(part)}
104 }
105
106 Ok(collection)
107 }
108
109 pub fn call(self, symbol: &String, args: &Option<Vec<Expr>>) -> Result<Collection> {
110 match symbol.as_str() {
111 "where" => {
112 match args {
113 None => Err(FhirError::error("where()至少拥有一个参数")),
114 Some(arg) => {
115 if arg.len() == 1 {
116 self.filter(&arg[0])
117 } else {
118 Err(FhirError::error("where()至多只能拥有一个参数"))
119 }
120 }
121 }
122 },
123 other => Err(FhirError::Message(format!("这是无效或者未被支持的函数名[{}]", other))),
124 }
125 }
126
127 pub fn single(self) -> Result<Collection> {
128 if self.count() > 1 {
129 return Err(FhirError::error("执行single函数时集合内超过一个元素"))
130 }
131
132 Ok(self)
133 }
134
135 pub fn eq(self, right: Collection) -> Result<Collection> {
140 if self.empty() | right.empty() {
141 return Ok(Collection::new())
142 }
143
144 if self.count() != right.count() {
145 return Ok(Collection::new_boolean(false))
146 }
147
148 for (idx, part) in self.0.iter().enumerate() {
149 let lhs = part.as_ref();
150 let rhs = right.0[idx].as_ref();
151
152 if !lhs.eq(rhs)? {
153 return Ok(Collection::new_boolean(false))
154 }
155 }
156
157 Ok(Collection::new_boolean(true))
158 }
159
160 pub fn not(self) -> Result<Collection> {
161 match self.to_boolean()? {
162 None => Ok(Collection::new()),
163 Some(bl) => Ok(Collection::new_boolean(bl.not()))
164 }
165 }
166
167 pub fn gt(self, _right: Collection) -> Result<Collection> {
168 Ok(Collection::new_boolean(true))
169 }
170
171 pub fn ge(self, _right: Collection) -> Result<Collection> {
172 Ok(Collection::new_boolean(true))
173 }
174
175 pub fn lt(self, _right: Collection) -> Result<Collection> {
176 Ok(Collection::new_boolean(true))
177 }
178
179 pub fn le(self, _right: Collection) -> Result<Collection> {
180 Ok(Collection::new_boolean(true))
181 }
182}
183
184impl Add for Collection {
185 type Output = Result<Collection>;
186
187 fn add(self, rhs: Self) -> Self::Output {
188 if self.empty() | rhs.empty() {
189 return Ok(Collection::new())
190 }
191
192 if (self.count() > 1) | (rhs.count() > 1) {
193 return Err(FhirError::error("集合内元素数量大于1"))
194 }
195 Ok(Collection::new())
198 }
199}
200
201impl Sub for Collection {
202 type Output = Result<Collection>;
203
204 fn sub(self, _rhs: Self) -> Self::Output {
205 todo!()
206 }
207}
208
209impl Mul for Collection {
210 type Output = Result<Collection>;
211
212 fn mul(self, _rhs: Self) -> Self::Output {
213 todo!()
214 }
215}
216
217impl Div for Collection {
218 type Output = Result<Collection>;
219
220 fn div(self, _rhs: Self) -> Self::Output {
221 todo!()
222 }
223}
224
225impl BitAnd for Collection {
226 type Output = Result<Option<bool>>;
227
228 fn bitand(self, rhs: Self) -> Self::Output {
229 match (self.to_boolean()?, rhs.to_boolean()?) {
230 (Some(false), _) => Ok(Some(false)),
231 (_, Some(false)) => Ok(Some(false)),
232 (_, None) => Ok(None),
233 (None, _) => Ok(None),
234 (Some(true), Some(true)) => Ok(Some(true)),
235 }
236 }
237}
238
239impl BitOr for Collection {
240 type Output = Result<Option<bool>>;
241
242 fn bitor(self, rhs: Self) -> Self::Output {
243 match (self.to_boolean()?, rhs.to_boolean()?) {
244 (Some(true), _) => Ok(Some(true)),
245 (_, Some(true)) => Ok(Some(true)),
246 (_, None) => Ok(None),
247 (None, _) => Ok(None),
248 (Some(false), Some(false)) => Ok(Some(false)),
249 }
250 }
251}
252
253impl BitXor for Collection {
254 type Output = Result<Option<bool>>;
255
256 fn bitxor(self, rhs: Self) -> Self::Output {
257 match (self.to_boolean()?, rhs.to_boolean()?) {
258 (_, None) => Ok(None),
259 (None, _) => Ok(None),
260 (Some(true), Some(false)) => Ok(Some(true)),
261 (Some(false), Some(true)) => Ok(Some(true)),
262 (Some(true), Some(true)) => Ok(Some(false)),
263 (Some(false), Some(false)) => Ok(Some(false)),
264 }
265 }
266}