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
// Copyright (c) 2019 Timo Savola.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

use super::eval::Ref;
use super::obj::{Name, Obj, Pair};

/// Stringify (), bool, i64, String, Ref or Pair.  String will be quoted.  None
/// is returned if the type is not supported.
pub fn stringify(x: &Obj) -> Option<String> {
    stringify_ex(x, |_: &Obj| None)
}

/// Stringify custom types.  All values (except pairs) are passed to the
/// supplied function first; if it returns None, the default implementation
/// (see `stringify`) is used.
pub fn stringify_ex<F: Fn(&Obj) -> Option<String> + Clone>(x: &Obj, f: F) -> Option<String> {
    if x.is::<Pair>() {
        let mut s = String::new();
        s.push('(');
        s.push_str(&stringify_inner(x, f).unwrap_or("?".to_string()));
        s.push(')');
        Some(s)
    } else {
        stringify_inner(x, f)
    }
}

fn stringify_inner<F: Fn(&Obj) -> Option<String> + Clone>(x: &Obj, f: F) -> Option<String> {
    if let Some(s) = f(x) {
        return Some(s);
    }

    if x.is::<()>() {
        return Some("()".to_string());
    }

    if let Some(b) = x.downcast_ref::<bool>() {
        return Some(b.to_string());
    }

    if let Some(n) = x.downcast_ref::<i64>() {
        return Some(n.to_string());
    }

    if let Some(s) = x.downcast_ref::<String>() {
        let escaped = s.escape_debug().to_string();
        let mut quoted = String::with_capacity(escaped.len() + 2);
        quoted.push('"');
        quoted.push_str(&escaped);
        quoted.push('"');
        return Some(quoted);
    }

    if let Some(n) = x.downcast_ref::<Name>() {
        return Some(n.0.clone());
    }

    if let Some(r) = x.downcast_ref::<Ref>() {
        return Some(r.to_string());
    }

    if let Some(p) = x.downcast_ref::<Pair>() {
        let mut s = stringify_ex(&p.0, f.clone()).unwrap_or("?".to_string());
        if !p.1.is::<()>() {
            if p.1.is::<Pair>() {
                s.push(' ');
            } else {
                s.push_str(" . ");
            }
            s.push_str(&stringify_inner(&p.1, f).unwrap_or("?".to_string()));
        }
        return Some(s);
    }

    None
}

#[cfg(test)]
mod tests {
    use super::super::obj;
    use super::*;

    #[test]
    fn test_list() {
        assert_eq!(
            stringify(&obj::pair(obj::int(1), obj::int(2))).unwrap(),
            "(1 . 2)"
        );

        assert_eq!(
            stringify(&obj::pair(
                obj::int(1),
                obj::pair(obj::int(2), obj::pair(obj::int(3), obj::nil()))
            ))
            .unwrap(),
            "(1 2 3)"
        );

        assert_eq!(
            stringify(&obj::pair(
                obj::pair(obj::int(1), obj::int(2)),
                obj::pair(obj::int(3), obj::nil())
            ))
            .unwrap(),
            "((1 . 2) 3)"
        );

        assert_eq!(
            stringify(&obj::pair(obj::pair(obj::int(1), obj::int(2)), obj::int(3))).unwrap(),
            "((1 . 2) . 3)"
        );

        assert_eq!(
            stringify(&obj::pair(
                obj::int(1),
                obj::pair(obj::pair(obj::int(2), obj::int(3)), obj::nil())
            ))
            .unwrap(),
            "(1 (2 . 3))"
        );

        assert_eq!(
            stringify(&obj::pair(obj::int(1), obj::pair(obj::nil(), obj::nil()))).unwrap(),
            "(1 ())"
        );

        assert_eq!(
            stringify(&obj::pair(obj::int(1), obj::nil())).unwrap(),
            "(1)"
        );
    }
}