marlowe_lang 0.2.1

experimental parser lib for Cardano Marlowe DSL
Documentation
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
use std::collections::HashMap;

use serde::Deserialize;
use crate::types::marlowe::*;

#[cfg(feature="infinite-recursion")]
pub fn deserialize<T>(json:String) -> Result<T,String> 
where T : serde::de::DeserializeOwned + std::marker::Send + 'static {
    let work = std::thread::Builder::new().stack_size(32 * 1024 * 1024).spawn(move ||{
        let mut deserializer = serde_json::Deserializer::from_str(&json);
        deserializer.disable_recursion_limit();        
        let deserializer = serde_stacker::Deserializer::new(&mut deserializer);        
        T::deserialize(deserializer)
    });
    match work {
        Ok(handle) => {
            match handle.join() {
                Ok(Ok(v)) => Ok(v),
                Ok(Err(e)) => Err(format!("inner error: {:?}",e)),
                Err(e) => match e.downcast_ref::<String>() {
                    Some(as_string) => Err(as_string.into()),
                    None => Err("something went terribly wrong.".into())
                }
            }
        },
        Err(e) => Err(format!("exc: {:?}",e))
    }
}


#[cfg(not(feature="infinite-recursion"))]
pub fn deserialize<T>(json:String) -> Result<T,String> 
where T : serde::de::DeserializeOwned + std::marker::Send + 'static {
    serde_json::de::from_str(&json).map_err(|e| format!("{e:?}"))
}

struct TimeoutVisitor;
impl<'de> serde::de::Visitor<'de> for TimeoutVisitor {
    type Value = Timeout;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "a valid timeout object")
    }
    fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E> {
        Ok(Timeout::TimeConstant(value))
    }
    fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
        Ok(Timeout::TimeConstant(value as i64))
    }
}

struct ValueIdVisitor;
impl<'de> serde::de::Visitor<'de> for ValueIdVisitor {
    type Value = ValueId;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "a valid valueid object")
    }
    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> 
    where E: serde::de::Error {
        Ok(ValueId::Name(value.to_string()))          
    }
}

struct TokenVisitor;
impl<'de> serde::de::Visitor<'de> for TokenVisitor {
    type Value = Token;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "a valid token object")
    }

    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: serde::de::MapAccess<'de>
    {

        let mut token_name = None;
        let mut currency_symbol = None;
        
        while let Some(k) = map.next_key::<&str>()? {
            if k == "token_name" {
                token_name = Some(map.next_value()?);
            }
            else if k == "currency_symbol" {
                currency_symbol = Some(map.next_value()?);
            }
            else {
                return Err(serde::de::Error::custom(&format!("Invalid key in action object: {}", k)));
            }
        }

        if token_name.is_none() && currency_symbol.is_none() {
            return Err(serde::de::Error::custom(&"Invalid token object".to_string()));
        }

        Ok(Token {
            currency_symbol: currency_symbol.map_or_else(
                || Err(serde::de::Error::custom(&format!("missing currency symbol"))),
                Ok

            )?,
            token_name: token_name.map_or_else(
                || Err(serde::de::Error::custom(&format!("missing token name"))),
                Ok

            )?,
        })

    }
}



