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
extern crate yaml_rust;
extern crate rpds;

#[macro_use]
extern crate serde_derive;

#[cfg(feature = "topyobject")]
extern crate pyo3;

#[cfg(feature = "topyobject")]
mod context_py {
    use pyo3::prelude::*;
    use pyo3::Python;
    use pyo3::types::PyDict;
//     use pyo3::ObjectProtocol;
//     use pyo3::class::basic::{PyObjectProtocol};

//     // class Context(dict):
//     //     def __getattr__(self, name):
//     //         return self[name]

//     #[pyclass(dict)]
//     struct Context {}

//     #[pyproto]
//     impl PyObjectProtocol for Context {
//         fn __getattr__(&self, name: &PyString) -> PyResult<()> {
//             let a = self.get_base().get_item(name);
//             Ok(())
//         }
//     }

    /// Wrap a Python dict into a type equivalent to this:
    /// 
    /// ```python
    /// class Context(dict):
    ///     def __getattr__(self, name):
    ///         return self[name]
    /// ```
    /// 
    /// This type is used to pass contexts around in Python, which makes it easier to refer to their elements
    /// using the attribute syntax instead of an indexer + string, e.g.
    /// 
    /// ```python
    /// # no more ctx['var'], just
    /// ctx.var
    /// ```
    pub fn wrapper(obj: PyObject, py: Python) -> PyObject {
        let locals = PyDict::new(py);
        locals.set_item("obj", obj).unwrap();
        py.eval("type('Context', (dict,), { '__getattr__': lambda self, name: self[name] })(obj)", None, Some(&locals)).unwrap().to_object(py)
    }
}

pub mod context {
    use yaml_rust::{Yaml, YamlLoader, YamlEmitter};
    use std::ops::Index;
    use rpds::HashTrieMap;
    use std::fmt::{Display, Formatter};

    #[cfg(feature = "topyobject")]
    use pyo3::prelude::*;
    #[cfg(feature = "topyobject")]
    use pyo3::Python;
    #[cfg(feature = "topyobject")]
    use pyo3::types::{PyDict, PyString, PyList};

    /// A data structure designed to provide a high-level language agnostic calling convention.
    #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
    pub struct Context {
        data: HashTrieMap<String, CtxObj>
    }

    /// Enum of all primitive types that a Ccntext is composed of.
    #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
    pub enum CtxObj {
        Str(String),
        Bin(Vec<u8>),
        Int(i64),
        Real(f64),
        Bool(bool),
        Array(Vec<CtxObj>),
        Context(Context),
        None
    }

    /// The error type for the low-level unpack() functions.
    /// This error occurs when either the key does not exist or it is not feasible to convert into the deduced type.
    #[derive(Debug, Clone, PartialEq, Eq)]
    pub struct CtxObjUnpackError {}

    /// Unpack data from some context element into a Rust primitive/standard type.
    /// The destination type can usually be deduced to save type annotations as well.
    pub trait CtxObjUnpack: Sized {
        fn unpack(src: CtxObj) -> Option<Self>;
    }

    impl CtxObjUnpack for String {
        fn unpack(src: CtxObj) -> Option<Self> {
            if let CtxObj::Str(val) = src {
                Some(val)
            }
            else { None }
        }
    }

    impl CtxObjUnpack for i64 {
        fn unpack(src: CtxObj) -> Option<Self> {
            if let CtxObj::Int(val) = src {
                Some(val)
            }
            else { None }
        }
    }

    impl CtxObjUnpack for i32 {
        fn unpack(src: CtxObj) -> Option<Self> {
            if let CtxObj::Int(val) = src {
                Some(val as Self)
            }
            else { None }
        }
    }

    impl CtxObjUnpack for usize {
        fn unpack(src: CtxObj) -> Option<Self> {
            if let CtxObj::Int(val) = src {
                Some(val as Self)
            }
            else { None }
        }
    }

    impl CtxObjUnpack for f64 {
        fn unpack(src: CtxObj) -> Option<Self> {
            if let CtxObj::Real(val) = src {
                Some(val)
            }
            else { None }
        }
    }

    impl CtxObjUnpack for bool {
        fn unpack(src: CtxObj) -> Option<Self> {
            if let CtxObj::Bool(val) = src {
                Some(val)
            }
            else { None }
        }
    }

