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

//! Value representation.

use lep::Obj;

use crate::future::{Fut, FutState};
use crate::stream::{ReadStreamRef, ReadWriteStreamRef, WriteStreamRef};

/// Stringify Lep and Gain object types.
///
/// 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> {
    lep::display::stringify_ex(x, |x: &Obj| -> Option<String> {
        if let Some(s) = f(x) {
            return Some(s);
        }

        if let Some(cell) = x.downcast_ref::<Fut>() {
            return Some(match cell.replace(FutState::Unknown) {
                FutState::Pending(b) => {
                    cell.set(FutState::Pending(b));
                    "FuturePending".to_string()
                }
                FutState::Done(res) => {
                    let s = match res {
                        Ok(ref value) => {
                            let mut s = "Future<".to_string();
                            s.push_str(&stringify_ex(&value, f.clone()).unwrap_or("?".to_string()));
                            s.push('>');
                            s
                        }
                        Err(ref x) => format!("FutureError<{}>", x),
                    };
                    cell.set(FutState::Done(res));
                    s
                }
                FutState::Unknown => "FutureUnknown".to_string(),
            });
        }

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

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

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

        if let Some(data) = x.downcast_ref::<Vec<u8>>() {
            return Some(format!("Vec{:x?}", data.as_slice()));
        }

        None
    })
}