struct InputActionVisitor;
impl<'de> serde::de::Visitor<'de> for InputActionVisitor {
    type Value = InputAction;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "a valid input action object")
    }

    
    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> 
    where E: serde::de::Error {
        
        if value == "input_notify" {
            Ok(InputAction::Notify)            
        } else {
            Err(serde::de::Error::custom(format!("Not a valid input action: {}",value)))
        }
    }


    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: serde::de::MapAccess<'de>
    {

        // DEPOSIT
        let mut input_from_party = None;
        let mut that_deposits = None;
        let mut of_token = None;
        let mut into_account = None;
        
        // CHOICE
        let mut input_that_chooses_num = None;
        let mut for_choice_id = None;
        
        
        while let Some(k) = map.next_key::<&str>()? {
            if k == "input_from_party" {
                input_from_party = Some(map.next_value()?);
            }
            else if k == "that_deposits" {
                that_deposits = Some(map.next_value()?);
            }
            else if k == "of_token" {
                of_token = Some(map.next_value()?);
            }
            else if k == "into_account" {
                into_account = Some(map.next_value()?);
            }
            else if k == "input_that_chooses_num" {
                input_that_chooses_num = Some(map.next_value()?);
            }
            else if k == "for_choice_id" {
                for_choice_id = Some(map.next_value()?);
            }
            else {
                return Err(serde::de::Error::custom(&format!("Invalid key in input action object: {}", k)));
            }
        }

        if  let Some(inp) = input_from_party &&
            let Some(tdp) = that_deposits &&
            let Some(oft) = of_token &&
            let Some(int) = into_account  {
            return Ok(InputAction::Deposit { 
                into_account:Some(int), 
                input_from_party:Some(inp), 
                of_tokens: Some(oft), 
                that_deposits: tdp
            })
        }

        if  let Some(a) = input_that_chooses_num &&
            let Some(b) = for_choice_id 
        {
            return Ok(InputAction::Choice { 
                for_choice_id: Some(a), 
                input_that_chooses_num: b 
            })
        }

        Err(serde::de::Error::custom(&"Invalid input action object".to_string()))

    }
}



struct ActionVisitor;
impl<'de> serde::de::Visitor<'de> for ActionVisitor {
    type Value = Action;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "a valid action object")
    }

    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: serde::de::MapAccess<'de>
    {
        // DEPOSIT:
        let mut party : Option<Party> = None;
        let mut of_token : Option<Token> = None;
        let mut into_account : Option<AccountId> = None;
        let mut deposits : Option<Value> = None;

        // NOTIFY:
        let mut notify_if : Option<Observation> = None;

        // CHOICE:
        let mut for_choice = None;
        let mut choose_between : Option<Vec<Bound>> = None;

        while let Some(k) = map.next_key::<&str>()? {
            if k == "party" {
                party = Some(map.next_value()?);
            }
            else if k == "of_token" {
                of_token = Some(map.next_value()?);
            }
            else if k == "deposits" {
                deposits = Some(map.next_value()?);
            }
            else if k == "into_account" {
                into_account = Some(map.next_value()?);
            }
            else if k == "notify_if" {
                notify_if = Some(map.next_value()?);
            }
            else if k == "for_choice" {
                for_choice = Some(map.next_value()?);
            }
            else if k == "choose_between" {
                choose_between = Some(map.next_value()?);
            }
            else {
                return Err(serde::de::Error::custom(&format!("Invalid key in action object: {}", k)));
            }
        }

        // NOTIFY:
        if notify_if.is_some() {
            return Ok(Action::Notify { notify_if } )
        }
        
        
        // DEPOSIT:
        if party.is_some() && of_token.is_some() && into_account.is_some() && deposits.is_some() {
            return Ok(Action::Deposit { 
                into_account, 
                party, 
                of_token, 
                deposits 
            })
        }

        // CHOICE:        
        if for_choice.is_some() && choose_between.is_some()  {
            return Ok(Action::Choice { 
                for_choice, 
                choose_between: choose_between.map_or_else(
                    || Err(serde::de::Error::custom(&format!("missing bounds"))),
                    Ok

                )?.iter()
                    .map(|x|Some(x.to_owned())).collect::<Vec<Option<Bound>>>()
            })
        }

        Err(serde::de::Error::custom(&"Invalid action object!".to_string()))

    }
}





