toasty-core 0.5.0

Core types, schema representations, and driver interface for Toasty
Documentation
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
//! Numeric type support for [`Value`], [`Expr`], and [`Type`].
//!
//! This module uses the `impl_num!` macro to generate, for each integer type
//! (`i8`..`u64`):
//!
//! - `Type::is_{ty}()` -- type predicate
//! - `Value::to_{ty}()` / `Value::to_{ty}_unwrap()` -- cross-width conversion
//! - `From<{ty}> for Value` / `TryFrom<Value> for {ty}`
//! - `PartialEq<{ty}>` for both `Value` and `Expr`
//!
//! # Examples
//!
//! ```
//! use toasty_core::stmt::{Value, Type};
//!
//! let v = Value::from(42_i64);
//! assert_eq!(v, 42_i64);
//! assert_eq!(v.to_i64(), Some(42));
//! assert!(Type::I64.is_i64());
//! ```

use super::{Expr, Type, Value};

macro_rules! try_from {
    ($v:expr, $ty:ty) => {
        match $v {
            Value::I8(v) => <$ty>::try_from(v).ok(),
            Value::I16(v) => <$ty>::try_from(v).ok(),
            Value::I32(v) => <$ty>::try_from(v).ok(),
            Value::I64(v) => <$ty>::try_from(v).ok(),
            Value::U8(v) => <$ty>::try_from(v).ok(),
            Value::U16(v) => <$ty>::try_from(v).ok(),
            Value::U32(v) => <$ty>::try_from(v).ok(),
            Value::U64(v) => <$ty>::try_from(v).ok(),
            _ => None,
        }
    };
}

macro_rules! impl_num {
    (
        $(
            $variant:ident($ty:ty) {
                $to:ident
                $to_unwrap:ident
                $is:ident
            } )*
    ) => {
        impl Type {
            $(
                /// Returns `true` if this type matches the corresponding integer variant.
                pub fn $is(&self) -> bool {
                    matches!(self, Self::$variant)
                }
            )*
        }

        impl Value {
            $(
                /// Attempts to convert this value to the target integer type.
                ///
                /// Returns `None` if the value is not an integer variant or is out of
                /// range for the target type. Conversion works across all integer
                /// widths and signedness.
                pub fn $to(&self) -> Option<$ty> {
                    try_from!(*self, $ty)
                }

                /// Converts this value to the target integer type, panicking on failure.
                ///
                /// # Panics
                ///
                /// Panics if the value is not an integer variant or is out of range.
                #[track_caller]
                pub fn $to_unwrap(&self) -> $ty {
                    try_from!(*self, $ty).expect("out of range integral type conversion attempted")
                }
            )*
        }

        $(
            impl PartialEq<$ty> for Value {
                fn eq(&self, other: &$ty) -> bool {
                    try_from!(*self, $ty).map(|v| v == *other).unwrap_or(false)
                }
            }

            impl PartialEq<Value> for $ty {
                fn eq(&self, other: &Value) -> bool {
                    other.eq(self)
                }
            }

            impl PartialEq<$ty> for Expr {
                fn eq(&self, other: &$ty) -> bool {
                    match self {
                        Expr::Value(value) => value.eq(other),
                        _ => false,
                    }
                }
            }

            impl PartialEq<Expr> for $ty {
                fn eq(&self, other: &Expr) -> bool {
                    other.eq(self)
                }
            }

            impl From<$ty> for Value {
                fn from(value: $ty) -> Self {
                    Self::$variant(value)
                }
            }

            impl From<&$ty> for Value {
                fn from(value: &$ty) -> Self {
                    Self::$variant(*value)
                }
            }

            impl TryFrom<Value> for $ty {
                type Error = crate::Error;

                fn try_from(value: Value) -> crate::Result<Self> {
                    value.$to().ok_or_else(|| {
                        crate::Error::type_conversion(value.clone(), stringify!($ty))
                    })
                }
            }

            #[cfg(feature = "assert-struct")]
            impl assert_struct::Like<$ty> for Value {
                fn like(&self, pattern: &$ty) -> bool {
                    try_from!(*self, $ty).map(|v| v == *pattern).unwrap_or(false)
                }
            }

            #[cfg(feature = "assert-struct")]
            impl assert_struct::Like<$ty> for Expr {
                fn like(&self, pattern: &$ty) -> bool {
                    match self {
                        Expr::Value(value) => value.like(pattern),
                        _ => false,
                    }
                }
            }
        )*
    };
}

macro_rules! impl_float {
    (
        $(
            $variant:ident($ty:ty) {
                $to:ident
                $to_unwrap:ident
                $is:ident
                $unwrap_msg:literal
            }
        )*
    ) => {
        impl Type {
            $(
                /// Returns `true` if this type matches the corresponding float variant.
                pub fn $is(&self) -> bool {
                    matches!(self, Self::$variant)
                }
            )*
        }

        $(
            impl From<$ty> for Value {
                fn from(value: $ty) -> Self {
                    Self::$variant(value)
                }
            }

            impl From<&$ty> for Value {
                fn from(value: &$ty) -> Self {
                    Self::$variant(*value)
                }
            }

            impl Value {
                /// Converts this value to the target float type, panicking on failure.
                ///
                /// # Panics
                ///
                /// Panics if the value is not a float variant or if a narrowing conversion
                /// overflows.
                #[track_caller]
                pub fn $to_unwrap(&self) -> $ty {
                    self.$to().expect($unwrap_msg)
                }
            }

            impl TryFrom<Value> for $ty {
                type Error = crate::Error;

                fn try_from(value: Value) -> crate::Result<Self> {
                    value.$to().ok_or_else(|| {
                        crate::Error::type_conversion(value.clone(), stringify!($ty))
                    })
                }
            }
        )*
    };
}

