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
//! Library for encoding/decoding Erlang External Term Format.
//!
//! # Examples
//!
//! Decodes an atom:
//!
//! ```
//! use std::io::Cursor;
//! use eetf::{Term, Atom};
//!
//! let bytes = vec![131, 100, 0, 3, 102, 111, 111];
//! let term = Term::decode(Cursor::new(&bytes)).unwrap();
//! assert_eq!(term, Term::from(Atom::from("foo")));
//! ```
//!
//! Encodes an atom:
//!
//! ```
//! use eetf::{Term, Atom};
//!
//! let mut buf = Vec::new();
//! let term = Term::from(Atom::from("foo"));
//! term.encode(&mut buf).unwrap();
//! assert_eq!(vec![131, 100, 0, 3, 102, 111, 111], buf);
//! ```
//!
//! # Reference
//!
//! - [Erlang External Term Format](http://erlang.org/doc/apps/erts/erl_ext_dist.html)
//!
extern crate num;
extern crate byteorder;
extern crate flate2;

use std::fmt;
use std::io;
use std::convert;
use num::bigint::BigInt;

mod codec;

pub use codec::EncodeResult;
pub use codec::DecodeResult;
pub use codec::EncodeError;
pub use codec::DecodeError;

/// Term.
#[derive(Debug, PartialEq, Clone)]
pub enum Term {
    Atom(Atom),
    FixInteger(FixInteger),
    BigInteger(BigInteger),
    Float(Float),
    Pid(Pid),
    Port(Port),
    Reference(Reference),
    ExternalFun(ExternalFun),
    InternalFun(InternalFun),
    Binary(Binary),
    BitBinary(BitBinary),
    List(List),
    ImproperList(ImproperList),
    Tuple(Tuple),
    Map(Map),
}
impl Term {
    /// Decodes a term.
    pub fn decode<R: io::Read>(reader: R) -> DecodeResult {
        codec::Decoder::new(reader).decode()
    }

    /// Encodes the term.
    pub fn encode<W: io::Write>(&self, writer: W) -> EncodeResult {
        codec::Encoder::new(writer).encode(self)
    }

    /// Extracts the atom value if it is an atom term.
    pub fn as_atom(&self) -> Option<&Atom> {
        if let Term::Atom(ref x) = *self {
            Some(x)
        } else {
            None
        }
    }

    /// Converts the term to the atom value if it is an atom term.
    pub fn into_atom(self) -> Result<Atom, Term> {
        if let Term::Atom(x) = self {
            Ok(x)
        } else {
            Err(self)
        }
    }

    /// Extracts the fix integer value if it is a fix integer term.
    pub fn as_fix_integer(&self) -> Option<&FixInteger> {
        if let Term::FixInteger(ref x) = *self {
            Some(x)
        } else {
            None
        }
    }

    /// Converts the term to the fix integer value if it is a fix integer term.
    pub fn into_fix_integer(self) -> Result<FixInteger, Term> {
        if let Term::FixInteger(x) = self {
            Ok(x)
        } else {
            Err(self)
        }
    }

    /// Extracts the big integer value if it is a big integer term.
    pub fn as_big_integer(&self) -> Option<&BigInteger> {
        if let Term::BigInteger(ref x) = *self {
            Some(x)
        } else {
            None
        }
    }

    /// Converts the term to the big integer value if it is a big integer term.
    pub fn into_big_integer(self) -> Result<BigInteger, Term> {
        if let Term::BigInteger(x) = self {
            Ok(x)
        } else {
            Err(self)
        }
    }

    /// Extracts the float value if it is a float term.
    pub fn as_float(&self) -> Option<&Float> {
        if let Term::Float(ref x) = *self {
            Some(x)
        } else {
            None
        }
    }

    /// Converts the term to the float value if it is a float term.
    pub fn into_float(self) -> Result<Float, Term> {
        if let Term::Float(x) = self {
            Ok(x)
        } else {
            Err(self)
        }
    }

    /// Extracts the pid value if it is a pid term.
    pub fn as_pid(&self) -> Option<&Pid> {
        if let Term::Pid(ref x) = *self {
            Some(x)
        } else {
            None
        }
    }

    /// Converts the term to the pid value if it is a pid term.
    pub fn into_pid(self) -> Result<Pid, Term> {
        if let Term::Pid(x) = self {
            Ok(x)
        } else {
            Err(self)
        }
    }