struct BoundVisitor;
impl<'de> serde::de::Visitor<'de> for BoundVisitor {
    type Value = Bound;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "a valid bound object")
    }

    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: serde::de::MapAccess<'de>
    {
        let mut from : Option<i64> = None;
        let mut to : Option<i64> = None;
        
        while let Some(k) = map.next_key::<&str>()? {
            if k == "from" {
                from = Some(map.next_value()?);
            }
            else if k == "to" {
                to = Some(map.next_value()?);
            }
            else {
                return Err(serde::de::Error::custom(&format!("Invalid key in Bound object: {}", k)));
            }
        }

        if let Some(from) = from && let Some(to) = to {
            Ok(Bound(from,to))
        } else {
            Err(serde::de::Error::custom("Invalid bound item!"))
        }

    }
}



struct PartyVisitor;
impl<'de> serde::de::Visitor<'de> for PartyVisitor {
    type Value = Party;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "a valid party object")
    }

    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: serde::de::MapAccess<'de>
    {
        let mut role_token : Option<String> = None;
        let mut address : Option<String> = None;
        
        while let Some(k) = map.next_key::<&str>()? {
            if k == "address" {
                address = Some(map.next_value()?);
            }
            else if k == "role_token" {
                role_token = Some(map.next_value()?);
            }
            else {
                return Err(serde::de::Error::custom(&format!("Invalid key in Party object: {}", k)));
            }
        }

        if let Some(addr) = address {
            match Address::from_bech32(&addr) {
                Ok(baddr) => return Ok(Party::Address(baddr)),
                Err(e) => return Err(
                    serde::de::Error::custom(
                        &format!("Invalid party address: {}. {}", addr,e)))
            }                
        }
        
        if let Some(role) = role_token {
            return Ok(Party::Role {
                role_token: role
            });
        }
        
        Err(serde::de::Error::custom("Invalid Party item!"))

    }
}





struct PayeeVisitor;
impl<'de> serde::de::Visitor<'de> for PayeeVisitor {
    type Value = Payee;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "a valid Payee object")
    }

    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: serde::de::MapAccess<'de>
    {
        let mut account = None;
        let mut party = None;

        while let Some(k) = map.next_key::<&str>()? {
            if k == "account" {
                account = Some(map.next_value()?);
            }
            else if k == "party" {
                party = Some(map.next_value()?);
            }
            else {
                return Err(serde::de::Error::custom(&format!("Invalid key in Payee object: {}", k)));
            }
        }
        
        if account.is_some() {
            return Ok(Payee::Account(account))
        }

        if party.is_some() {
            return Ok(Payee::Party(party))
        }

        Err(serde::de::Error::custom("Invalid Payee item!"))

    }
}




struct ChoiceIdVisitor;
impl<'de> serde::de::Visitor<'de> for ChoiceIdVisitor {
    type Value = ChoiceId;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "a valid choiceid object")
    }

    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: serde::de::MapAccess<'de>
    {
        let mut choice_name = None;
        let mut choice_owner = None;

        while let Some(k) = map.next_key::<&str>()? {
            if k == "choice_name" {
                choice_name = Some(map.next_value()?);
            }
            else if k == "choice_owner" {
                choice_owner = Some(map.next_value()?);
            }
            else {
                return Err(serde::de::Error::custom(&format!("Invalid key in ChoiceId object: {}", k)));
            }
        }

        if choice_name.is_none() || choice_owner.is_none() {
            return Err(serde::de::Error::custom("Missing choice_name or choice_owner"));
        }

        Ok(ChoiceId{
            choice_name: choice_name.map_or_else(
                || Err(serde::de::Error::custom(&format!("missing choice name"))),
                Ok

            )?,
            choice_owner
        })
    }
}




