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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
use std::{cmp::Ordering, sync::Arc};

/// IR of the values of Trustfall fields.
use async_graphql_value::{ConstValue, Number, Value};
use serde::{Deserialize, Serialize};

/// Values of fields in Trustfall.
///
/// For version that is serialized as an untagged enum, see [TransparentValue].
#[non_exhaustive]
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub enum FieldValue {
    // Order may matter here! Deserialization, if ever configured for untagged serialization,
    // will attempt each variant in order until the first one that matches. Int64 must be
    // above Uint64, which must be above Float64.
    // This is because we want to prioritize the standard Integer GraphQL type over our custom u64,
    // and prioritize exact integers over lossy floats.
    #[default]
    Null,

    /// Together with `Uint64`, corresponds to schemas' `Int` type.
    Int64(i64),

    /// Together with `Int64`, corresponds to schemas' `Int` type.
    Uint64(u64),

    /// Corresponds to schemas' `Float` type. Not allowed to be NaN or infinite.
    Float64(f64),

    String(Arc<str>),
    Boolean(bool),
    Enum(Arc<str>),
    List(Arc<[FieldValue]>),
}

impl FieldValue {
    pub const NULL: Self = FieldValue::Null;
}

impl Default for &FieldValue {
    fn default() -> Self {
        &FieldValue::NULL
    }
}

/// Values of fields in GraphQL types.
///
/// Same as [FieldValue], but serialized as an untagged enum,
/// which may be more suitable e.g. when serializing to JSON.
#[non_exhaustive]
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(untagged)]
pub enum TransparentValue {
    // Order may matter here! Deserialization, if ever configured for untagged serialization,
    // will attempt each variant in order until the first one that matches. Int64 must be
    // above Uint64, which must be above Float64.
    // This is because we want to prioritize the standard Integer GraphQL type over our custom u64,
    // and prioritize exact integers over lossy floats.
    #[default]
    Null,

    /// Together with `Uint64`, corresponds to schemas' `Int` type.
    Int64(i64),

    /// Together with `Int64`, corresponds to schemas' `Int` type.
    Uint64(u64),

    /// Corresponds to schemas' `Float` type. Not allowed to be NaN or infinite.
    Float64(f64),

    String(Arc<str>),
    Boolean(bool),
    Enum(Arc<str>),
    List(Arc<[TransparentValue]>),
}

impl From<FieldValue> for TransparentValue {
    fn from(value: FieldValue) -> Self {
        match value {
            FieldValue::Null => TransparentValue::Null,
            FieldValue::Int64(x) => TransparentValue::Int64(x),
            FieldValue::Uint64(x) => TransparentValue::Uint64(x),
            FieldValue::Float64(x) => TransparentValue::Float64(x),
            FieldValue::String(x) => TransparentValue::String(x),
            FieldValue::Boolean(x) => TransparentValue::Boolean(x),
            FieldValue::Enum(x) => TransparentValue::Enum(x),
            FieldValue::List(x) => TransparentValue::List(
                x.iter().map(|v| v.clone().into()).collect::<Vec<_>>().into(),
            ),
        }
    }
}

impl From<TransparentValue> for FieldValue {
    fn from(value: TransparentValue) -> Self {
        match value {
            TransparentValue::Null => FieldValue::Null,
            TransparentValue::Int64(x) => FieldValue::Int64(x),
            TransparentValue::Uint64(x) => FieldValue::Uint64(x),
            TransparentValue::Float64(x) => FieldValue::Float64(x),
            TransparentValue::String(x) => FieldValue::String(x),
            TransparentValue::Boolean(x) => FieldValue::Boolean(x),
            TransparentValue::Enum(x) => FieldValue::Enum(x),
            TransparentValue::List(x) => {
                FieldValue::List(x.iter().map(|v| v.clone().into()).collect::<Vec<_>>().into())
            }
        }
    }
}

impl FieldValue {
    fn discriminant(&self) -> isize {
        // Ensure this is the same order as the variant order at definition-time.
        match self {
            Self::Null => 0,
            Self::Int64(..) => 1,
            Self::Uint64(..) => 2,
            Self::Float64(..) => 3,
            Self::String(..) => 4,
            Self::Boolean(..) => 5,
            Self::Enum(..) => 6,
            Self::List(..) => 7,
        }
    }

