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
use super::{valuetype::ValueType, Value};
use std::fmt;

impl fmt::Display for ValueType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::IntType => write!(f, "Integer"),
            Self::FloatType => write!(f, "Float"),
            Self::ComplexType => write!(f, "Complex"),
            Self::VectorType => write!(f, "Vector"),
            Self::BoolType => write!(f, "Bool"),
        }
    }
}

impl fmt::Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Bool(v) => write!(f, "{}", v),
            Self::Int(v) => write!(f, "{}", v),
            Self::Float(v) => write!(f, "{}", v),
            Self::Complex(v) => write!(
                f,
                "{}",
                if v.re == 0.0 {
                    if v.im == 1.0 {
                        String::from("i")
                    } else {
                        format!("{}i", v.im)
                    }
                } else if v.im == 0.0 {
                    format!("{}", v.re)
                } else {
                    format!(
                        "{}{}{}i",
                        v.re,
                        if v.im > 0.0 { "+" } else { "-" },
                        if v.im.abs() == 1.0 {
                            String::from("")
                        } else {
                            format!("{}", v.im.abs())
                        }
                    )
                }
            ),
            Self::Vector(v) => write!(f, "[{}]", {
                let mut as_string = vec![];

                for elem in v {
                    as_string.push(elem.to_string());
                }

                as_string.join(", ")
            }),
        }
    }
}