struct ContractVisitor;
impl<'de> serde::de::Visitor<'de> for ContractVisitor {
    type Value = Contract;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "a valid contract object")
    }

    
    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> 
    where E: serde::de::Error {
        
        if value == "close" {
            Ok(Contract::Close)            
        } else {
            Err(serde::de::Error::custom("Not a valid contract.".to_string()))
        }
    }

    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: serde::de::MapAccess<'de> 
    {

        // PAY
        let mut from_account : Option<AccountId>  = None;
        let mut to : Option<Payee> = None;
        let mut token: Option<Token> = None;
        let mut pay : Option<Value> = None;
        // let r#then : Option<Contract> = None; <-- overlapping 

        // IF
        let mut r#if : Option<Observation> = None;
        let mut then : Option<Contract> = None;
        let mut r#else : Option<Contract> = None;

        // WHEN
        let mut when : Option<Vec<Case>> = None;
        let mut timeout : Option<i64> = None;
        let mut timeout_continuation : Option<Contract> = None;

        // LET
        let mut r#let : Option<ValueId> = None;
        let mut be : Option<Value> = None;
        // let r#else : Option<Contract> = None; <-- overlapping
        
        // ASSERT
        let mut assert : Option<Observation> = None;
        // let r#then : Option<Contract> = None; <-- overlapping   
        
        while let Some(k) = map.next_key::<&str>()? {
            if k == "assert" {
                assert = Some(map.next_value()?);
            }
            else if k == "then" {
                
                r#then = Some(map.next_value()?);
            }
            else if k == "to" {
                to = Some(map.next_value()?);
            }
            else if k == "from_account" {
                from_account = Some(map.next_value()?);
            }
            else if k == "token" {
                token = Some(map.next_value()?);
            }
            else if k == "pay" {
                pay = Some(map.next_value()?);
            }
            else if k == "if" {
                r#if = Some(map.next_value()?);
            }
            else if k == "else" {
                r#else = Some(map.next_value()?);
            }
            else if k == "when" {
                when = Some(map.next_value()?);
            }
            else if k == "timeout_continuation" {
                timeout_continuation = Some(map.next_value()?);
            }
            else if k == "let" {
                r#let = Some(map.next_value()?);
            }
            else if k == "be" {
                be = Some(map.next_value()?);
            }
            else if k == "timeout" {
                timeout = Some(map.next_value()?);
            }
            else {
                return Err(serde::de::Error::custom(&format!("Invalid key in Contract object: {}", k)));
            }
        }

        // WHEN CONTRACT
        if timeout_continuation.is_some() && timeout.is_some() && when.is_some() {
            return Ok(Contract::When { 
                when: when.map_or_else(
                    || Err(serde::de::Error::custom(&format!("missing when contract continuation"))),
                    Ok

                )?.iter().map(|x|Some(x.clone())).collect::<Vec<Option<Case>>>(), 
                timeout: Some(Timeout::TimeConstant(timeout.map_or_else(
                    || Err(serde::de::Error::custom(&format!("missing timeout"))),
                    Ok

                )?)), 
                timeout_continuation: Some(timeout_continuation.map_or_else(
                    || Err(serde::de::Error::custom(&format!("missing timeout continuation"))),
                    Ok

                )?.boxed())
            })
        }

        // IF CONTRACT
        if r#if.is_some() && then.is_some() && r#else.is_some() {
            return Ok(Contract::If { 
                x_if: r#if, 
                then: Some(Box::new(then.map_or_else(
                    || Err(serde::de::Error::custom(&format!("missing IF contract continuation (then)"))),
                    Ok

                )?)), 
                x_else: Some(Box::new(r#else.map_or_else(
                    || Err(serde::de::Error::custom(&format!("missing IF contract continuation (else)"))),
                    Ok

                )?)) 
            })
        }

        // LET CONTRACT
        if r#let.is_some() && be.is_some() && then.is_some() {
            return Ok(Contract::Let {
                x_let: r#let.map_or_else(
                    || Err(serde::de::Error::custom(&format!("missing LET contract valueId (let)"))),
                    Ok

                )?,
                be: Some(Box::new(be.map_or_else(
                    || Err(serde::de::Error::custom(&format!("missing LET contract value (be)"))),
                    Ok

                )?)),
                then: Some(Box::new(then.map_or_else(
                    || Err(serde::de::Error::custom(&format!("missing LET contract continuation (then)"))),
                    Ok

                )?)),
            })
        }
        
        // PAY CONTRACT
        if from_account.is_some() && to.is_some() && token.is_some() && pay.is_some() && r#then.is_some() {
            return Ok(Contract::Pay { 
                from_account, 
                to, 
                token, 
                pay, 
                then: Some(Box::new(r#then.map_or_else(
                    || Err(serde::de::Error::custom(&format!("missing PAY contract continuation (then)"))),
                    Ok

                )?))
            })
        }

        // ASSERT CONTRACT
        if assert.is_some() && r#then.is_some() {
            return Ok(Contract::Assert {
                assert,
                then: Some(Box::new(r#then.map_or_else(
                    || Err(serde::de::Error::custom(&format!("missing ASSERT contract continuation (then)"))),
                    Ok

                )?)),
            })
        }

        panic!("UNKNOWN CONTRACT TYPE!");

    }
}