    /// Extracts the port value if it is a port term.
    pub fn as_port(&self) -> Option<&Port> {
        if let Term::Port(ref x) = *self {
            Some(x)
        } else {
            None
        }
    }

    /// Converts the term to the port value if it is a port term.
    pub fn into_port(self) -> Result<Port, Term> {
        if let Term::Port(x) = self {
            Ok(x)
        } else {
            Err(self)
        }
    }

    /// Extracts the reference value if it is a reference term.
    pub fn as_reference(&self) -> Option<&Reference> {
        if let Term::Reference(ref x) = *self {
            Some(x)
        } else {
            None
        }
    }

    /// Converts the term to the reference value if it is a reference term.
    pub fn into_reference(self) -> Result<Reference, Term> {
        if let Term::Reference(x) = self {
            Ok(x)
        } else {
            Err(self)
        }
    }

    /// Extracts the external functionif it is an external function term.
    pub fn as_external_fun(&self) -> Option<&ExternalFun> {
        if let Term::ExternalFun(ref x) = *self {
            Some(x)
        } else {
            None
        }
    }

    /// Converts the term to the external function if it is an external function term.
    pub fn into_external_fun(self) -> Result<ExternalFun, Term> {
        if let Term::ExternalFun(x) = self {
            Ok(x)
        } else {
            Err(self)
        }
    }

    /// Extracts the internal function if it is an internal function term.
    pub fn as_internal_fun(&self) -> Option<&InternalFun> {
        if let Term::InternalFun(ref x) = *self {
            Some(x)
        } else {
            None
        }
    }

    /// Converts the term to the internal function if it is an internal function term.
    pub fn into_internal_fun(self) -> Result<InternalFun, Term> {
        if let Term::InternalFun(x) = self {
            Ok(x)
        } else {
            Err(self)
        }
    }

    /// Extracts the binary value if it is a binary term.
    pub fn as_binary(&self) -> Option<&Binary> {
        if let Term::Binary(ref x) = *self {
            Some(x)
        } else {
            None
        }
    }

    /// Converts the term to the binary value if it is a binary term.
    pub fn into_binary(self) -> Result<Binary, Term> {
        if let Term::Binary(x) = self {
            Ok(x)
        } else {
            Err(self)
        }
    }

    /// Extracts the bitstring value if it is a bitstring term.
    pub fn as_bit_binary(&self) -> Option<&BitBinary> {
        if let Term::BitBinary(ref x) = *self {
            Some(x)
        } else {
            None
        }
    }

    /// Converts the term to the bitstring value if it is a bitstring term.
    pub fn into_bit_binary(self) -> Result<BitBinary, Term> {
        if let Term::BitBinary(x) = self {
            Ok(x)
        } else {
            Err(self)
        }
    }

    /// Extracts the list value if it is a list term.
    pub fn as_list(&self) -> Option<&List> {
        if let Term::List(ref x) = *self {
            Some(x)
        } else {
            None
        }
    }

    /// Converts the term to the list value if it is a list term.
    pub fn into_list(self) -> Result<List, Term> {
        if let Term::List(x) = self {
            Ok(x)
        } else {
            Err(self)
        }
    }

    /// Extracts the improper list value if it is an improper list term.
    pub fn as_improper_list(&self) -> Option<&ImproperList> {
        if let Term::ImproperList(ref x) = *self {
            Some(x)
        } else {
            None
        }
    }

    /// Converts the term to the improper list value if it is an improper list term.
    pub fn into_improper_list(self) -> Result<ImproperList, Term> {
        if let Term::ImproperList(x) = self {
            Ok(x)
        } else {
            Err(self)
        }
    }

    /// Extracts the tuple value if it is a tuple term.
    pub fn as_tuple(&self) -> Option<&Tuple> {
        if let Term::Tuple(ref x) = *self {
            Some(x)
        } else {
            None
        }
    }

    /// Converts the term to the tuple value if it is a tuple term.
    pub fn into_tuple(self) -> Result<Tuple, Term> {
        if let Term::Tuple(x) = self {
            Ok(x)
        } else {
            Err(self)
        }
    }

    /// Extracts the map value if it is a map term.
    pub fn as_map(&self) -> Option<&Map> {
        if let Term::Map(ref x) = *self {
            Some(x)
        } else {
            None
        }
    }

