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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#![allow(clippy::many_single_char_names, clippy::type_complexity)]
//! Trait and implementations of `FromPolar` for converting from
//! Polar types back to Rust types.

use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
use std::hash::Hash;

use impl_trait_for_tuples::*;

use super::class::Instance;
use super::PolarValue;
use crate::errors::TypeError;
use crate::PolarClass;

/// Convert Polar types to Rust types.
///
/// This trait is automatically implemented for any
/// type that implements the `PolarClass` trait,
/// which should be preferred.
///
/// This is also implemented automatically using the
/// `#[derive(PolarClass)]` macro.
///
/// ## Trait bounds
///
/// `FromPolar` requires `Clone` because we can only
/// get a borrowed value back from oso. In the future, this could
/// be updated to return borrowed data instead.
///
/// The default implementation for `PolarClass`
/// also requires types to be `Send + Sync`, since it
/// is possible to store a `FromPolar` value on an `Oso` instance
/// which can be shared between threads
pub trait FromPolar: Clone {
    fn from_polar(val: PolarValue) -> crate::Result<Self>;
}

impl FromPolar for PolarValue {
    fn from_polar(val: PolarValue) -> crate::Result<Self> {
        Ok(val)
    }
}

macro_rules! polar_to_int {
    ($i:ty) => {
        impl FromPolar for $i {
            fn from_polar(val: PolarValue) -> crate::Result<Self> {
                if let PolarValue::Integer(i) = val {
                    <$i>::try_from(i).map_err(|_| crate::OsoError::FromPolar)
                } else {
                    Err(TypeError::expected("Integer").user())
                }
            }
        }
    };
}

polar_to_int!(u8);
polar_to_int!(i8);
polar_to_int!(u16);
polar_to_int!(i16);
polar_to_int!(u32);
polar_to_int!(i32);
polar_to_int!(i64);

impl<T> FromPolar for T
where
    T: 'static + Clone + PolarClass,
{
    fn from_polar(val: PolarValue) -> crate::Result<Self> {
        if let PolarValue::Instance(instance) = val {
            Ok(instance.downcast::<T>(None).map_err(|e| e.user())?.clone())
        } else {
            Err(TypeError::expected("Instance").user())
        }
    }
}

impl FromPolar for f64 {
    fn from_polar(val: PolarValue) -> crate::Result<Self> {
        if let PolarValue::Float(f) = val {
            Ok(f)
        } else {
            Err(TypeError::expected("Float").user())
        }
    }
}

impl FromPolar for String {
    fn from_polar(val: PolarValue) -> crate::Result<Self> {
        if let PolarValue::String(s) = val {
            Ok(s)
        } else {
            Err(TypeError::expected("String").user())
        }
    }
}

impl FromPolar for bool {
    fn from_polar(val: PolarValue) -> crate::Result<Self> {
        if let PolarValue::Boolean(b) = val {
            Ok(b)
        } else {
            Err(TypeError::expected("Boolean").user())
        }
    }
}

impl<T: FromPolar> FromPolar for HashMap<String, T> {
    fn from_polar(val: PolarValue) -> crate::Result<Self> {
        if let PolarValue::Map(map) = val {
            let mut result = HashMap::new();
            for (k, v) in map {
                let val = T::from_polar(v)?;
                result.insert(k, val);
            }
            Ok(result)
        } else {
            Err(TypeError::expected("Map").user())
        }
    }
}

impl<T: FromPolar> FromPolar for BTreeMap<String, T> {
    fn from_polar(val: PolarValue) -> crate::Result<Self> {
        if let PolarValue::Map(map) = val {
            let mut result = BTreeMap::new();
            for (k, v) in map {
                let val = T::from_polar(v)?;
                result.insert(k, val);
            }
            Ok(result)
        } else {
            Err(TypeError::expected("Map").user())
        }
    }
}

impl<T: FromPolar> FromPolar for Vec<T> {
    fn from_polar(val: PolarValue) -> crate::Result<Self> {
        if let PolarValue::List(l) = val {
            let mut result = vec![];
            for v in l {
                result.push(T::from_polar(v)?);
            }
            Ok(result)
        } else {
            Err(TypeError::expected("List").user())
        }
    }
}

impl<T: FromPolar> FromPolar for LinkedList<T> {
    fn from_polar(val: PolarValue) -> crate::Result<Self> {
        if let PolarValue::List(l) = val {
            let mut result = LinkedList::new();
            for v in l {
                result.push_back(T::from_polar(v)?);
            }
            Ok(result)
        } else {
            Err(TypeError::expected("List").user())
        }
    }
}

impl<T: FromPolar> FromPolar for VecDeque<T> {
    fn from_polar(val: PolarValue) -> crate::Result<Self> {
        if let PolarValue::List(l) = val {
            let mut result = VecDeque::new();
            for v in l {
                result.push_back(T::from_polar(v)?);
            }
            Ok(result)
        } else {
            Err(TypeError::expected("List").user())
        }
    }
}