struct CaseVisitor;
impl<'de> serde::de::Visitor<'de> for CaseVisitor {
    type Value = Case;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "a valid case object")
    }

    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: serde::de::MapAccess<'de>
    {

        let mut then : Option<Contract> = None;
        let mut merkleized_then : Option<String> = None;
        let mut case = None;
        
        while let Some(k) = map.next_key::<&str>()? {
            if k == "then" {
                then = Some(map.next_value()?);
            }
            else if k == "merkleized_then" {
                merkleized_then = Some(map.next_value()?);
            }
            else if k == "case" {
                case = Some(map.next_value()?);
            }
            else {
                return Err(serde::de::Error::custom(&format!("Invalid key in Case object: '{}'", k)));
            }
        }

        if (then.is_none()&&merkleized_then.is_none()) && case.is_none() {
            return Err(serde::de::Error::custom(&"Missing data in case!".to_string()));
        }

        if merkleized_then.is_some() {
            Ok(Case { case:Some(case.map_or_else(
                || Err(serde::de::Error::custom(&format!("missing CASE action"))),
                Ok

            )?), then:Some(
                PossiblyMerkleizedContract::Merkleized(merkleized_then.map_or_else(
                    || Err(serde::de::Error::custom(&format!("missing CASE continuation merkleized data (merkleized_then)"))),
                    Ok

                )?)) })
        } else {
            Ok(Case { case:Some(case.map_or_else(
                || Err(serde::de::Error::custom(&format!("missing CASE action"))),
                Ok

            )?), then:Some(
                PossiblyMerkleizedContract::Raw(Box::new(then.map_or_else(
                    || Err(serde::de::Error::custom(&format!("missing CASE continuation (then)"))),
                    Ok

                )?))) })
        }
        

    }
}




// pub accounts : HashMap<(Party,Token),i64>, // Accounts: Map (AccountId, Token) Integer
// // 2 , choices
// pub choices : HashMap<ChoiceId,i64> , // Map ChoiceId ChosenNum
// // 3 , bound vals
// pub bound_values : HashMap<String,i64>, // Map ValueId Integer




struct StateVisitor;
impl<'de> serde::de::Visitor<'de> for StateVisitor {
    type Value = State;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "a valid state object")
    }

    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: serde::de::MapAccess<'de>
    {
        
        let mut accounts : Option<Vec<((Party,Token),u64)>> = None;
        let mut choices : Option<Vec<(ChoiceId,i64)>> = None;
        let mut bound_values : Option<Vec<(ValueId,i64)>> = None;
        let mut min_time : Option<u64> = None;
        
        while let Some(k) = map.next_key::<&str>()? {
            if k == "accounts" {
                accounts = Some(map.next_value()?);
            }
            else if k == "choices" {
                choices = Some(map.next_value()?);
            }
            else if k == "boundValues" {
                bound_values = Some(map.next_value()?);
            }
            else if k == "minTime" {
                min_time = Some(map.next_value()?);
            }
            else {
                return Err(serde::de::Error::custom(&format!("Invalid key in Case object: '{}'", k)));
            }
        }

        if let Some(accs) = accounts &&
            let Some(choi) = choices &&
            let Some(boun) = bound_values &&
            let Some(min) = min_time {
            
            let mut accs_hash = HashMap::new();
            let mut choi_hash = HashMap::new();
            let mut boun_hash = HashMap::new();

            for x in accs {
                accs_hash.insert(
                    (
                        x.0.0,
                        x.0.1
                    ),
                    x.1
                );
            }
            for x in choi {choi_hash.insert(x.0,x.1);}
            for x in boun {boun_hash.insert(x.0,x.1);}

            return Ok(State {
                accounts: accs_hash,
                choices: choi_hash,
                bound_values: boun_hash,
                min_time: min,
            })
        } 

        Err(serde::de::Error::custom(&"Invalid state object.".to_string()))
        

    }
}