    pub fn as_i64(&self) -> Option<i64> {
        match self {
            FieldValue::Uint64(u) => (*u).try_into().ok(),
            FieldValue::Int64(i) => Some(*i),
            FieldValue::Null
            | FieldValue::Float64(_)
            | FieldValue::String(_)
            | FieldValue::Boolean(_)
            | FieldValue::List(_)
            | FieldValue::Enum(_) => None,
        }
    }

    pub fn as_u64(&self) -> Option<u64> {
        match self {
            FieldValue::Uint64(u) => Some(*u),
            FieldValue::Int64(i) => (*i).try_into().ok(),
            FieldValue::Null
            | FieldValue::Float64(_)
            | FieldValue::String(_)
            | FieldValue::Boolean(_)
            | FieldValue::List(_)
            | FieldValue::Enum(_) => None,
        }
    }

    pub fn as_usize(&self) -> Option<usize> {
        match self {
            FieldValue::Uint64(u) => (*u).try_into().ok(),
            FieldValue::Int64(i) => (*i).try_into().ok(),
            FieldValue::Null
            | FieldValue::Float64(_)
            | FieldValue::String(_)
            | FieldValue::Boolean(_)
            | FieldValue::List(_)
            | FieldValue::Enum(_) => None,
        }
    }

    pub fn as_f64(&self) -> Option<f64> {
        match self {
            FieldValue::Float64(f) => Some(*f),
            _ => None,
        }
    }

    pub fn as_str(&self) -> Option<&str> {
        match self {
            FieldValue::String(s) => Some(s.as_ref()),
            _ => None,
        }
    }

    pub fn as_arc_str(&self) -> Option<&Arc<str>> {
        match self {
            FieldValue::String(s) => Some(s),
            _ => None,
        }
    }

    pub fn as_bool(&self) -> Option<bool> {
        match self {
            FieldValue::Boolean(b) => Some(*b),
            _ => None,
        }
    }

    pub fn as_slice(&self) -> Option<&[FieldValue]> {
        match self {
            FieldValue::List(l) => Some(l.as_ref()),
            _ => None,
        }
    }

    pub fn as_arc_slice(&self) -> Option<&Arc<[FieldValue]>> {
        match self {
            FieldValue::List(l) => Some(l),
            _ => None,
        }
    }

    pub fn as_vec_with<'a, T>(
        &'a self,
        inner: impl Fn(&'a FieldValue) -> Option<T>,
    ) -> Option<Vec<T>> {
        match self {
            FieldValue::List(l) => {
                let maybe_vec: Option<Vec<T>> = l.iter().map(inner).collect();
                maybe_vec
            }
            _ => None,
        }
    }

    pub(crate) fn structural_eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Uint64(l0), Self::Uint64(r0)) => l0 == r0,
            (Self::Int64(l0), Self::Int64(r0)) => l0 == r0,
            (Self::Float64(l0), Self::Float64(r0)) => {
                assert!(l0.is_finite());
                assert!(r0.is_finite());
                l0 == r0
            }
            (Self::String(l0), Self::String(r0)) => l0 == r0,
            (Self::Boolean(l0), Self::Boolean(r0)) => l0 == r0,
            (Self::List(l0), Self::List(r0)) => l0 == r0,
            (Self::Enum(l0), Self::Enum(r0)) => l0 == r0,
            _ => core::mem::discriminant(self) == core::mem::discriminant(other),
        }
    }

    fn compare_i64_to_u64(signed: i64, unsigned: u64) -> Ordering {
        if let Ok(conv) = unsigned.try_into() {
            // We succeeded in converting the unsigned value into signed.
            // Compare them on equal terms.
            signed.cmp(&conv)
        } else if let Ok(conv) = u64::try_from(signed) {
            // We succeeded in converting the signed value into unsigned.
            // Compare them on equal terms.
            conv.cmp(&unsigned)
        } else {
            // Both values are out of each other's valid range.
            // Since both values are the same bit width,
            // the signed value must be negative and the unsigned value must be very large.
            Ordering::Less
        }
    }
}

