1use crate::Constant;
2
3#[derive(Debug, Default)]
47pub struct Expr {
48 id: i64,
49 kind: Option<Box<ExprKind>>,
50}
51
52impl std::ops::Deref for Expr {
53 type Target = ExprKind;
54
55 fn deref(&self) -> &Self::Target {
56 self.kind.as_ref().unwrap()
57 }
58}
59
60impl std::ops::DerefMut for Expr {
61 fn deref_mut(&mut self) -> &mut Self::Target {
62 self.kind.as_mut().unwrap()
63 }
64}
65
66impl Expr {
67 pub fn id(&self) -> i64 {
72 self.id
73 }
74
75 pub fn set_id(&mut self, id: i64) {
80 self.id = id;
81 }
82
83 pub fn kind(&self) -> Option<&ExprKind> {
100 self.kind.as_ref()
101 .map(|k| k.as_ref())
102 }
103
104 pub fn set_kind(&mut self, kind: ExprKind) {
109 self.kind = Some(Box::new(kind));
110 }
111
112 pub fn clear_kind(&mut self) {
116 self.kind = None;
117 }
118}
119
120#[derive(Debug)]
136pub enum ExprKind {
137 Constant(Constant),
139 Ident(IdentExpr),
141 Select(SelectExpr),
143 Call(CallExpr),
145 List(ListExpr),
147 Struct(StructExpr),
149 Map(MapExpr),
151 Comprehension(ComprehensionExpr),
153}
154
155impl ExprKind {
156 pub fn is_constant(&self) -> bool {
158 matches!(self, ExprKind::Constant(_))
159 }
160
161 pub fn is_ident(&self) -> bool {
163 matches!(self, ExprKind::Ident(_))
164 }
165
166 pub fn is_select(&self) -> bool {
168 matches!(self, ExprKind::Select(_))
169 }
170
171 pub fn is_call(&self) -> bool {
173 matches!(self, ExprKind::Call(_))
174 }
175
176 pub fn is_list(&self) -> bool {
178 matches!(self, ExprKind::List(_))
179 }
180
181 pub fn is_struct(&self) -> bool {
183 matches!(self, ExprKind::Struct(_))
184 }
185
186 pub fn is_map(&self) -> bool {
188 matches!(self, ExprKind::Map(_))
189 }
190
191 pub fn is_comprehension(&self) -> bool {
193 matches!(self, ExprKind::Comprehension(_))
194 }
195
196 pub fn as_constant(&self) -> Option<&Constant> {
198 if let ExprKind::Constant(c) = self {
199 Some(c)
200 } else {
201 None
202 }
203 }
204
205 pub fn as_ident(&self) -> Option<&IdentExpr> {
207 if let ExprKind::Ident(i) = self {
208 Some(i)
209 } else {
210 None
211 }
212 }
213
214 pub fn as_select(&self) -> Option<&SelectExpr> {
216 if let ExprKind::Select(s) = self {
217 Some(s)
218 } else {
219 None
220 }
221 }
222
223 pub fn as_call(&self) -> Option<&CallExpr> {
225 if let ExprKind::Call(c) = self {
226 Some(c)
227 } else {
228 None
229 }
230 }
231
232 pub fn as_list(&self) -> Option<&ListExpr> {
234 if let ExprKind::List(l) = self {
235 Some(l)
236 } else {
237 None
238 }
239 }
240
241 pub fn as_struct(&self) -> Option<&StructExpr> {
243 if let ExprKind::Struct(s) = self {
244 Some(s)
245 } else {
246 None
247 }
248 }
249
250 pub fn as_map(&self) -> Option<&MapExpr> {
252 if let ExprKind::Map(m) = self {
253 Some(m)
254 } else {
255 None
256 }
257 }
258
259 pub fn as_comprehension(&self) -> Option<&ComprehensionExpr> {
261 if let ExprKind::Comprehension(c) = self {
262 Some(c)
263 } else {
264 None
265 }
266 }
267
268 pub fn as_constant_mut(&mut self) -> Option<&mut Constant> {
270 if let ExprKind::Constant(c) = self {
271 Some(c)
272 } else {
273 None
274 }
275 }
276
277 pub fn as_ident_mut(&mut self) -> Option<&mut IdentExpr> {
279 if let ExprKind::Ident(i) = self {
280 Some(i)
281 } else {
282 None
283 }
284 }
285
286 pub fn as_select_mut(&mut self) -> Option<&mut SelectExpr> {
288 if let ExprKind::Select(s) = self {
289 Some(s)
290 } else {
291 None
292 }
293 }
294
295 pub fn as_call_mut(&mut self) -> Option<&mut CallExpr> {
297 if let ExprKind::Call(c) = self {
298 Some(c)
299 } else {
300 None
301 }
302 }
303
304 pub fn as_list_mut(&mut self) -> Option<&mut ListExpr> {
306 if let ExprKind::List(l) = self {
307 Some(l)
308 } else {
309 None
310 }
311 }
312
313 pub fn as_struct_mut(&mut self) -> Option<&mut StructExpr> {
315 if let ExprKind::Struct(s) = self {
316 Some(s)
317 } else {
318 None
319 }
320 }
321
322 pub fn as_map_mut(&mut self) -> Option<&mut MapExpr> {
324 if let ExprKind::Map(m) = self {
325 Some(m)
326 } else {
327 None
328 }
329 }
330
331 pub fn as_comprehension_mut(&mut self) -> Option<&mut ComprehensionExpr> {
333 if let ExprKind::Comprehension(c) = self {
334 Some(c)
335 } else {
336 None
337 }
338 }
339}
340
341#[derive(Debug)]
346pub struct IdentExpr {
347 pub name: String,
349}
350
351#[derive(Debug)]
357pub struct SelectExpr {
358 pub operand: Expr,
360 pub field: String,
362 pub test_only: bool,
364}
365
366#[derive(Debug)]
371pub struct CallExpr {
372 pub function: String,
374 pub target: Expr,
376 pub args: Vec<Expr>,
378}
379
380#[derive(Debug)]
384pub struct ListExprElement {
385 pub expr: Expr,
387 pub optional: bool,
389}
390
391#[derive(Debug)]
395pub struct ListExpr {
396 pub elements: Vec<ListExprElement>,
398}
399
400#[derive(Debug)]
404pub struct StructExprField {
405 pub id: i64,
407 pub name: String,
409 pub value: Expr,
411 pub optional: bool,
413}
414
415#[derive(Debug)]
420pub struct StructExpr {
421 pub name: String,
423 pub fields: Vec<StructExprField>,
425}
426
427#[derive(Debug)]
431pub struct MapExprEntry {
432 pub id: i64,
434 pub key: Expr,
436 pub value: Expr,
438 pub optional: bool,
440}
441
442#[derive(Debug)]
446pub struct MapExpr {
447 pub entries: Vec<MapExprEntry>,
449}
450
451#[derive(Debug)]
479pub struct ComprehensionExpr {
480 pub iter_var: String,
482 pub iter_var2: String,
484 pub iter_range: Expr,
486 pub accu_var: String,
488 pub accu_init: Expr,
490 pub loop_condition: Expr,
492 pub loop_step: Expr,
494 pub result: Expr,
496}
497
498impl From<&Expr> for cxx::UniquePtr<crate::ffi::Expr> {
499 fn from(value: &Expr) -> Self {
500 use crate::ffi::Expr as FfiExpr;
501 use crate::ffi::Constant as FfiConstant;
502 use crate::ffi::CxxVectorExt;
503
504 let mut ffi_expr = FfiExpr::new();
505 ffi_expr.pin_mut().set_id(value.id());
506 match value.kind() {
507 None => {}
508 Some(ExprKind::Constant(constant)) => {
509 let ffi_constant: cxx::UniquePtr<FfiConstant> = constant.into();
510 ffi_expr.pin_mut()
511 .set_const_expr(ffi_constant);
512 }
513 Some(ExprKind::Ident(ident)) => {
514 let mut ident_expr = ffi_expr.pin_mut().ident_expr_mut();
515 ident_expr.as_mut().set_name(ident.name.as_str().into());
516 }
517 Some(ExprKind::Select(select)) => {
518 let mut select_expr = ffi_expr.pin_mut().select_expr_mut();
519 select_expr.as_mut().set_operand((&select.operand).into());
520 select_expr.as_mut().set_field(select.field.as_str().into());
521 select_expr.as_mut().set_test_only(select.test_only);
522 }
523 Some(ExprKind::Call(call)) => {
524 let mut call_expr = ffi_expr.pin_mut().call_expr_mut();
525 call_expr.as_mut().set_function(call.function.as_str().into());
526 call_expr.as_mut().set_target((&call.target).into());
527 let mut args = call_expr.as_mut().args_mut();
528 for arg in &call.args {
529 args.as_mut().push_unique(arg.into());
530 }
531 }
532 Some(ExprKind::List(list)) => {
533 let mut list_expr = ffi_expr.pin_mut().list_expr_mut();
534 let mut elements = list_expr.as_mut().elements_mut();
535 for element in &list.elements {
536 elements.as_mut().push_unique(element.into());
537 }
538 }
539 Some(ExprKind::Struct(struct_)) => {
540 let mut struct_expr = ffi_expr.pin_mut().struct_expr_mut();
541 let mut fields = struct_expr.as_mut().fields_mut();
542 for field in &struct_.fields {
543 fields.as_mut().push_unique(field.into());
544 }
545 }
546 Some(ExprKind::Map(map)) => {
547 let mut map_expr = ffi_expr.pin_mut().map_expr_mut();
548 let mut entries = map_expr.as_mut().entries_mut();
549 for entry in &map.entries {
550 entries.as_mut().push_unique(entry.into());
551 }
552 }
553 Some(ExprKind::Comprehension(comprehension)) => {
554 let mut comprehension_expr = ffi_expr.pin_mut().comprehension_expr_mut();
555 comprehension_expr.as_mut().set_iter_var(comprehension.iter_var.as_str().into());
556 comprehension_expr.as_mut().set_iter_var2(comprehension.iter_var2.as_str().into());
557 comprehension_expr.as_mut().set_iter_range((&comprehension.iter_range).into());
558 comprehension_expr.as_mut().set_accu_var(comprehension.accu_var.as_str().into());
559 comprehension_expr.as_mut().set_accu_init((&comprehension.accu_init).into());
560 comprehension_expr.as_mut().set_loop_condition((&comprehension.loop_condition).into());
561 comprehension_expr.as_mut().set_loop_step((&comprehension.loop_step).into());
562 comprehension_expr.as_mut().set_result((&comprehension.result).into());
563 }
564 }
565 ffi_expr
566 }
567}
568
569impl From<Expr> for cxx::UniquePtr<crate::ffi::Expr> {
570 fn from(value: Expr) -> Self {
571 Self::from(&value)
572 }
573}
574
575impl From<&crate::ffi::Expr> for Expr {
576 fn from(value: &crate::ffi::Expr) -> Self {
577 use crate::ffi::ExprKindCase as FfiExprKindCase;
578
579 let mut expr = Expr::default();
580 expr.set_id(value.id());
581
582 let kind = match value.kind_case() {
583 FfiExprKindCase::Unspecified => None,
584 FfiExprKindCase::Constant => Some(ExprKind::Constant(Constant::from(value.const_expr()))),
585 FfiExprKindCase::Ident => Some(ExprKind::Ident(IdentExpr::from(value.ident_expr()))),
586 FfiExprKindCase::Select => Some(ExprKind::Select(SelectExpr::from(value.select_expr()))),
587 FfiExprKindCase::Call => Some(ExprKind::Call(CallExpr::from(value.call_expr()))),
588 FfiExprKindCase::List => Some(ExprKind::List(ListExpr::from(value.list_expr()))),
589 FfiExprKindCase::Struct => Some(ExprKind::Struct(StructExpr::from(value.struct_expr()))),
590 FfiExprKindCase::Map => Some(ExprKind::Map(MapExpr::from(value.map_expr()))),
591 FfiExprKindCase::Comprehension => Some(ExprKind::Comprehension(ComprehensionExpr::from(value.comprehension_expr()))),
592 };
593
594 if let Some(kind) = kind {
595 expr.set_kind(kind);
596 }
597 expr
598 }
599}
600
601impl From<cxx::UniquePtr<crate::ffi::Expr>> for Expr {
602 fn from(value: cxx::UniquePtr<crate::ffi::Expr>) -> Self {
603 Self::from(&*value)
604 }
605}
606
607impl From<&crate::ffi::IdentExpr> for IdentExpr {
608 fn from(value: &crate::ffi::IdentExpr) -> Self {
609 IdentExpr {
610 name: value.name().to_string(),
611 }
612 }
613}
614
615impl From<cxx::UniquePtr<crate::ffi::IdentExpr>> for IdentExpr {
616 fn from(value: cxx::UniquePtr<crate::ffi::IdentExpr>) -> Self {
617 Self::from(&*value)
618 }
619}
620
621impl From<&crate::ffi::SelectExpr> for SelectExpr {
622 fn from(value: &crate::ffi::SelectExpr) -> Self {
623 SelectExpr {
624 operand: Expr::from(value.operand()),
625 field: value.field().to_string(),
626 test_only: value.test_only(),
627 }
628 }
629}
630
631impl From<cxx::UniquePtr<crate::ffi::SelectExpr>> for SelectExpr {
632 fn from(value: cxx::UniquePtr<crate::ffi::SelectExpr>) -> Self {
633 Self::from(&*value)
634 }
635}
636
637impl From<&crate::ffi::CallExpr> for CallExpr {
638 fn from(value: &crate::ffi::CallExpr) -> Self {
639 CallExpr {
640 function: value.function().to_string(),
641 target: Expr::from(value.target()),
642 args: value.args()
643 .into_iter()
644 .map(|arg| Expr::from(arg))
645 .collect(),
646 }
647 }
648}
649
650impl From<cxx::UniquePtr<crate::ffi::CallExpr>> for CallExpr {
651 fn from(value: cxx::UniquePtr<crate::ffi::CallExpr>) -> Self {
652 Self::from(&*value)
653 }
654}
655
656impl From<&crate::ffi::ListExpr> for ListExpr {
657 fn from(value: &crate::ffi::ListExpr) -> Self {
658 ListExpr {
659 elements: value.elements()
660 .into_iter()
661 .map(|element| ListExprElement::from(element))
662 .collect(),
663 }
664 }
665}
666
667impl From<cxx::UniquePtr<crate::ffi::ListExpr>> for ListExpr {
668 fn from(value: cxx::UniquePtr<crate::ffi::ListExpr>) -> Self {
669 Self::from(&*value)
670 }
671}
672
673impl From<&crate::ffi::StructExpr> for StructExpr {
674 fn from(value: &crate::ffi::StructExpr) -> Self {
675 StructExpr {
676 name: value.name().to_string(),
677 fields: value.fields()
678 .into_iter()
679 .map(|field| StructExprField::from(field))
680 .collect(),
681 }
682 }
683}
684
685impl From<cxx::UniquePtr<crate::ffi::StructExpr>> for StructExpr {
686 fn from(value: cxx::UniquePtr<crate::ffi::StructExpr>) -> Self {
687 Self::from(&*value)
688 }
689}
690
691impl From<&crate::ffi::MapExpr> for MapExpr {
692 fn from(value: &crate::ffi::MapExpr) -> Self {
693 MapExpr {
694 entries: value.entries()
695 .into_iter()
696 .map(|entry| MapExprEntry::from(entry))
697 .collect(),
698 }
699 }
700}
701
702impl From<cxx::UniquePtr<crate::ffi::MapExpr>> for MapExpr {
703 fn from(value: cxx::UniquePtr<crate::ffi::MapExpr>) -> Self {
704 Self::from(&*value)
705 }
706}
707
708impl From<&crate::ffi::ComprehensionExpr> for ComprehensionExpr {
709 fn from(value: &crate::ffi::ComprehensionExpr) -> Self {
710 ComprehensionExpr {
711 iter_var: value.iter_var().to_string(),
712 iter_var2: value.iter_var2().to_string(),
713 iter_range: Expr::from(value.iter_range()),
714 accu_var: value.accu_var().to_string(),
715 accu_init: Expr::from(value.accu_init()),
716 loop_condition: Expr::from(value.loop_condition()),
717 loop_step: Expr::from(value.loop_step()),
718 result: Expr::from(value.result()),
719 }
720 }
721}
722
723impl From<cxx::UniquePtr<crate::ffi::ComprehensionExpr>> for ComprehensionExpr {
724 fn from(value: cxx::UniquePtr<crate::ffi::ComprehensionExpr>) -> Self {
725 Self::from(&*value)
726 }
727}
728
729impl From<&ListExprElement> for cxx::UniquePtr<crate::ffi::ListExprElement> {
730 fn from(value: &ListExprElement) -> Self {
731 use crate::ffi::ListExprElement as FfiListExprElement;
732
733 let mut ffi_element = FfiListExprElement::new();
734 ffi_element.pin_mut().set_expr((&value.expr).into());
735 ffi_element.pin_mut().set_optional(value.optional);
736 ffi_element
737 }
738}
739
740impl From<ListExprElement> for cxx::UniquePtr<crate::ffi::ListExprElement> {
741 fn from(element: ListExprElement) -> Self {
742 Self::from(&element)
743 }
744}
745
746impl From<&crate::ffi::ListExprElement> for ListExprElement {
747 fn from(value: &crate::ffi::ListExprElement) -> Self {
748 ListExprElement {
749 expr: Expr::from(value.expr()),
750 optional: value.optional(),
751 }
752 }
753}
754
755impl From<cxx::UniquePtr<crate::ffi::ListExprElement>> for ListExprElement {
756 fn from(value: cxx::UniquePtr<crate::ffi::ListExprElement>) -> Self {
757 Self::from(&*value)
758 }
759}
760
761impl From<&StructExprField> for cxx::UniquePtr<crate::ffi::StructExprField> {
762 fn from(value: &StructExprField) -> Self {
763 use crate::ffi::StructExprField as FfiStructExprField;
764
765 let mut ffi_field = FfiStructExprField::new();
766 ffi_field.pin_mut().set_id(value.id);
767 ffi_field.pin_mut().set_name(value.name.as_str().into());
768 ffi_field.pin_mut().set_value((&value.value).into());
769 ffi_field.pin_mut().set_optional(value.optional);
770 ffi_field
771 }
772}
773
774impl From<StructExprField> for cxx::UniquePtr<crate::ffi::StructExprField> {
775 fn from(value: StructExprField) -> Self {
776 Self::from(&value)
777 }
778}
779
780impl From<&crate::ffi::StructExprField> for StructExprField {
781 fn from(value: &crate::ffi::StructExprField) -> Self {
782 StructExprField {
783 id: value.id(),
784 name: value.name().to_string(),
785 value: Expr::from(value.value()),
786 optional: value.optional(),
787 }
788 }
789}
790
791impl From<cxx::UniquePtr<crate::ffi::StructExprField>> for StructExprField {
792 fn from(value: cxx::UniquePtr<crate::ffi::StructExprField>) -> Self {
793 Self::from(&*value)
794 }
795}
796
797impl From<&MapExprEntry> for cxx::UniquePtr<crate::ffi::MapExprEntry> {
798 fn from(value: &MapExprEntry) -> Self {
799 use crate::ffi::MapExprEntry as FfiMapExprEntry;
800 let mut ffi_entry = FfiMapExprEntry::new();
801 ffi_entry.pin_mut().set_id(value.id);
802 ffi_entry.pin_mut().set_key((&value.key).into());
803 ffi_entry.pin_mut().set_value((&value.value).into());
804 ffi_entry.pin_mut().set_optional(value.optional);
805 ffi_entry
806 }
807}
808
809impl From<MapExprEntry> for cxx::UniquePtr<crate::ffi::MapExprEntry> {
810 fn from(value: MapExprEntry) -> Self {
811 Self::from(&value)
812 }
813}
814
815impl From<&crate::ffi::MapExprEntry> for MapExprEntry {
816 fn from(value: &crate::ffi::MapExprEntry) -> Self {
817 MapExprEntry {
818 id: value.id(),
819 key: Expr::from(value.key()),
820 value: Expr::from(value.value()),
821 optional: value.optional(),
822 }
823 }
824}
825
826impl From<cxx::UniquePtr<crate::ffi::MapExprEntry>> for MapExprEntry {
827 fn from(value: cxx::UniquePtr<crate::ffi::MapExprEntry>) -> Self {
828 Self::from(&*value)
829 }
830}