icydb_core/db/predicate/
coercion.rs1use crate::value::CoercionFamily;
7use std::fmt;
8
9#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
19pub enum CoercionId {
20 Strict,
21 NumericWiden,
22 TextCasefold,
23 CollectionElement,
24}
25
26impl CoercionId {
27 #[must_use]
29 pub const fn plan_hash_tag(self) -> u8 {
30 match self {
31 Self::Strict => 0x01,
32 Self::NumericWiden => 0x02,
33 Self::TextCasefold => 0x04,
34 Self::CollectionElement => 0x05,
35 }
36 }
37}
38
39#[derive(Clone, Eq, PartialEq)]
46pub struct CoercionSpec {
47 pub(crate) id: CoercionId,
48 pub(crate) params: Vec<(String, String)>,
49}
50
51impl CoercionSpec {
52 #[must_use]
53 pub const fn new(id: CoercionId) -> Self {
54 Self {
55 id,
56 params: Vec::new(),
57 }
58 }
59
60 #[must_use]
62 pub const fn id(&self) -> CoercionId {
63 self.id
64 }
65
66 #[must_use]
68 pub const fn params(&self) -> &[(String, String)] {
69 self.params.as_slice()
70 }
71}
72
73impl fmt::Debug for CoercionSpec {
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 f.debug_struct("CoercionSpec")
76 .field("id", &self.id)
77 .field("params", &CoercionParamsDebug(&self.params))
78 .finish()
79 }
80}
81
82impl Default for CoercionSpec {
83 fn default() -> Self {
84 Self::new(CoercionId::Strict)
85 }
86}
87
88struct CoercionParamsDebug<'a>(&'a [(String, String)]);
91
92impl fmt::Debug for CoercionParamsDebug<'_> {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 let mut debug = f.debug_map();
95 for (key, value) in self.0 {
96 debug.entry(key, value);
97 }
98
99 debug.finish()
100 }
101}
102
103#[derive(Clone, Copy, Debug, Eq, PartialEq)]
110pub(crate) enum CoercionRuleFamily {
111 Any,
112 Family(CoercionFamily),
113}
114
115#[derive(Clone, Copy, Debug, Eq, PartialEq)]
122pub(crate) struct CoercionRule {
123 left: CoercionRuleFamily,
124 right: CoercionRuleFamily,
125 id: CoercionId,
126}
127
128pub(crate) const COERCION_TABLE: &[CoercionRule] = &[
129 CoercionRule {
130 left: CoercionRuleFamily::Any,
131 right: CoercionRuleFamily::Any,
132 id: CoercionId::Strict,
133 },
134 CoercionRule {
135 left: CoercionRuleFamily::Family(CoercionFamily::Numeric),
136 right: CoercionRuleFamily::Family(CoercionFamily::Numeric),
137 id: CoercionId::NumericWiden,
138 },
139 CoercionRule {
140 left: CoercionRuleFamily::Family(CoercionFamily::Textual),
141 right: CoercionRuleFamily::Family(CoercionFamily::Textual),
142 id: CoercionId::TextCasefold,
143 },
144 CoercionRule {
145 left: CoercionRuleFamily::Any,
146 right: CoercionRuleFamily::Any,
147 id: CoercionId::CollectionElement,
148 },
149];
150
151#[must_use]
153pub(in crate::db) fn supports_coercion(
154 left: CoercionFamily,
155 right: CoercionFamily,
156 id: CoercionId,
157) -> bool {
158 COERCION_TABLE.iter().any(|rule| {
159 rule.id == id && family_matches(rule.left, left) && family_matches(rule.right, right)
160 })
161}
162
163fn family_matches(rule: CoercionRuleFamily, value: CoercionFamily) -> bool {
164 match rule {
165 CoercionRuleFamily::Any => true,
166 CoercionRuleFamily::Family(expected) => expected == value,
167 }
168}