impl PartialOrd for FieldValue {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        if let (Self::Int64(l), Self::Uint64(r)) = (self, other) {
            Some(FieldValue::compare_i64_to_u64(*l, *r))
        } else if let (Self::Uint64(l), Self::Int64(r)) = (self, other) {
            Some(FieldValue::compare_i64_to_u64(*r, *l).reverse())
        } else {
            match (self, other) {
                (Self::Uint64(l0), Self::Uint64(r0)) => l0.partial_cmp(r0),
                (Self::Int64(l0), Self::Int64(r0)) => l0.partial_cmp(r0),
                (Self::Float64(l0), Self::Float64(r0)) => {
                    assert!(l0.is_finite());
                    assert!(r0.is_finite());
                    l0.partial_cmp(r0)
                }
                (Self::String(l0), Self::String(r0)) => l0.partial_cmp(r0),
                (Self::Boolean(l0), Self::Boolean(r0)) => l0.partial_cmp(r0),
                (Self::List(l0), Self::List(r0)) => l0.partial_cmp(r0),
                (Self::Enum(l0), Self::Enum(r0)) => l0.partial_cmp(r0),
                _ => self.discriminant().partial_cmp(&other.discriminant()),
            }
        }
    }
}

impl PartialEq for FieldValue {
    fn eq(&self, other: &Self) -> bool {
        if let (Self::Int64(l), Self::Uint64(r)) = (self, other) {
            FieldValue::compare_i64_to_u64(*l, *r).is_eq()
        } else if let (Self::Uint64(..), Self::Int64(..)) = (self, other) {
            Self::eq(other, self)
        } else {
            self.structural_eq(other)
        }
    }
}

impl Eq for FieldValue {}

impl AsRef<FieldValue> for FieldValue {
    fn as_ref(&self) -> &FieldValue {
        self
    }
}

impl From<Arc<str>> for FieldValue {
    fn from(v: Arc<str>) -> Self {
        Self::String(v)
    }
}

impl From<String> for FieldValue {
    fn from(v: String) -> Self {
        Self::String(v.into())
    }
}

impl From<&String> for FieldValue {
    fn from(v: &String) -> Self {
        Self::String(v.clone().into())
    }
}

impl From<&str> for FieldValue {
    fn from(v: &str) -> Self {
        Self::from(v.to_owned())
    }
}

impl From<bool> for FieldValue {
    fn from(v: bool) -> Self {
        Self::Boolean(v)
    }
}

/// Represents a finite (non-infinite, not-NaN) [f64] value
pub struct FiniteF64(f64);
impl From<FiniteF64> for FieldValue {
    fn from(f: FiniteF64) -> FieldValue {
        FieldValue::Float64(f.0)
    }
}

