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
use nu_protocol::{Record, Span, Value};
use rusty_value::{Fields, HashableValue, RustyValue};

use crate::utils::NewEmpty;

/// A helper struct to allow IntoValue operations for nu values
pub struct RawValue(pub Value);

/// Converts the given type into a value
/// This trait is implemented for all types that
/// Implement the RustyValue trait
pub trait IntoValue {
    fn into_value(self) -> Value;
}

impl IntoValue for RawValue {
    #[inline]
    fn into_value(self) -> Value {
        self.0
    }
}

/// Helper trait to avoid conflicts
pub trait RustyIntoValue {
    fn into_value(self) -> Value;
}

pub(crate) trait HashableIntoString {
    fn into_string(self) -> String;
}

impl HashableIntoString for HashableValue {
    fn into_string(self) -> String {
        match self {
            HashableValue::Primitive(p) => p.to_string(),
            HashableValue::List(l) => l
                .into_iter()
                .map(|v| v.into_string())
                .collect::<Vec<_>>()
                .join(","),
            HashableValue::None => String::new(),
        }
    }
}

impl RustyIntoValue for Vec<Value> {
    #[inline]
    fn into_value(self) -> Value {
        Value::List {
            vals: self,
            internal_span: Span::empty(),
        }
    }
}

impl RustyIntoValue for rusty_value::Value {
    fn into_value(self) -> Value {
        match self {
            rusty_value::Value::Primitive(p) => p.into_value(),
            rusty_value::Value::Struct(s) => {
                if let Fields::Unit = &s.fields {
                    Value::String {
                        val: s.name,
                        internal_span: Span::empty(),
                    }
                } else {
                    s.fields.into_value()
                }
            }
            rusty_value::Value::Enum(e) => {
                if let Fields::Unit = &e.fields {
                    Value::String {
                        val: e.variant,
                        internal_span: Span::empty(),
                    }
                } else {
                    e.fields.into_value()
                }
            }
            rusty_value::Value::Map(map) => {
                let mut cols = Vec::new();
                let mut vals = Vec::new();

                for (key, val) in map {
                    cols.push(key.into_string());
                    vals.push(val.into_value());
                }
                Value::Record {
                    val: Record::from_raw_cols_vals_unchecked(cols, vals),
                    internal_span: Span::empty(),
                }
            }
            rusty_value::Value::List(l) => {
                let vals = l.into_iter().map(|e| e.into_value()).collect();

                Value::List {
                    vals,
                    internal_span: Span::empty(),
                }
            }
            rusty_value::Value::None => Value::Nothing {
                internal_span: Span::empty(),
            },
        }
    }
}

impl RustyIntoValue for rusty_value::Primitive {
    fn into_value(self) -> Value {
        match self {
            rusty_value::Primitive::Integer(i) => i.into_value(),
            rusty_value::Primitive::Float(f) => f.into_value(),
            rusty_value::Primitive::String(val) => Value::String {
                val,
                internal_span: Span::empty(),
            },
            rusty_value::Primitive::Char(val) => Value::String {
                val: val.to_string(),
                internal_span: Span::empty(),
            },
            rusty_value::Primitive::Bool(val) => Value::Bool {
                val,
                internal_span: Span::empty(),
            },
            rusty_value::Primitive::OsString(osstr) => osstr.to_string_lossy().into_value(),
        }
    }
}

impl RustyIntoValue for rusty_value::Fields {
    fn into_value(self) -> Value {
        match self {
            rusty_value::Fields::Named(named) => {
                let mut cols = Vec::with_capacity(named.len());
                let mut vals = Vec::with_capacity(named.len());

                for (k, v) in named {
                    cols.push(k);
                    vals.push(v.into_value());
                }
                Value::Record {
                    val: Record::from_raw_cols_vals_unchecked(cols, vals),
                    internal_span: Span::empty(),
                }
            }
            rusty_value::Fields::Unnamed(unnamed) => {
                let mut vals = unnamed
                    .into_iter()
                    .map(|v| v.into_value())
                    .collect::<Vec<_>>();

                // newtypes should be handled differently
                // and only return the inner value instead of a range of values
                if vals.len() == 1 {
                    vals.pop().unwrap()
                } else {
                    Value::List {
                        vals,
                        internal_span: Span::empty(),
                    }
                }
            }
            rusty_value::Fields::Unit => Value::Nothing {
                internal_span: Span::empty(),
            },
        }
    }
}

impl RustyIntoValue for rusty_value::Integer {
    fn into_value(self) -> Value {
        let val = match self {
            rusty_value::Integer::USize(i) => i as i64,
            rusty_value::Integer::ISize(i) => i as i64,
            rusty_value::Integer::U8(i) => i as i64,
            rusty_value::Integer::I8(i) => i as i64,
            rusty_value::Integer::U16(i) => i as i64,
            rusty_value::Integer::I16(i) => i as i64,
            rusty_value::Integer::U32(i) => i as i64,
            rusty_value::Integer::I32(i) => i as i64,
            rusty_value::Integer::U64(i) => i as i64,
            rusty_value::Integer::I64(i) => i,
            rusty_value::Integer::U128(i) => i as i64,
            rusty_value::Integer::I128(i) => i as i64,
        };
        Value::Int {
            val,
            internal_span: Span::empty(),
        }
    }
}

impl RustyIntoValue for rusty_value::Float {
    #[inline]
    fn into_value(self) -> Value {
        let val = match self {
            rusty_value::Float::F32(f) => f as f64,
            rusty_value::Float::F64(f) => f,
        };
        Value::Float {
            val,
            internal_span: Span::empty(),
        }
    }
}

impl<R: RustyValue> IntoValue for R {
    #[inline]
    fn into_value(self) -> Value {
        self.into_rusty_value().into_value()
    }
}