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
use std::cmp::Ordering;

use serde::{Deserialize, Serialize};

use super::Node;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Ty {
    Literal(TyLit),
    Named(String),
    Parameterized(Box<Ty>, Box<Node>),
    AnyOf(Vec<Ty>),

    /// Means that we have no information about the type of the variable and
    /// that it should be inferred from other usages.
    Infer,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, strum::EnumString, strum::Display)]
pub enum TyLit {
    #[strum(to_string = "table")]
    Table,
    #[strum(to_string = "column")]
    Column,
    #[strum(to_string = "scalar")]
    Scalar,
    #[strum(to_string = "integer")]
    Integer,
    #[strum(to_string = "float")]
    Float,
    #[strum(to_string = "boolean")]
    Boolean,
    #[strum(to_string = "string")]
    String,
    #[strum(to_string = "date")]
    Date,
    #[strum(to_string = "time")]
    Time,
    #[strum(to_string = "timestamp")]
    Timestamp,
}

impl Ty {
    pub const fn frame() -> Ty {
        Ty::Literal(TyLit::Table)
    }

    pub const fn column() -> Ty {
        Ty::Literal(TyLit::Column)
    }
}

impl From<TyLit> for Ty {
    fn from(lit: TyLit) -> Self {
        Ty::Literal(lit)
    }
}

impl Default for Ty {
    fn default() -> Self {
        Ty::Infer
    }
}

/// Implements a partial ordering or types:
/// - higher up are types that include many others (AnyOf, Any) and
/// - on the bottom are the atomic types (bool, string).
impl PartialOrd for Ty {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match (self, other) {
            (Self::Literal(l0), Self::Literal(r0)) => {
                if l0 == r0 {
                    Some(Ordering::Equal)
                } else {
                    None
                }
            }
            (Self::Parameterized(l0, l1), Self::Parameterized(r0, r1)) => {
                if l0 == r0 && l1 == r1 {
                    Some(Ordering::Equal)
                } else {
                    None
                }
            }
            (Self::AnyOf(many), one) => {
                if many.iter().any(|m| m == one) {
                    Some(Ordering::Greater)
                } else {
                    None
                }
            }
            (one, Self::AnyOf(many)) => {
                if many.iter().any(|m| m == one) {
                    Some(Ordering::Less)
                } else {
                    None
                }
            }
            _ => None,
        }
    }
}