    /// Converts the term to the map value if it is a map term.
    pub fn into_map(self) -> Result<Map, Term> {
        if let Term::Map(x) = self {
            Ok(x)
        } else {
            Err(self)
        }
    }
}
impl fmt::Display for Term {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Term::Atom(ref x) => x.fmt(f),
            Term::FixInteger(ref x) => x.fmt(f),
            Term::BigInteger(ref x) => x.fmt(f),
            Term::Float(ref x) => x.fmt(f),
            Term::Pid(ref x) => x.fmt(f),
            Term::Port(ref x) => x.fmt(f),
            Term::Reference(ref x) => x.fmt(f),
            Term::ExternalFun(ref x) => x.fmt(f),
            Term::InternalFun(ref x) => x.fmt(f),
            Term::Binary(ref x) => x.fmt(f),
            Term::BitBinary(ref x) => x.fmt(f),
            Term::List(ref x) => x.fmt(f),
            Term::ImproperList(ref x) => x.fmt(f),
            Term::Tuple(ref x) => x.fmt(f),
            Term::Map(ref x) => x.fmt(f),
        }
    }
}
impl convert::From<Atom> for Term {
    fn from(x: Atom) -> Self {
        Term::Atom(x)
    }
}
impl convert::From<FixInteger> for Term {
    fn from(x: FixInteger) -> Self {
        Term::FixInteger(x)
    }
}
impl convert::From<BigInteger> for Term {
    fn from(x: BigInteger) -> Self {
        Term::BigInteger(x)
    }
}
impl convert::From<Float> for Term {
    fn from(x: Float) -> Self {
        Term::Float(x)
    }
}
impl convert::From<Pid> for Term {
    fn from(x: Pid) -> Self {
        Term::Pid(x)
    }
}
impl convert::From<Port> for Term {
    fn from(x: Port) -> Self {
        Term::Port(x)
    }
}
impl convert::From<Reference> for Term {
    fn from(x: Reference) -> Self {
        Term::Reference(x)
    }
}
impl convert::From<ExternalFun> for Term {
    fn from(x: ExternalFun) -> Self {
        Term::ExternalFun(x)
    }
}
impl convert::From<InternalFun> for Term {
    fn from(x: InternalFun) -> Self {
        Term::InternalFun(x)
    }
}
impl convert::From<Binary> for Term {
    fn from(x: Binary) -> Self {
        Term::Binary(x)
    }
}
impl convert::From<BitBinary> for Term {
    fn from(x: BitBinary) -> Self {
        Term::BitBinary(x)
    }
}
impl convert::From<List> for Term {
    fn from(x: List) -> Self {
        Term::List(x)
    }
}
impl convert::From<ImproperList> for Term {
    fn from(x: ImproperList) -> Self {
        Term::ImproperList(x)
    }
}
impl convert::From<Tuple> for Term {
    fn from(x: Tuple) -> Self {
        Term::Tuple(x)
    }
}
impl convert::From<Map> for Term {
    fn from(x: Map) -> Self {
        Term::Map(x)
    }
}

/// Atom.
#[derive(Debug, PartialEq, Clone)]
pub struct Atom {
    /// The name of the atom.
    pub name: String,
}
impl fmt::Display for Atom {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f,
               "'{}'",
               self.name.replace("\\", "\\\\").replace("'", "\\'"))
    }
}
impl<'a> convert::From<&'a str> for Atom {
    fn from(name: &'a str) -> Self {
        Atom { name: name.to_string() }
    }
}

/// Fixed width integer.
#[derive(Debug, PartialEq, Clone)]
pub struct FixInteger {
    /// The value of the integer
    pub value: i32,
}
impl fmt::Display for FixInteger {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}
impl convert::From<i32> for FixInteger {
    fn from(value: i32) -> Self {
        FixInteger { value: value }
    }
}

/// Multiple precision integer.
#[derive(Debug, PartialEq, Clone)]
pub struct BigInteger {
    /// The value of the integer
    pub value: BigInt,
}
impl fmt::Display for BigInteger {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}
impl convert::From<i64> for BigInteger {
    fn from(value: i64) -> Self {
        BigInteger { value: BigInt::from(value) }
    }
}
impl<'a> convert::From<&'a FixInteger> for BigInteger {
    fn from(i: &FixInteger) -> Self {
        BigInteger { value: BigInt::from(i.value) }
    }
}