    impl From<Yaml> for CtxObj {
        fn from(src: Yaml) -> CtxObj {
            match src {
                Yaml::String(val) => { CtxObj::Str(val.to_owned()) },
                Yaml::Boolean(val) => { CtxObj::Bool(val.to_owned()) },
                Yaml::Integer(val) => { CtxObj::Int(val.to_owned()) },
                Yaml::Real(val) => { CtxObj::Real(val.parse().unwrap()) }
                Yaml::Null => { CtxObj::None },
                Yaml::Hash(_) => { CtxObj::Context(Context::from(src)) },
                Yaml::Array(val) => {
                    CtxObj::Array(val.iter().map(|i| { CtxObj::from(i.clone()) }).collect()) 
                },
                Yaml::Alias(_val) => {
                    unimplemented!();
                },
                Yaml::BadValue => { unreachable!(); },
            }
        }
    }

    impl Into<Yaml> for CtxObj {
        fn into(self) -> Yaml {
            match self {
                CtxObj::Str(val) => Yaml::String(val.to_owned()),
                CtxObj::Bool(val) => Yaml::Boolean(val.to_owned()),
                CtxObj::Int(val) => Yaml::Integer(val.to_owned()),
                CtxObj::Real(val) => Yaml::Real(val.to_string()),
                CtxObj::None => Yaml::Null,
                CtxObj::Context(val) => val.clone().into(),
                CtxObj::Array(val) => Yaml::Array(val.iter().map(|i| {i.clone().into()}).collect()),
                CtxObj::Bin(_) => unimplemented!()
            }
        }
    }

    #[cfg(feature = "topyobject")]
    impl ToPyObject for CtxObj {
        fn to_object(&self, py: Python) -> PyObject {
            match self {
                CtxObj::None => py.None(),
                CtxObj::Str(val) => val.to_object(py),
                CtxObj::Bool(val) => val.to_object(py),
                CtxObj::Int(val) => val.to_object(py),
                CtxObj::Real(val) => val.to_object(py),
                CtxObj::Context(val) => val.to_object(py),
                CtxObj::Array(val) => {
                    let tmp: Vec<PyObject> = val.iter().map(|i| {i.to_object(py)}).collect();
                    PyList::new(py, &tmp).to_object(py)
                },
                CtxObj::Bin(val) => val.to_object(py)
            }
        }
    }

    impl From<Yaml> for Context {
        fn from(src: Yaml) -> Self {
            let mut context_data = HashTrieMap::new();
            if let Yaml::Hash(raw) = src {
                for (k, v) in raw {
                    if let Yaml::String(key) = k {
                        match v {
                            Yaml::String(_) | Yaml::Boolean(_) | Yaml::Integer(_) | Yaml::Real(_) | Yaml::Null | Yaml::Hash(_) | Yaml::Array(_) | Yaml::Alias(_) => {
                                context_data.insert_mut(key.to_owned(), CtxObj::from(v));
                            }
                            Yaml::BadValue => { }
                        }
                    }
                }
            }
            Context { data: context_data }
        }
    }

    impl<'a> From<&'a str> for Context {
        fn from(s: &str) -> Self {
            Context::from(YamlLoader::load_from_str(s).unwrap()[0].clone())
        }
    }

    impl Into<Yaml> for Context {
        fn into(self) -> Yaml {
            let mut map = yaml_rust::yaml::Hash::new();
            for (k, v) in self.data.iter() {
                map.insert(Yaml::String(k.to_owned()), v.to_owned().into());
            }
            Yaml::Hash(map)
        }
    }

    #[cfg(feature = "topyobject")]
    impl ToPyObject for Context {
        fn to_object(&self, py: Python) -> PyObject {
            let ctx = PyDict::new(py);
            for (k, v) in self.data.iter() {
                ctx.set_item(PyString::new(py, k), v.to_object(py)).unwrap();
            }
            crate::context_py::wrapper(ctx.to_object(py), py)
        }
    }

