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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! Provides types for dealing with row deserialization.

use std::fmt::Display;

use thiserror::Error;

use super::value::DeserializeValue;
use super::{make_error_replace_rust_name, DeserializationError, FrameSlice, TypeCheckError};
use crate::frame::response::result::{ColumnSpec, ColumnType, CqlValue, Row};

/// Represents a raw, unparsed column value.
#[non_exhaustive]
pub struct RawColumn<'frame, 'metadata> {
    pub index: usize,
    pub spec: &'metadata ColumnSpec<'metadata>,
    pub slice: Option<FrameSlice<'frame>>,
}

/// Iterates over columns of a single row.
#[derive(Clone, Debug)]
pub struct ColumnIterator<'frame, 'metadata> {
    specs: std::iter::Enumerate<std::slice::Iter<'metadata, ColumnSpec<'metadata>>>,
    slice: FrameSlice<'frame>,
}

impl<'frame, 'metadata> ColumnIterator<'frame, 'metadata> {
    /// Creates a new iterator over a single row.
    ///
    /// - `specs` - information about columns of the serialized response,
    /// - `slice` - a [FrameSlice] which points to the serialized row.
    #[inline]
    pub(crate) fn new(
        specs: &'metadata [ColumnSpec<'metadata>],
        slice: FrameSlice<'frame>,
    ) -> Self {
        Self {
            specs: specs.iter().enumerate(),
            slice,
        }
    }

    /// Returns the remaining number of columns that this iterator is expected
    /// to return.
    #[inline]
    pub fn columns_remaining(&self) -> usize {
        self.specs.len()
    }
}

impl<'frame, 'metadata> Iterator for ColumnIterator<'frame, 'metadata> {
    type Item = Result<RawColumn<'frame, 'metadata>, DeserializationError>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let (column_index, spec) = self.specs.next()?;
        Some(
            self.slice
                .read_cql_bytes()
                .map(|slice| RawColumn {
                    index: column_index,
                    spec,
                    slice,
                })
                .map_err(|err| {
                    mk_deser_err::<Self>(
                        BuiltinDeserializationErrorKind::RawColumnDeserializationFailed {
                            column_index,
                            column_name: spec.name().to_owned(),
                            err: DeserializationError::new(err),
                        },
                    )
                }),
        )
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.specs.size_hint()
    }
}

/// A type that can be deserialized from a row that was returned from a query.
///
/// For tips on how to write a custom implementation of this trait, see the
/// documentation of the parent module.
///
/// The crate also provides a derive macro which allows to automatically
/// implement the trait for a custom type. For more details on what the macro
/// is capable of, see its documentation.
pub trait DeserializeRow<'frame, 'metadata>
where
    Self: Sized,
{
    /// Checks that the schema of the result matches what this type expects.
    ///
    /// This function can check whether column types and names match the
    /// expectations.
    fn type_check(specs: &[ColumnSpec]) -> Result<(), TypeCheckError>;

    /// Deserializes a row from given column iterator.
    ///
    /// This function can assume that the driver called `type_check` to verify
    /// the row's type. Note that `deserialize` is not an unsafe function,
    /// so it should not use the assumption about `type_check` being called
    /// as an excuse to run `unsafe` code.
    fn deserialize(row: ColumnIterator<'frame, 'metadata>) -> Result<Self, DeserializationError>;
}

// raw deserialization as ColumnIterator

// What is the purpose of implementing DeserializeRow for ColumnIterator?
//
// Sometimes users might be interested in operating on ColumnIterator directly.
// Implementing DeserializeRow for it allows us to simplify our interface. For example,
// we have `QueryResult::rows<T: DeserializeRow>()` - you can put T = ColumnIterator
// instead of having a separate rows_raw function or something like this.
impl<'frame, 'metadata> DeserializeRow<'frame, 'metadata> for ColumnIterator<'frame, 'metadata> {
    #[inline]
    fn type_check(_specs: &[ColumnSpec]) -> Result<(), TypeCheckError> {
        Ok(())
    }

    #[inline]
    fn deserialize(row: ColumnIterator<'frame, 'metadata>) -> Result<Self, DeserializationError> {
        Ok(row)
    }
}

make_error_replace_rust_name!(
    _typck_error_replace_rust_name,
    TypeCheckError,
    BuiltinTypeCheckError
);