/// Floating point number
#[derive(Debug, PartialEq, Clone)]
pub struct Float {
    /// The value of the number
    pub value: f64,
}
impl fmt::Display for Float {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}
impl convert::From<f64> for Float {
    fn from(value: f64) -> Self {
        Float { value: value }
    }
}

/// Process Identifier.
#[derive(Debug, PartialEq, Clone)]
pub struct Pid {
    pub node: Atom,
    pub id: u32,
    pub serial: u32,
    pub creation: u8,
}
impl fmt::Display for Pid {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "<{}.{}.{}>", self.node, self.id, self.serial)
    }
}
impl<'a> convert::From<(&'a str, u32, u32)> for Pid {
    fn from((node, id, serial): (&'a str, u32, u32)) -> Self {
        Pid {
            node: Atom::from(node),
            id: id,
            serial: serial,
            creation: 0,
        }
    }
}

/// Port.
#[derive(Debug, PartialEq, Clone)]
pub struct Port {
    pub node: Atom,
    pub id: u32,
    pub creation: u8,
}
impl fmt::Display for Port {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "#Port<{}.{}>", self.node, self.id)
    }
}
impl<'a> convert::From<(&'a str, u32)> for Port {
    fn from((node, id): (&'a str, u32)) -> Self {
        Port {
            node: Atom::from(node),
            id: id,
            creation: 0,
        }
    }
}

/// Reference.
#[derive(Debug, PartialEq, Clone)]
pub struct Reference {
    pub node: Atom,
    pub id: Vec<u32>,
    pub creation: u8,
}
impl fmt::Display for Reference {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        try!(write!(f, "#Ref<{}", self.node));
        for n in &self.id {
            try!(write!(f, ".{}", n));
        }
        write!(f, ">")
    }
}
impl<'a> convert::From<(&'a str, u32)> for Reference {
    fn from((node, id): (&'a str, u32)) -> Self {
        Reference {
            node: Atom::from(node),
            id: vec![id],
            creation: 0,
        }
    }
}
impl<'a> convert::From<(&'a str, Vec<u32>)> for Reference {
    fn from((node, id): (&'a str, Vec<u32>)) -> Self {
        Reference {
            node: Atom::from(node),
            id: id,
            creation: 0,
        }
    }
}

/// External Function.
#[derive(Debug, PartialEq, Clone)]
pub struct ExternalFun {
    pub module: Atom,
    pub function: Atom,
    pub arity: u8,
}
impl fmt::Display for ExternalFun {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "fun {}:{}/{}", self.module, self.function, self.arity)
    }
}
impl<'a, 'b> convert::From<(&'a str, &'b str, u8)> for ExternalFun {
    fn from((module, function, arity): (&'a str, &'b str, u8)) -> Self {
        ExternalFun {
            module: Atom::from(module),
            function: Atom::from(function),
            arity: arity,
        }
    }
}

/// Internal Function.
#[derive(Debug, PartialEq, Clone)]
pub enum InternalFun {
    /// Old representation.
    Old {
        module: Atom,
        pid: Pid,
        free_vars: Vec<Term>,
        index: i32,
        uniq: i32,
    },
    /// New representation.
    New {
        module: Atom,
        arity: u8,
        pid: Pid,
        free_vars: Vec<Term>,
        index: u32,
        uniq: [u8; 16],
        old_index: i32,
        old_uniq: i32,
    },
}
impl fmt::Display for InternalFun {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            InternalFun::Old { ref module, index, uniq, .. } => {
                write!(f, "#Fun<{}.{}.{}>", module, index, uniq)
            }
            InternalFun::New { ref module, index, uniq, .. } => {
                use num::bigint::Sign;
                let uniq = BigInt::from_bytes_be(Sign::Plus, &uniq);
                write!(f, "#Fun<{}.{}.{}>", module, index, uniq)
            }
        }
    }
}

/// Binary.
#[derive(Debug, PartialEq, Clone)]
pub struct Binary {
    pub bytes: Vec<u8>,
}
impl fmt::Display for Binary {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        try!(write!(f, "<<"));
        for (i, b) in self.bytes.iter().enumerate() {
            if i != 0 {
                try!(write!(f, ","));
            }
            try!(write!(f, "{}", b));
        }
        try!(write!(f, ">>"));
        Ok(())
    }
}
impl<'a> convert::From<(&'a [u8])> for Binary {
    fn from(bytes: &'a [u8]) -> Self {
        Binary { bytes: Vec::from(bytes) }
    }
}
impl convert::From<Vec<u8>> for Binary {
    fn from(bytes: Vec<u8>) -> Self {
        Binary { bytes: bytes }
    }
}