impl_float! {
    F32(f32) {
        to_f32
        to_f32_unwrap
        is_f32
        "value is not a finite f32"
    }
    F64(f64) {
        to_f64
        to_f64_unwrap
        is_f64
        "value is not a float type"
    }
}

impl Value {
    /// Attempts to convert this value to `f32`.
    ///
    /// Returns `None` if the value is not a float variant, or if a `F64` value
    /// overflows `f32` range (would produce infinity from a finite value).
    pub fn to_f32(&self) -> Option<f32> {
        match self {
            Value::F32(v) => Some(*v),
            Value::F64(v) => {
                let converted = *v as f32;
                if converted.is_infinite() && !v.is_infinite() {
                    None
                } else {
                    Some(converted)
                }
            }
            _ => None,
        }
    }

    /// Attempts to convert this value to `f64`.
    ///
    /// Returns `None` if the value is not a float variant.
    /// `F32 → f64` is always safe (widening conversion).
    pub fn to_f64(&self) -> Option<f64> {
        match self {
            Value::F32(v) => Some(*v as f64),
            Value::F64(v) => Some(*v),
            _ => None,
        }
    }
}

impl_num! {
    I8(i8) {
        to_i8
        to_i8_unwrap
        is_i8
    }
    I16(i16) {
        to_i16
        to_i16_unwrap
        is_i16
    }
    I32(i32) {
        to_i32
        to_i32_unwrap
        is_i32
    }
    I64(i64) {
        to_i64
        to_i64_unwrap
        is_i64
    }
    U8(u8) {
        to_u8
        to_u8_unwrap
        is_u8
    }
    U16(u16) {
        to_u16
        to_u16_unwrap
        is_u16
    }
    U32(u32) {
        to_u32
        to_u32_unwrap
        is_u32
    }
    U64(u64) {
        to_u64
        to_u64_unwrap
        is_u64
    }
}

impl From<usize> for Value {
    fn from(value: usize) -> Self {
        Value::U64(value as u64)
    }
}

impl From<&usize> for Value {
    fn from(value: &usize) -> Self {
        Value::U64(*value as u64)
    }
}

impl From<isize> for Value {
    fn from(value: isize) -> Self {
        Value::I64(value as i64)
    }
}

impl From<&isize> for Value {
    fn from(value: &isize) -> Self {
        Value::I64(*value as i64)
    }
}

#[cfg(feature = "assert-struct")]
impl assert_struct::Like<usize> for Value {
    fn like(&self, pattern: &usize) -> bool {
        usize::try_from(self)
            .map(|v| v == *pattern)
            .unwrap_or(false)
    }
}

#[cfg(feature = "assert-struct")]
impl assert_struct::Like<usize> for Expr {
    fn like(&self, pattern: &usize) -> bool {
        match self {
            Expr::Value(v) => v.like(pattern),
            _ => false,
        }
    }
}

#[cfg(feature = "assert-struct")]
impl assert_struct::Like<isize> for Value {
    fn like(&self, pattern: &isize) -> bool {
        isize::try_from(self)
            .map(|v| v == *pattern)
            .unwrap_or(false)
    }
}

#[cfg(feature = "assert-struct")]
impl assert_struct::Like<isize> for Expr {
    fn like(&self, pattern: &isize) -> bool {
        match self {
            Expr::Value(v) => v.like(pattern),
            _ => false,
        }
    }
}

// Pointer-sized integers convert from their fixed-size equivalents
impl TryFrom<Value> for usize {
    type Error = crate::Error;

    fn try_from(value: Value) -> crate::Result<Self> {
        (&value).try_into()
    }
}

impl TryFrom<&Value> for usize {
    type Error = crate::Error;

    fn try_from(value: &Value) -> crate::Result<Self> {
        let u64_val = value
            .to_u64()
            .ok_or_else(|| crate::Error::type_conversion(value.clone(), "usize"))?;
        u64_val
            .try_into()
            .map_err(|_| crate::Error::type_conversion(Value::U64(u64_val), "usize"))
    }
}

impl TryFrom<Value> for isize {
    type Error = crate::Error;

    fn try_from(value: Value) -> crate::Result<Self> {
        (&value).try_into()
    }
}

impl TryFrom<&Value> for isize {
    type Error = crate::Error;

    fn try_from(value: &Value) -> crate::Result<Self> {
        let i64_val = value
            .to_i64()
            .ok_or_else(|| crate::Error::type_conversion(value.clone(), "isize"))?;
        i64_val
            .try_into()
            .map_err(|_| crate::Error::type_conversion(Value::I64(i64_val), "isize"))
    }
}