make_error_replace_rust_name!(
    deser_error_replace_rust_name,
    DeserializationError,
    BuiltinDeserializationError
);

// legacy/dynamic deserialization as Row
//
/// While no longer encouraged (because the new framework encourages deserializing
/// directly into desired types, entirely bypassing [CqlValue]), this can be indispensable
/// for some use cases, i.e. those involving dynamic parsing (ORMs?).
impl<'frame, 'metadata> DeserializeRow<'frame, 'metadata> for Row {
    #[inline]
    fn type_check(_specs: &[ColumnSpec]) -> Result<(), TypeCheckError> {
        // CqlValues accept all types, no type checking needed.
        Ok(())
    }

    #[inline]
    fn deserialize(
        mut row: ColumnIterator<'frame, 'metadata>,
    ) -> Result<Self, DeserializationError> {
        let mut columns = Vec::with_capacity(row.size_hint().0);
        while let Some(column) = row
            .next()
            .transpose()
            .map_err(deser_error_replace_rust_name::<Self>)?
        {
            columns.push(
                <Option<CqlValue>>::deserialize(column.spec.typ(), column.slice).map_err(
                    |err| {
                        mk_deser_err::<Self>(
                            BuiltinDeserializationErrorKind::ColumnDeserializationFailed {
                                column_index: column.index,
                                column_name: column.spec.name().to_owned(),
                                err,
                            },
                        )
                    },
                )?,
            );
        }
        Ok(Self { columns })
    }
}

// tuples
//
/// This is the new encouraged way for deserializing a row.
/// If only you know the exact column types in advance, you had better deserialize the row
/// to a tuple. The new deserialization framework will take care of all type checking
/// and needed conversions, issuing meaningful errors in case something goes wrong.
macro_rules! impl_tuple {
    ($($Ti:ident),*; $($idx:literal),*; $($idf:ident),*) => {
        impl<'frame, 'metadata, $($Ti),*> DeserializeRow<'frame, 'metadata> for ($($Ti,)*)
        where
            $($Ti: DeserializeValue<'frame, 'metadata>),*
        {
            fn type_check(specs: &[ColumnSpec]) -> Result<(), TypeCheckError> {
                const TUPLE_LEN: usize = (&[$($idx),*] as &[i32]).len();

                let column_types_iter = || specs.iter().map(|spec| spec.typ().clone().into_owned());
                if let [$($idf),*] = &specs {
                    $(
                        <$Ti as DeserializeValue<'frame, 'metadata>>::type_check($idf.typ())
                            .map_err(|err| mk_typck_err::<Self>(column_types_iter(), BuiltinTypeCheckErrorKind::ColumnTypeCheckFailed {
                                column_index: $idx,
                                column_name: specs[$idx].name().to_owned(),
                                err
                            }))?;
                    )*
                    Ok(())
                } else {
                    Err(mk_typck_err::<Self>(column_types_iter(), BuiltinTypeCheckErrorKind::WrongColumnCount {
                        rust_cols: TUPLE_LEN, cql_cols: specs.len()
                    }))
                }
            }

            fn deserialize(mut row: ColumnIterator<'frame, 'metadata>) -> Result<Self, DeserializationError> {
                const TUPLE_LEN: usize = (&[$($idx),*] as &[i32]).len();

                let ret = (
                    $({
                        let column = row.next().unwrap_or_else(|| unreachable!(
                            "Typecheck should have prevented this scenario! Column count mismatch: rust type {}, cql row {}",
                            TUPLE_LEN,
                            $idx
                        )).map_err(deser_error_replace_rust_name::<Self>)?;

                        <$Ti as DeserializeValue<'frame, 'metadata>>::deserialize(column.spec.typ(), column.slice)
                            .map_err(|err| mk_deser_err::<Self>(BuiltinDeserializationErrorKind::ColumnDeserializationFailed {
                                column_index: column.index,
                                column_name: column.spec.name().to_owned(),
                                err,
                            }))?
                    },)*
                );
                assert!(
                    row.next().is_none(),
                    "Typecheck should have prevented this scenario! Column count mismatch: rust type {}, cql row is bigger",
                    TUPLE_LEN,
                );
                Ok(ret)
            }
        }
    }
}

use super::value::impl_tuple_multiple;

// Implements row-to-tuple deserialization for all tuple sizes up to 16.
impl_tuple_multiple!(
    T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15;
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15;
    t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15
);