/// Bit string.
#[derive(Debug, PartialEq, Clone)]
pub struct BitBinary {
    pub bytes: Vec<u8>,
    pub tail_bits_size: u8,
}
impl fmt::Display for BitBinary {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        try!(write!(f, "<<"));
        for (i, b) in self.bytes.iter().enumerate() {
            if i == self.bytes.len() - 1 && self.tail_bits_size == 0 {
                break;
            }
            if i != 0 {
                try!(write!(f, ","));
            }
            if i == self.bytes.len() - 1 && self.tail_bits_size < 8 {
                try!(write!(f, "{}:{}", b, self.tail_bits_size));
            } else {
                try!(write!(f, "{}", b));
            }
        }
        try!(write!(f, ">>"));
        Ok(())
    }
}
impl convert::From<Binary> for BitBinary {
    fn from(binary: Binary) -> Self {
        BitBinary {
            bytes: binary.bytes,
            tail_bits_size: 8,
        }
    }
}
impl convert::From<(Vec<u8>, u8)> for BitBinary {
    fn from((bytes, tail_bits_size): (Vec<u8>, u8)) -> Self {
        BitBinary {
            bytes: bytes,
            tail_bits_size: tail_bits_size,
        }
    }
}

/// List.
#[derive(Debug, PartialEq, Clone)]
pub struct List {
    pub elements: Vec<Term>,
}
impl List {
    /// Returns a nil value (i.e., an empty list).
    pub fn nil() -> Self {
        List { elements: Vec::new() }
    }

    /// Returns `true` if it is nil value, otherwise `false`.
    pub fn is_nil(&self) -> bool {
        self.elements.is_empty()
    }
}
impl fmt::Display for List {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        try!(write!(f, "["));
        for (i, x) in self.elements.iter().enumerate() {
            if i != 0 {
                try!(write!(f, ","));
            }
            try!(write!(f, "{}", x));
        }
        try!(write!(f, "]"));
        Ok(())
    }
}
impl convert::From<Vec<Term>> for List {
    fn from(elements: Vec<Term>) -> Self {
        List { elements: elements }
    }
}

/// Improper list.
#[derive(Debug, PartialEq, Clone)]
pub struct ImproperList {
    pub elements: Vec<Term>,
    pub last: Box<Term>,
}
impl fmt::Display for ImproperList {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        try!(write!(f, "["));
        for (i, x) in self.elements.iter().enumerate() {
            if i != 0 {
                try!(write!(f, ","));
            }
            try!(write!(f, "{}", x));
        }
        try!(write!(f, "|{}", self.last));
        try!(write!(f, "]"));
        Ok(())
    }
}
impl convert::From<(Vec<Term>, Term)> for ImproperList {
    fn from((elements, last): (Vec<Term>, Term)) -> Self {
        ImproperList {
            elements: elements,
            last: Box::new(last),
        }
    }
}

/// Tuple.
#[derive(Debug, PartialEq, Clone)]
pub struct Tuple {
    pub elements: Vec<Term>,
}
impl Tuple {
    pub fn nil() -> Self {
        Tuple { elements: Vec::new() }
    }
}
impl fmt::Display for Tuple {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        try!(write!(f, "{{"));
        for (i, x) in self.elements.iter().enumerate() {
            if i != 0 {
                try!(write!(f, ","));
            }
            try!(write!(f, "{}", x));
        }
        try!(write!(f, "}}"));
        Ok(())
    }
}
impl convert::From<Vec<Term>> for Tuple {
    fn from(elements: Vec<Term>) -> Self {
        Tuple { elements: elements }
    }
}

/// Map.
#[derive(Debug, PartialEq, Clone)]
pub struct Map {
    pub entries: Vec<(Term, Term)>,
}
impl fmt::Display for Map {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        try!(write!(f, "#{{"));
        for (i, &(ref k, ref v)) in self.entries.iter().enumerate() {
            if i != 0 {
                try!(write!(f, ","));
            }
            try!(write!(f, "{}=>{}", k, v));
        }
        try!(write!(f, "}}"));
        Ok(())
    }
}
impl convert::From<Vec<(Term, Term)>> for Map {
    fn from(entries: Vec<(Term, Term)>) -> Self {
        Map { entries: entries }
    }
}