macro_rules! impl_finite_f64_try_from_float {
    ( $( $Float: ident )+ ) => {
        $(
            impl TryFrom<$Float> for FiniteF64 {
                type Error = ($Float, &'static str);

                fn try_from(v: $Float) -> Result<Self, Self::Error> {
                    if v.is_finite() {
                        Ok(Self(v.into()))
                    } else {
                        Err((v, "not a finite (non-infinite, not-NaN) value"))
                    }
                }
            }
        )+
    }
}

impl_finite_f64_try_from_float!(f32 f64);

macro_rules! impl_field_value_from_int {
    ( $( $Int: ident )+ ) => {
        $(
            impl From<$Int> for FieldValue {
                fn from(v: $Int) -> Self {
                    Self::Int64(v.into())
                }
            }
        )+
    }
}

macro_rules! impl_field_value_from_uint {
    ( $( $Uint: ident )+ ) => {
        $(
            impl From<$Uint> for FieldValue {
                fn from(v: $Uint) -> Self {
                    Self::Uint64(v.into())
                }
            }
        )+
    }
}

impl_field_value_from_int!(i8 i16 i32 i64);
impl_field_value_from_uint!(u8 u16 u32 u64);

impl TryFrom<Option<f32>> for FieldValue {
    type Error = (f32, &'static str);

    fn try_from(value: Option<f32>) -> Result<Self, Self::Error> {
        match value {
            None => Ok(FieldValue::Null),
            Some(v) => {
                let finite_f64 = FiniteF64::try_from(v);
                finite_f64.map(|x| x.into())
            }
        }
    }
}

impl TryFrom<Option<f64>> for FieldValue {
    type Error = (f64, &'static str);

    fn try_from(value: Option<f64>) -> Result<Self, Self::Error> {
        match value {
            None => Ok(FieldValue::Null),
            Some(v) => Ok(FiniteF64::try_from(v)?.into()),
        }
    }
}

impl<T: Into<FieldValue>> From<Option<T>> for FieldValue {
    fn from(opt: Option<T>) -> FieldValue {
        match opt {
            Some(inner) => inner.into(),
            None => FieldValue::Null,
        }
    }
}

impl<T: Into<FieldValue>> FromIterator<T> for FieldValue {
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = T>,
    {
        FieldValue::List(iter.into_iter().map(Into::into).collect())
    }
}

impl<T: Into<FieldValue>> From<Vec<T>> for FieldValue {
    fn from(vec: Vec<T>) -> FieldValue {
        vec.into_iter().collect()
    }
}

impl<T: Into<FieldValue> + Clone> From<&[T]> for FieldValue {
    fn from(slice: &[T]) -> FieldValue {
        slice.iter().cloned().collect()
    }
}

/// Converts a JSON number to a [FieldValue]
fn convert_number_to_field_value(n: &Number) -> Result<FieldValue, String> {
    // The order here matters!
    // Int64 must be before Uint64, which must be before Float64.
    // See the comment near the definition of FieldValue for details.
    if let Some(i) = n.as_i64() {
        Ok(FieldValue::Int64(i))
    } else if let Some(u) = n.as_u64() {
        Ok(FieldValue::Uint64(u))
    } else if let Some(f) = n.as_f64() {
        Ok(FieldValue::Float64(f))
    } else {
        unreachable!()
    }
}

impl TryFrom<Value> for FieldValue {
    type Error = String;

    fn try_from(value: Value) -> Result<Self, Self::Error> {
        match value {
            Value::Null => Ok(Self::Null),
            Value::Number(n) => convert_number_to_field_value(&n),
            Value::String(s) => Ok(Self::String(s.into())),
            Value::Boolean(b) => Ok(Self::Boolean(b)),
            Value::List(l) => l.into_iter().map(Self::try_from).collect::<Result<Self, _>>(),
            Value::Enum(n) => {
                // We have an enum value, so we know the variant name but the variant on its own
                // doesn't tell us the name of the enum type it belongs in. We'll have to determine
                // the name of the enum type from context. For now, it's None.
                Ok(Self::Enum(n.to_string().into()))
            }
            Value::Binary(_) => Err(String::from("Binary values are not supported")),
            Value::Variable(_) => Err(String::from("Cannot use a variable reference")),
            Value::Object(_) => Err(String::from("Object values are not supported")),
        }
    }
}

impl TryFrom<ConstValue> for FieldValue {
    type Error = String;

    fn try_from(value: ConstValue) -> Result<Self, Self::Error> {
        value.into_value().try_into()
    }
}

#[cfg(test)]
mod tests {
    use super::{FieldValue, FiniteF64};

    #[test]
    fn test_field_value_into() {
        #[track_caller]
        fn test(value: impl Into<FieldValue>, expected: FieldValue) {
            assert_eq!(value.into(), expected);
        }

        test(false, FieldValue::Boolean(false));

        test(123i64, FieldValue::Int64(123));
        test(123u64, FieldValue::Uint64(123));

        test(None::<i64>, FieldValue::Null);
        test(Some::<i64>(123), FieldValue::Int64(123));
        test(Some::<u64>(123), FieldValue::Uint64(123));

        test(FiniteF64::try_from(3.15).unwrap(), FieldValue::Float64(3.15));

        test("a &str", FieldValue::String("a &str".into()));
        test("a String".to_string(), FieldValue::String("a String".into()));

        test(vec![1, 2], FieldValue::List(vec![FieldValue::Int64(1), FieldValue::Int64(2)].into()));

        test(
            ["a String".to_string()].as_slice(),
            FieldValue::List(vec![FieldValue::String("a String".to_string().into())].into()),
        );
    }
}