impl<T: Eq + Hash + FromPolar> FromPolar for HashSet<T> {
    fn from_polar(val: PolarValue) -> crate::Result<Self> {
        if let PolarValue::List(l) = val {
            let mut result = HashSet::new();
            for v in l {
                result.insert(T::from_polar(v)?);
            }
            Ok(result)
        } else {
            Err(TypeError::expected("List").user())
        }
    }
}

impl<T: Eq + Ord + FromPolar> FromPolar for BTreeSet<T> {
    fn from_polar(val: PolarValue) -> crate::Result<Self> {
        if let PolarValue::List(l) = val {
            let mut result = BTreeSet::new();
            for v in l {
                result.insert(T::from_polar(v)?);
            }
            Ok(result)
        } else {
            Err(TypeError::expected("List").user())
        }
    }
}

impl<T: Ord + FromPolar> FromPolar for BinaryHeap<T> {
    fn from_polar(val: PolarValue) -> crate::Result<Self> {
        if let PolarValue::List(l) = val {
            let mut result = BinaryHeap::new();
            for v in l {
                result.push(T::from_polar(v)?);
            }
            Ok(result)
        } else {
            Err(TypeError::expected("List").user())
        }
    }
}

impl<T: FromPolar> FromPolar for Option<T> {
    fn from_polar(val: PolarValue) -> crate::Result<Self> {
        // if the value is a Option<PolarValue>, convert from PolarValue
        if let PolarValue::Instance(ref instance) = &val {
            if let Ok(opt) = instance.downcast::<Option<PolarValue>>(None) {
                return opt.clone().map(T::from_polar).transpose();
            }
        }
        T::from_polar(val).map(Some)
    }
}

// well, you can't do this
// impl<U: FromPolar> TryFrom<U> for PolarValue {
//     type Error = crate::OsoError;

//     fn try_from(v: PolarValue) -> Result<Self, Self::Error> {
//         U::from_polar(v)
//     }
// }

// so I have to do this
macro_rules! try_from_polar {
    ($i:ty) => {
        impl TryFrom<PolarValue> for $i {
            type Error = crate::OsoError;

            fn try_from(v: PolarValue) -> Result<Self, Self::Error> {
                Self::from_polar(v)
            }
        }
    };
}

try_from_polar!(u8);
try_from_polar!(i8);
try_from_polar!(u16);
try_from_polar!(i16);
try_from_polar!(u32);
try_from_polar!(i32);
try_from_polar!(i64);
try_from_polar!(f64);
try_from_polar!(String);
try_from_polar!(bool);

impl<T: FromPolar> TryFrom<PolarValue> for HashMap<String, T> {
    type Error = crate::OsoError;

    fn try_from(v: PolarValue) -> Result<Self, Self::Error> {
        Self::from_polar(v)
    }
}

impl<T: FromPolar> TryFrom<PolarValue> for Vec<T> {
    type Error = crate::OsoError;

    fn try_from(v: PolarValue) -> Result<Self, Self::Error> {
        Self::from_polar(v)
    }
}

mod private {
    /// Prevents implementations of `FromPolarList` outside of this crate
    pub trait Sealed {}
}

pub trait FromPolarList: private::Sealed {
    fn from_polar_list(values: &[PolarValue]) -> crate::Result<Self>
    where
        Self: Sized;
}

impl FromPolar for Instance {
    fn from_polar(value: PolarValue) -> crate::Result<Self> {
        // We need to handle converting all value variants to an
        // instance so that we can use the `Class` mechanism to
        // handle methods on them
        let instance = match value {
            PolarValue::Boolean(b) => Instance::new(b),
            PolarValue::Integer(i) => Instance::new(i),
            PolarValue::Float(f) => Instance::new(f),
            PolarValue::List(v) => Instance::new(v),
            PolarValue::String(s) => Instance::new(s),
            PolarValue::Map(d) => Instance::new(d),
            PolarValue::Instance(instance) => instance,
            v => {
                tracing::warn!(value = ?v, "invalid conversion attempted");
                return Err(crate::OsoError::FromPolar);
            }
        };
        Ok(instance)
    }
}

#[impl_for_tuples(16)]
#[tuple_types_custom_trait_bound(FromPolar)]
impl FromPolarList for Tuple {
    fn from_polar_list(values: &[PolarValue]) -> crate::Result<Self> {
        let mut iter = values.iter();
        let result = Ok((for_tuples!(
            #( Tuple::from_polar(iter.next().ok_or(
                // TODO better error type
                crate::OsoError::FromPolar
            )?.clone())? ),*
        )));

        if iter.len() > 0 {
            // TODO (dhatch): Debug this!!!
            tracing::warn!("Remaining items in iterator after conversion.");
            for item in iter {
                tracing::trace!("Remaining item {:?}", item);
            }

            return Err(crate::OsoError::FromPolar);
        }

        result
    }
}

#[impl_for_tuples(16)]
#[tuple_types_custom_trait_bound(FromPolar)]
impl private::Sealed for Tuple {}