struct ObservationVisitor;
impl<'de> serde::de::Visitor<'de> for ObservationVisitor {
    type Value = Observation;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "a valid observation")
    }

    fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
        where
            E: serde::de::Error, {
        if v {
            Ok(Observation::True)
        } else {
            Ok(Observation::False)
        }
    }

    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: serde::de::MapAccess<'de>
    {

        // AndObs
        let mut both : Option<Observation> = None;
        let mut and : Option<Observation> = None;

        // OrObs
        let mut either : Option<Observation> = None;
        let mut or : Option<Observation> = None;

        // NotObs
        let mut not : Option<Observation> = None;

        // ChoseSomething
        let mut chose_something_for : Option<ChoiceId> = None;

        // ValueGE
        let mut value : Option<Value> = None;
        let mut ge_than : Option<Value> = None;

        // ValueGT
        //let mut value : Option<Value> = None;
        let mut gt : Option<Value> = None;

        // ValueLT
        //let mut value : Option<Value> = None;
        let mut lt : Option<Value> = None;

        // ValueLE
        //let mut value : Option<Value> = None;
        let mut le_then : Option<Value> = None;

        // ValueEQ
        //let mut value : Option<Value> = None;
        let mut equal_to : Option<Value> = None;

        
        while let Some(k) = map.next_key::<&str>()? {
            if k == "value" {
                value = Some(map.next_value()?);
            } else if k == "equal_to" {
                equal_to = Some(map.next_value()?);
            } else if k == "le_then" {
                le_then = Some(map.next_value()?);
            } else if k == "lt" {
                lt = Some(map.next_value()?);
            } else if k == "ge_than" {
                ge_than = Some(map.next_value()?);
            } else if k == "choice_id" {
                chose_something_for = Some(map.next_value()?);
            } else if k == "not" {
                not = Some(map.next_value()?);
            } else if k == "and" {
                and = Some(map.next_value()?);
            } else if k == "or" {
                or = Some(map.next_value()?);
            } else if k == "both" {
                both = Some(map.next_value()?);
            } else if k == "either" {
                either = Some(map.next_value()?);
            } else if k == "gt" {
                gt = Some(map.next_value()?);
            } else {
                return Err(serde::de::Error::custom(&format!("Invalid key in Observation object: '{}'", k)));
            }
        }

        // AndObs
        if let Some(a) = both && let Some(b) = and {
            return Ok(Observation::AndObs { both: Some(Box::new(a)), and: Some(Box::new(b)) })
        }

        // OrObs
        if let Some(a) = either && let Some(b) = or {
            return Ok(Observation::OrObs {
                either: Some(Box::new(a)),
                or: Some(Box::new(b)),
            })
        }

        // NotObs
        if let Some(a) = not {
            return Ok(Observation::NotObs {not:Some(Box::new(a))})
        }

        // ChoseSomething
        if let Some(a) = chose_something_for {
            return Ok(Observation::ChoseSomething(Some(a)))
        }

        // ValueGE
        if let Some(a) = &value && let Some(b) = ge_than {
            return Ok(Observation::ValueGE { 
                value:Some(Box::new(a.clone())), 
                ge_than:Some(Box::new(b))
            })
        } 

        // ValueGT
        if let Some(a) = &value && let Some(b) = gt {
            return Ok(Observation::ValueGT { 
                value:Some(Box::new(a.clone())), 
                gt_than:Some(Box::new(b))
            })
        }

        // ValueLT
        if let Some(a) = &value && let Some(b) = lt {
            return Ok(Observation::ValueLT { 
                value:Some(Box::new(a.clone())), 
                lt_than:Some(Box::new(b))
            })
        }

        // ValueLE
        if let Some(a) = &value && let Some(b) = le_then {
            return Ok(Observation::ValueLE { 
                value:Some(Box::new(a.clone())), 
                le_than:Some(Box::new(b))
            })
        }

        // ValueEQ
        if let Some(a) = &value && let Some(b) = equal_to {
            return Ok(Observation::ValueEQ { 
                value:Some(Box::new(a.clone())), 
                equal_to:Some(Box::new(b))
            })
        }
        

        Err(serde::de::Error::custom(&"Invalid Observation object!".to_string()))

    }

}