    impl Display for Context {
        fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
            let mut out_str = String::new();
            {
                let mut emitter = YamlEmitter::new(&mut out_str);
                emitter.dump(&self.clone().into()).unwrap();
            }
            write!(f, "{}", &out_str)
        }
    }

    impl<'a> Index<&'a str> for Context {
        type Output = CtxObj;

        fn index(&self, key: &'a str) -> &CtxObj {
            self.data.get(key).expect(&format!("Key error: {}", key))
        }
    }

    impl Context {
        /// Create an empty context.
        pub fn new() -> Context {
            Context { data: HashTrieMap::new() }
        }

        /// Overlay `another` on top of this context.
        /// 
        /// **Example**
        /// ```
        /// # extern crate ymlctx;
        /// # use ymlctx::context::Context;
        /// let eg = Context::from("a: 1\nb: 0");
        /// let another = Context::from("b: 1");
        /// assert_eq!(eg.overlay(&another), Context::from("a: 1\nb: 1"));
        /// ```
        pub fn overlay(&self, another: &Context) -> Context {
            let mut forward_snapshot = self.data.clone();
            for (k, v) in another.data.iter() {
                forward_snapshot.insert_mut(k.to_owned(), v.to_owned());
            }
            Context { data: forward_snapshot }
        }

        /// Use as a template and render it with other values
        /// 
        /// **Example**
        /// ```
        /// # extern crate ymlctx;
        /// # use ymlctx::context::Context;
        /// let template = Context::from("a: 1\nb: 0");
        /// let values = Context::from("b: 1\nc: 2");
        /// assert_eq!(template.render(&values), Context::from("a: 1\nb: 1"));
        /// ```
        /// 
        /// Note that this is recursive!
        /// 
        /// ```
        /// # extern crate ymlctx;
        /// # use ymlctx::context::Context;
        /// let template = Context::from("a: 1\nb:\n  b1: 0");
        /// let values = Context::from("b:\n  b1: 1\n  b2: 2\nc: 2");
        /// assert_eq!(template.render(&values), Context::from("a: 1\nb:\n  b1: 1"));
        /// ```
        pub fn render(&self, values: &Context) -> Context {
            let mut forward_snapshot = self.data.clone();
            for (k, v) in values.data.iter() {
                if forward_snapshot.contains_key(k) {
                    if let CtxObj::Context(subtemplate) = forward_snapshot[k].clone() {
                        if let CtxObj::Context(subvalues) = v {
                            forward_snapshot.insert_mut(k.to_owned(), CtxObj::Context(subtemplate.render(&subvalues)));
                        }
                    }
                    else {
                        forward_snapshot.insert_mut(k.to_owned(), v.to_owned()); 
                    }
                }
            }
            Context { data: forward_snapshot }
        }

        /// Set a context element.
        ///
        /// **Example**
        /// ```
        /// # extern crate ymlctx;
        /// # use ymlctx::context::{Context, CtxObj};
        /// let eg = Context::from("a: 1\nb: 0");
        /// assert_eq!(eg.set("b", CtxObj::Int(1)), Context::from("a: 1\nb: 1"));
        /// ```
        pub fn set(&self, key: &str, val: CtxObj) -> Context {
            Context { data: self.data.insert(key.to_owned(), val) }
        }

        /// Optionally set a context element. (Syntax sugar to adapt to Option<CtxObj>)
        ///
        /// **Example**
        /// ```
        /// # extern crate ymlctx;
        /// # use ymlctx::context::{Context, CtxObj};
        /// let eg = Context::from("a: 1\nb: 0");
        /// assert_eq!(eg.set_opt("b", Some(CtxObj::Int(1))), Context::from("a: 1\nb: 1"));
        /// assert_eq!(eg.set_opt("b", None), Context::from("a: 1\nb: 0"));
        /// ```
        pub fn set_opt(&self, key: &str, optional: Option<CtxObj>) -> Context {
            match optional {
                Some(val) => Context { data: self.data.insert(key.to_owned(), val) },
                None => self.clone()
            }
        }

        /// Get an element *by reference* if the key exists.
        ///
        /// **Example**
        /// ```
        /// # extern crate ymlctx;
        /// # use ymlctx::context::{Context, CtxObj, CtxObjUnpack};
        /// let eg = Context::from("a: 1");
        /// assert_eq!(eg.get("a").unwrap().clone(), CtxObj::Int(1));
        /// assert_eq!(eg.get("b"), None);
        /// ```
        pub fn get(&self, key: &str) -> Option<&CtxObj> {
            self.data.get(key)
        }

        /// Get an element *by value* if the key exists.
        ///
        /// **Example**
        /// ```
        /// # extern crate ymlctx;
        /// # use ymlctx::context::{Context, CtxObj, CtxObjUnpack};
        /// let eg = Context::from("a: 1");
        /// assert_eq!(eg.get_clone("a").unwrap(), CtxObj::Int(1));
        /// assert_eq!(eg.get_clone("b"), None);
        /// ```
        pub fn get_clone(&self, key: &str) -> Option<CtxObj> {
            match self.data.get(key) {
                Some(p) => Some(p.clone()),
                None => None
            }
        }

        /// Unpack an element by key if it exists.
        ///
        /// **Example**
        /// ```
        /// # extern crate ymlctx;
        /// # use ymlctx::context::{Context, CtxObj, CtxObjUnpack};
        /// let eg = Context::from("a: 1");
        /// let success: i64 = eg.unpack("a").unwrap();
        /// assert_eq!(success, 1);
        /// 
        /// /*Although, beware of auto-conversion due to polymorphism with the CtxObjUnpack trait.*/
        /// let success: i32 = eg.unpack("a").unwrap();
        /// assert_eq!(success, 1);
        /// ```
        /// This should fail because the key specified does not exist.
        /// ```rust,should_panic
        /// # extern crate ymlctx;
        /// # use ymlctx::context::{Context, CtxObj, CtxObjUnpack};
        /// let eg = Context::from("a: 1");
        /// let err: i64 = match eg.unpack("b") {
        ///     Ok(v) => v,
        ///     Err(_) => panic!()
        /// };
        /// ```
        /// 
        /// This should also fail because the type conversion is not feasible.
        /// ```rust,should_panic
        /// # extern crate ymlctx;
        /// # use ymlctx::context::{Context, CtxObj, CtxObjUnpack};
        /// let eg = Context::from("a: 1");
        /// let err: String = match eg.unpack("b") {
        ///     Ok(v) => v,
        ///     Err(_) => panic!()
        /// };
        /// ```
        pub fn unpack<T: CtxObjUnpack>(&self, key: &str) -> Result<T, CtxObjUnpackError> {
            if let Some(o) = self.data.get(key) {
                if let Some(v) = T::unpack(o.to_owned()) { Ok(v) }
                else { Err(CtxObjUnpackError{ }) }
            }
            else { Err(CtxObjUnpackError{ }) }
        }

        /// Enter a nested context.
        /// 
        /// **Example**
        /// 
        /// ```
        /// # extern crate ymlctx;
        /// # use ymlctx::context::{Context, CtxObj, CtxObjUnpack};
        /// let eg = Context::from("s:\n  a: 1");
        /// assert_eq!(eg.subcontext("s"), Some(Context::from("a: 1")));
        /// ```
        pub fn subcontext(&self, key: &str) -> Option<Context> {
            if let Some(CtxObj::Context(val)) = self.data.get(key) { Some(val.clone()) }
            else { None }
        }

        /// List nested contexts.
        /// 
        /// **Example**
        /// 
        /// ```
        /// # extern crate ymlctx;
        /// # use ymlctx::context::{Context, CtxObj, CtxObjUnpack};
        /// let eg = Context::from("s:\n- a: 1\n- b: 1");
        /// assert_eq!(eg.list_contexts("s"), Some(vec![Context::from("a: 1"), Context::from("b: 1")]));
        /// ```
        pub fn list_contexts(&self, key: &str) -> Option<Vec<Context>> {
            if let Some(CtxObj::Array(val)) = self.data.get(key) {
                let mut ret = Vec::new();
                for i in val.iter() {
                    if let CtxObj::Context(ctx) = i {
                        ret.push(ctx.clone());
                    }
                }
                return Some(ret);
            }
            else { None }
        }

        /// Hide an element by key if it exists.
        /// 
        /// **Example**
        /// 
        /// ```
        /// # extern crate ymlctx;
        /// # use ymlctx::context::{Context, CtxObj, CtxObjUnpack};
        /// let eg = Context::from("a: 1\nb: 1");
        /// assert_eq!(eg.hide("b"), Context::from("a: 1"));
        /// assert_eq!(eg.hide("c"), eg);
        /// ```
        pub fn hide(&self, key: &str) -> Context {
            Context { data: self.data.remove(key) }
        }

        /// Get all keys from this context.
        /// 
        /// **Example**
        /// 
        /// ```
        /// # extern crate ymlctx;
        /// # use ymlctx::context::{Context, CtxObj, CtxObjUnpack};
        /// let eg = Context::from("a: 1\nb: 1");
        /// let keys: std::collections::HashSet<String> = eg.keys().map(|k| { k.to_owned() }).collect();
        /// assert_eq!(keys, vec![String::from("a"), String::from("b")].into_iter().collect());
        /// ```
        pub fn keys(&self) -> rpds::map::hash_trie_map::IterKeys<String, CtxObj> {
            self.data.keys()
        }
    }
}

