Skip to main content

sql_fun_sqlast/sem/
cast.rs

1/// type cast definition
2#[derive(Debug, Clone)]
3pub struct CastDefinition {
4    cast_context: CastContext,
5}
6
7/// type cast context
8#[derive(Debug, Clone, Copy, PartialEq)]
9pub enum CastContext {
10    /// implicit and prefeerd
11    ImplicitPrefeerd,
12    /// implicit
13    Implicit,
14    /// assignment
15    Assignment,
16    /// explicit
17    Explicit,
18    /// non-conversion (same type)
19    NoConversion,
20}
21
22impl CastDefinition {
23    /// create with context
24    #[must_use]
25    pub fn new(cc: CastContext) -> Self {
26        Self { cast_context: cc }
27    }
28
29    /// is explicit
30    #[must_use]
31    pub fn use_in_explicit(&self) -> bool {
32        self.cast_context.use_in_explicit()
33    }
34
35    /// is implicit
36    #[must_use]
37    pub fn use_in_implicit(&self) -> bool {
38        self.cast_context.use_in_implicit()
39    }
40
41    /// get context
42    #[must_use]
43    pub fn context(&self) -> CastContext {
44        self.cast_context
45    }
46
47    /// in implicit and preferred
48    #[must_use]
49    pub fn is_implicit_preferred(&self) -> bool {
50        self.cast_context.is_implicit_preferred()
51    }
52
53    /// is no conversion
54    #[must_use]
55    pub fn is_no_coversion(&self) -> bool {
56        self.cast_context.is_no_coversion()
57    }
58}
59
60impl CastContext {
61    /// cast is defined, always use explicit
62    #[must_use]
63    pub fn use_in_explicit(&self) -> bool {
64        true
65    }
66
67    /// use in implicit
68    #[must_use]
69    pub fn use_in_implicit(&self) -> bool {
70        matches!(
71            self,
72            Self::Implicit | Self::ImplicitPrefeerd | Self::NoConversion
73        )
74    }
75
76    /// use in assignment context
77    #[must_use]
78    pub fn use_in_assignment(&self) -> bool {
79        matches!(
80            self,
81            Self::Implicit | Self::ImplicitPrefeerd | Self::Assignment | Self::NoConversion
82        )
83    }
84
85    /// Higher score means better match in overload resolution.
86    #[must_use]
87    pub fn overload_priority_score(&self) -> i32 {
88        match self {
89            Self::NoConversion => 10,
90            Self::ImplicitPrefeerd => 9,
91            Self::Implicit => 5,
92            // can not to use in overload resolution
93            _ => 0,
94        }
95    }
96
97    /// type cast is implicit
98    #[must_use]
99    pub fn is_implicit(&self) -> bool {
100        matches!(self, Self::Implicit | Self::ImplicitPrefeerd)
101    }
102
103    /// test cast is implicit and prefeered
104    #[must_use]
105    pub fn is_implicit_preferred(&self) -> bool {
106        matches!(self, Self::ImplicitPrefeerd)
107    }
108
109    /// test cast is non-conversion
110    #[must_use]
111    pub fn is_no_coversion(&self) -> bool {
112        matches!(self, Self::NoConversion)
113    }
114}