struct ValueVisitor;
impl<'de> serde::de::Visitor<'de> for ValueVisitor {
    type Value = Value;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        
        write!(formatter, "a valid Value object")
    }


    fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E> {
        Ok(Value::ConstantValue(value))
    }

    fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
        Ok(Value::ConstantValue(value as i64))
    }
    

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> 
    where E: serde::de::Error {
        if value == "time_interval_start" {
            return Ok(Value::TimeIntervalStart)
        } 
        if value == "time_interval_end" {
            return Ok(Value::TimeIntervalEnd)
        }
        Err(serde::de::Error::custom(format!("unknown value: ('{value}')")))
    }

    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: serde::de::MapAccess<'de>
    {

        // NegValue
        let mut negate : Option<Value> = None;

        // AvailableMoney
        let mut amount_of_token : Option<Token> = None;
        let mut in_account : Option<AccountId> = None;
        
        // AddValue
        let mut add : Option<Value> = None;
        let mut and : Option<Value> = None;

        // SubValue
        let mut value : Option<Value> = None;
        let mut minus : Option<Value> = None;

        // MulValue
        let mut multiply : Option<Value> = None;
        let mut times : Option<Value> = None;
        
        // DivValue
        let mut divide : Option<Value> = None;
        let mut by : Option<Value> = None;
        
        // ChoiceValue
        let mut value_of_choice : Option<ChoiceId> = None;
        
        // UseValue
        let mut use_value : Option<ValueId> = None;
        
        // Condition
        let mut r#if : Option<Observation> = None;
        let mut then : Option<Value> = None;
        let mut r#else : Option<Value> = None;

        while let Some(k) = map.next_key::<&str>()? {

            if k ==  "else" {
                r#else = Some(map.next_value()?);
            } else if k == "then" {
                then = Some(map.next_value()?);
            } else if k == "if" {
                r#if = Some(map.next_value()?);
            } else if k == "use_value" {
                use_value = Some(map.next_value()?);
            } else if k == "value_of_choice" {
                value_of_choice = Some(map.next_value()?);
            } else if k == "by" {
                by = Some(map.next_value()?);
            } else if k == "divide" {
                divide = Some(map.next_value()?);
            } else if k == "times" {
                times = Some(map.next_value()?);
            } else if k == "multiply" {
                multiply = Some(map.next_value()?);
            } else if k == "minus" {
                minus = Some(map.next_value()?);
            } else if k == "value" {
                value = Some(map.next_value()?);
            } else if k == "and" {
                and = Some(map.next_value()?);
            } else if k == "add" {
                add = Some(map.next_value()?);
            } else if k == "in_account" {
                in_account = Some(map.next_value()?);
            } else if k == "amount_of_token" {
                amount_of_token = Some(map.next_value()?);
            } else if k == "negate" {
                negate = Some(map.next_value()?);
            } else {
                return Err(serde::de::Error::custom(&format!("Invalid key in Value object: '{}'", k)));
            }
        }

        // UseValue
        if let Some(v) = use_value {
            return Ok(Value::UseValue(v))
        }

        // AvailableMoney 
        if amount_of_token.is_some() && in_account.is_some() {
            return Ok(Value::AvailableMoney(in_account, amount_of_token))
        }

        // ChoiceValue
        if value_of_choice.is_some() {
            return Ok(Value::ChoiceValue(value_of_choice))
        }

        // SubValue
        if let Some(a) = value && let Some(b) = minus {
            return Ok(
                Value::SubValue(
                    Some(Box::new(a)),
                    Some(Box::new(b))
                )
            )
        }

        // Negate
        if let Some(v) = negate {
            return Ok(Value::NegValue(Some(Box::new(v))))
        }

        // DivValue
        if let Some(a) = divide && let Some(b) = by {
            return Ok(
                Value::DivValue(
                    Some(Box::new(a)),
                    Some(Box::new(b))
                )
            )
        }

        // AddValue
        if let Some(a) = add && let Some(b) = and {
            return Ok(
                Value::AddValue(
                    Some(Box::new(a)),
                    Some(Box::new(b))
                )
            )
        }

        // MulValue
        if let Some(a) = multiply && let Some(b) = times {
            return Ok(
                Value::MulValue(
                    Some(Box::new(a)),
                    Some(Box::new(b))
                )
            )
        }

        // Condition
        if let Some(_if) = r#if && let Some(_then) = then && let Some(_else) = r#else {
            return Ok(
                Value::Cond(
                    Some(_if), 
                    Some(Box::new(_then)), 
                    Some(Box::new(_else))
                )
            )
        }
        


        Err(serde::de::Error::custom(&"Invalid value object!".to_string()))
        
    }

}