#[cfg(test)]
mod tests{
    use crate::context::Context;

    #[test]
    fn multiple_overwrites() {
        let a = Context::from("a: 1\nb: 0");
        let b = Context::from("a: 0\nb: 1");
        let c = a.overlay(&b);
        assert_eq!(c, b);
    }

    #[test]
    fn single_overwrite() {
        let a = Context::from("a: 1\nb: 0");
        let b = Context::from("b: 1");
        let c = a.overlay(&b);
        assert_eq!(c, Context::from("a: 1\nb: 1"));
    }

    #[test]
    fn insertion() {
        let a = Context::from("a: 1\nb: 0");
        let b = Context::from("c: 1");
        let c = a.overlay(&b);
        assert_eq!(c, Context::from("a: 1\nb: 0\nc: 1"));
    }

    #[test]
    fn subcontext() {
        let a = Context::from("a: 1\nb:\n  b1: 1\n  b2: 1");
        let b = Context::from("b1: 1\nb2: 1");
        assert_eq!(a.subcontext("b").unwrap(), b);
    }

    #[test]
    fn list_contexts() {
        let a = Context::from("a: 1\nb:\n- b1: 1\n- b2: 1");
        assert_eq!(a.list_contexts("b").unwrap(), vec![Context::from("b1: 1"), Context::from("b2: 1")]);
    }