// Error facilities

/// Failed to type check incoming result column types again given Rust type,
/// one of the types having support built into the driver.
#[derive(Debug, Error, Clone)]
#[error("Failed to type check the Rust type {rust_name} against CQL column types {cql_types:?} : {kind}")]
pub struct BuiltinTypeCheckError {
    /// Name of the Rust type used to represent the values.
    pub rust_name: &'static str,

    /// The CQL types of the values that the Rust type was being deserialized from.
    pub cql_types: Vec<ColumnType<'static>>,

    /// Detailed information about the failure.
    pub kind: BuiltinTypeCheckErrorKind,
}

// Not part of the public API; used in derive macros.
#[doc(hidden)]
pub fn mk_typck_err<T>(
    cql_types: impl IntoIterator<Item = ColumnType<'static>>,
    kind: impl Into<BuiltinTypeCheckErrorKind>,
) -> TypeCheckError {
    mk_typck_err_named(std::any::type_name::<T>(), cql_types, kind)
}

fn mk_typck_err_named(
    name: &'static str,
    cql_types: impl IntoIterator<Item = ColumnType<'static>>,
    kind: impl Into<BuiltinTypeCheckErrorKind>,
) -> TypeCheckError {
    TypeCheckError::new(BuiltinTypeCheckError {
        rust_name: name,
        cql_types: Vec::from_iter(cql_types),
        kind: kind.into(),
    })
}

/// Describes why type checking incoming result column types again given Rust type failed.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum BuiltinTypeCheckErrorKind {
    /// The Rust type expects `rust_cols` columns, but the statement operates on `cql_cols`.
    WrongColumnCount {
        /// The number of values that the Rust type provides.
        rust_cols: usize,

        /// The number of columns that the statement operates on.
        cql_cols: usize,
    },

    /// The CQL row contains a column for which a corresponding field is not found
    /// in the Rust type.
    ColumnWithUnknownName {
        /// Index of the excess column.
        column_index: usize,

        /// Name of the column that is present in CQL row but not in the Rust type.
        column_name: String,
    },

    /// Several values required by the Rust type are not provided by the DB.
    ValuesMissingForColumns {
        /// Names of the columns in the Rust type for which the DB doesn't
        /// provide value.
        column_names: Vec<&'static str>,
    },

    /// A different column name was expected at given position.
    ColumnNameMismatch {
        /// Index of the field determining the expected name.
        field_index: usize,

        /// Index of the column having mismatched name.
        column_index: usize,

        /// Name of the column, as expected by the Rust type.
        rust_column_name: &'static str,

        /// Name of the column for which the DB requested a value.
        db_column_name: String,
    },

    /// Column type check failed between Rust type and DB type at given position (=in given column).
    ColumnTypeCheckFailed {
        /// Index of the column.
        column_index: usize,

        /// Name of the column, as provided by the DB.
        column_name: String,

        /// Inner type check error due to the type mismatch.
        err: TypeCheckError,
    },

    /// Duplicated column in DB metadata.
    DuplicatedColumn {
        /// Column index of the second occurrence of the column with the same name.
        column_index: usize,

        /// The name of the duplicated column.
        column_name: &'static str,
    },
}

impl Display for BuiltinTypeCheckErrorKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BuiltinTypeCheckErrorKind::WrongColumnCount {
                rust_cols,
                cql_cols,
            } => {
                write!(f, "wrong column count: the statement operates on {cql_cols} columns, but the given rust types contains {rust_cols}")
            }
            BuiltinTypeCheckErrorKind::ColumnWithUnknownName { column_name, column_index } => {
                write!(
                    f,
                    "the CQL row contains a column {} at column index {}, but the corresponding field is not found in the Rust type",
                    column_name,
                    column_index,
                )
            }
            BuiltinTypeCheckErrorKind::ValuesMissingForColumns { column_names } => {
                write!(
                    f,
                    "values for columns {:?} are missing from the DB data but are required by the Rust type",
                    column_names
                )
            },
            BuiltinTypeCheckErrorKind::ColumnNameMismatch {
                field_index,
                column_index,rust_column_name,
                db_column_name
            } => write!(
                f,
                "expected column with name {} at column index {}, but the Rust field name at corresponding field index {} is {}",
                db_column_name,
                column_index,
                field_index,
                rust_column_name,
            ),
            BuiltinTypeCheckErrorKind::ColumnTypeCheckFailed {
                column_index,
                column_name,
                err,
            } => write!(
                f,
                "mismatched types in column {column_name} at index {column_index}: {err}"
            ),
            BuiltinTypeCheckErrorKind::DuplicatedColumn { column_name, column_index } => write!(
                f,
                "column {} occurs more than once in DB metadata; second occurrence is at column index {}",
                column_name,
                column_index,
            ),
        }
    }
}