// ==================================== IMPL DESERIALIZE ====================================

impl<'de> Deserialize<'de> for Value {    
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: serde::Deserializer<'de> { 
            deserializer.deserialize_any(ValueVisitor)
    }
}

impl<'de> Deserialize<'de> for Token {    
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: serde::Deserializer<'de> { 
            deserializer.deserialize_map(TokenVisitor)
    }
}

impl<'de> Deserialize<'de> for Party {    
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: serde::Deserializer<'de> { 
            deserializer.deserialize_map(PartyVisitor)
    }
}
impl<'de> Deserialize<'de> for Case {    
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: serde::Deserializer<'de> { 
            deserializer.deserialize_map(CaseVisitor)
    }
}

impl<'de> Deserialize<'de> for Contract {    
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: serde::Deserializer<'de> { 
            deserializer.deserialize_any(ContractVisitor)
    }
}

impl<'de> Deserialize<'de> for ChoiceId {    
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: serde::Deserializer<'de> { 
            deserializer.deserialize_map(ChoiceIdVisitor)

    }
}

impl<'de> Deserialize<'de> for Bound {    
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: serde::Deserializer<'de> { 
            deserializer.deserialize_any(BoundVisitor)
    }
}


impl<'de> Deserialize<'de> for Payee {    
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: serde::Deserializer<'de> { 
            deserializer.deserialize_map(PayeeVisitor)
    }
}

impl<'de> Deserialize<'de> for Action {    
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: serde::Deserializer<'de> { 
            deserializer.deserialize_map(ActionVisitor)
    }
}

impl<'de> Deserialize<'de> for Observation {    
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: serde::Deserializer<'de> { 
            deserializer.deserialize_any(ObservationVisitor)

    }
}

impl<'de> Deserialize<'de> for InputAction {    
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: serde::Deserializer<'de> { 
            
            deserializer.deserialize_map(InputActionVisitor)

    }
}


impl<'de> Deserialize<'de> for State {    
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: serde::Deserializer<'de> { 
            deserializer.deserialize_map(StateVisitor)

    }
}

impl<'de> Deserialize<'de> for ValueId {    
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: serde::Deserializer<'de> { 
            deserializer.deserialize_str(ValueIdVisitor)

    }
}