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
use std::{
    collections::BTreeMap,
    fmt::{self, Debug, Display, Formatter},
};

use serde::{Deserialize, Serialize};

use crate::{value_node::ValueNode, Expression, Function, Identifier, Value};

/// The type of a `Value`.
#[derive(Clone, Serialize, Deserialize, PartialOrd, Ord)]
pub enum ValueType {
    Any,
    String,
    Float,
    Integer,
    Boolean,
    ListExact(Vec<Expression>),
    Empty,
    Map(BTreeMap<String, Expression>),
    Table {
        column_names: Vec<Identifier>,
        rows: Box<Expression>,
    },
    Function(Function),
}

impl Eq for ValueType {}

impl PartialEq for ValueType {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (ValueType::Any, _) => true,
            (_, ValueType::Any) => true,
            (ValueType::String, ValueType::String) => true,
            (ValueType::Float, ValueType::Float) => true,
            (ValueType::Integer, ValueType::Integer) => true,
            (ValueType::Boolean, ValueType::Boolean) => true,
            (ValueType::ListExact(left), ValueType::ListExact(right)) => left == right,
            (ValueType::Empty, ValueType::Empty) => true,
            (ValueType::Map(left), ValueType::Map(right)) => left == right,
            (
                ValueType::Table {
                    column_names: left_columns,
                    rows: left_rows,
                },
                ValueType::Table {
                    column_names: right_columns,
                    rows: right_rows,
                },
            ) => left_columns == right_columns && left_rows == right_rows,
            (ValueType::Function(left), ValueType::Function(right)) => left == right,
            _ => false,
        }
    }
}

impl Display for ValueType {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match &self {
            ValueType::Any => write!(f, "any"),
            ValueType::String => write!(f, "string"),
            ValueType::Float => write!(f, "float"),
            ValueType::Integer => write!(f, "integer"),
            ValueType::Boolean => write!(f, "boolean"),
            ValueType::ListExact(list) => {
                write!(f, "(")?;
                for (index, item) in list.into_iter().enumerate() {
                    if index > 0 {
                        write!(f, ", ")?;
                    }

                    write!(f, "{item:?}")?;
                }

                write!(f, ")")
            }
            ValueType::Empty => write!(f, "empty"),
            ValueType::Map(_map) => write!(f, "map"),
            ValueType::Table {
                column_names: _,
                rows: _,
            } => {
                write!(f, "table")
            }
            ValueType::Function(function) => write!(f, "{function}"),
        }
    }
}

impl Debug for ValueType {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{self}")
    }
}

impl From<&Value> for ValueType {
    fn from(value: &Value) -> Self {
        match value {
            Value::String(_) => ValueType::String,
            Value::Float(_) => ValueType::Float,
            Value::Integer(_) => ValueType::Integer,
            Value::Boolean(_) => ValueType::Boolean,
            Value::Empty => ValueType::Empty,
            Value::List(list) => {
                let value_nodes = list
                    .iter()
                    .map(|value| Expression::Value(ValueNode::new(value.value_type(), 0, 0)))
                    .collect();

                ValueType::ListExact(value_nodes)
            }
            Value::Map(map) => {
                let mut value_nodes = BTreeMap::new();

                for (key, value) in map.inner() {
                    let value_type = ValueType::from(value);
                    let value_node = ValueNode::new(value_type, 0, 0);
                    let expression = Expression::Value(value_node);

                    value_nodes.insert(key.to_string(), expression);
                }

                ValueType::Map(value_nodes)
            }
            Value::Table(table) => ValueType::Table {
                column_names: table
                    .headers()
                    .iter()
                    .map(|column_name| Identifier::new(column_name.clone()))
                    .collect(),
                rows: Box::new(Expression::Value(ValueNode::new(
                    ValueType::ListExact(Vec::with_capacity(0)),
                    0,
                    0,
                ))),
            },
            Value::Function(function) => ValueType::Function(function.clone()),
        }
    }
}

impl From<&mut Value> for ValueType {
    fn from(value: &mut Value) -> Self {
        From::<&Value>::from(value)
    }
}