/// Failed to deserialize a row from the DB response, represented by one of the types
/// built into the driver.
#[derive(Debug, Error, Clone)]
#[error("Failed to deserialize query result row {rust_name}: {kind}")]
pub struct BuiltinDeserializationError {
    /// Name of the Rust type used to represent the row.
    pub rust_name: &'static str,

    /// Detailed information about the failure.
    pub kind: BuiltinDeserializationErrorKind,
}

// Not part of the public API; used in derive macros.
#[doc(hidden)]
pub fn mk_deser_err<T>(kind: impl Into<BuiltinDeserializationErrorKind>) -> DeserializationError {
    mk_deser_err_named(std::any::type_name::<T>(), kind)
}

fn mk_deser_err_named(
    name: &'static str,
    kind: impl Into<BuiltinDeserializationErrorKind>,
) -> DeserializationError {
    DeserializationError::new(BuiltinDeserializationError {
        rust_name: name,
        kind: kind.into(),
    })
}

/// Describes why deserializing a result row failed.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum BuiltinDeserializationErrorKind {
    /// One of the columns failed to deserialize.
    ColumnDeserializationFailed {
        /// Index of the column that failed to deserialize.
        column_index: usize,

        /// Name of the column that failed to deserialize.
        column_name: String,

        /// The error that caused the column deserialization to fail.
        err: DeserializationError,
    },

    /// One of the raw columns failed to deserialize, most probably
    /// due to the invalid column structure inside a row in the frame.
    RawColumnDeserializationFailed {
        /// Index of the raw column that failed to deserialize.
        column_index: usize,

        /// Name of the raw column that failed to deserialize.
        column_name: String,

        /// The error that caused the raw column deserialization to fail.
        err: DeserializationError,
    },
}

impl Display for BuiltinDeserializationErrorKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BuiltinDeserializationErrorKind::ColumnDeserializationFailed {
                column_index,
                column_name,
                err,
            } => {
                write!(
                    f,
                    "failed to deserialize column {column_name} at index {column_index}: {err}"
                )
            }
            BuiltinDeserializationErrorKind::RawColumnDeserializationFailed {
                column_index,
                column_name,
                err,
            } => {
                write!(
                    f,
                    "failed to deserialize raw column {column_name} at index {column_index} (most probably due to invalid column structure inside a row): {err}"
                )
            }
        }
    }
}

#[cfg(test)]
#[path = "row_tests.rs"]
pub(crate) mod tests;

/// ```compile_fail
///
/// #[derive(scylla_macros::DeserializeRow)]
/// #[scylla(crate = scylla_cql, skip_name_checks)]
/// struct TestRow {}
/// ```
fn _test_struct_deserialization_name_check_skip_requires_enforce_order() {}

/// ```compile_fail
///
/// #[derive(scylla_macros::DeserializeRow)]
/// #[scylla(crate = scylla_cql, skip_name_checks)]
/// struct TestRow {
///     #[scylla(rename = "b")]
///     a: i32,
/// }
/// ```
fn _test_struct_deserialization_skip_name_check_conflicts_with_rename() {}

/// ```compile_fail
///
/// #[derive(scylla_macros::DeserializeRow)]
/// #[scylla(crate = scylla_cql)]
/// struct TestRow {
///     #[scylla(rename = "b")]
///     a: i32,
///     b: String,
/// }
/// ```
fn _test_struct_deserialization_skip_rename_collision_with_field() {}

/// ```compile_fail
///
/// #[derive(scylla_macros::DeserializeRow)]
/// #[scylla(crate = scylla_cql)]
/// struct TestRow {
///     #[scylla(rename = "c")]
///     a: i32,
///     #[scylla(rename = "c")]
///     b: String,
/// }
/// ```
fn _test_struct_deserialization_rename_collision_with_another_rename() {}