    #[test]
    fn hide_existing() {
        let a = Context::from("a: 1\nb: 0");
        assert_eq!(a.hide("b"), Context::from("a: 1\n"));
    }

    #[test]
    fn hide_nonexisting() {
        let a = Context::from("a: 1\nb: 0");
        assert_eq!(a.hide("c"), Context::from("a: 1\nb: 0"));
    }

    #[test]
    fn unpack_string() {
        let a = Context::from("a: test");
        let out: String = a.unpack("a").unwrap();
        assert_eq!(out, String::from("test"));
    }

    #[test]
    fn unpack_i64() {
        let a = Context::from("a: 1");
        let out: i64 = a.unpack("a").unwrap();
        assert_eq!(out, 1);
    }

    #[test]
    fn unpack_i32() {
        let a = Context::from("a: 1");
        let out: i32 = a.unpack("a").unwrap();
        assert_eq!(out, 1);
    }

    #[test]
    fn unpack_usize() {
        let a = Context::from("a: 1");
        let out: usize = a.unpack("a").unwrap();
        assert_eq!(out, 1);
    }

    #[test]
    fn unpack_f64() {
        let a = Context::from("a: 1.2");
        let out: f64 = a.unpack("a").unwrap();
        assert_eq!(out, 1.2);
    }

    #[test]
    fn unpack_bool() {
        let a = Context::from("a: true");
        let out: bool = a.unpack("a").unwrap();
        assert_eq!(out, true);
    }


}

#[cfg(test)]
#[cfg(feature = "topyobject")]
mod tests_py {
    use crate::context::Context;
    use pyo3::prelude::*;
    use pyo3::Python;
    use pyo3::types::PyDict;

    fn pystr(obj: &PyObject, py: Python) -> PyResult<String> {
        let locals = PyDict::new(py);
        locals.set_item("obj", obj).unwrap();
        Ok(py.eval("str(obj)", None, Some(locals))?.extract()?)
    }

    fn pystr_attr_a(obj: &PyObject, py: Python) -> PyResult<String> {
        let locals = PyDict::new(py);
        locals.set_item("obj", obj).unwrap();
        Ok(py.eval("str(obj.a)", None, Some(locals))?.extract()?)
    }

    #[test]
    fn ctx2py() {
        let a = Context::from("a: 1");
        let gil = Python::acquire_gil();
        let py = gil.python();
        let ret: PyObject = a.to_object(py);

        if let Ok(ret_str) = pystr(&ret, py) {
            assert_eq!(ret_str, String::from("{'a': 1}"));
        }
        else {
            assert!(false);
        }
    }

    #[test]
    fn ctx2py_attr_a() {
        let a = Context::from("a: 1");
        let gil = Python::acquire_gil();
        let py = gil.python();
        let ret: PyObject = a.to_object(py);

        if let Ok(ret_str) = pystr_attr_a(&ret, py) {
            assert_eq!(ret_str, String::from("1"));
        }
        else {
            assert!(false);
        }
    }
}