toasty_core/driver/
response.rs1use crate::{stmt, Result};
2
3#[derive(Debug)]
4pub struct Response {
5 pub rows: Rows,
6}
7
8#[derive(Debug)]
9pub enum Rows {
10 Count(u64),
12
13 Value(stmt::Value),
15
16 Stream(stmt::ValueStream),
18}
19
20impl Response {
21 pub fn count(count: u64) -> Self {
22 Self {
23 rows: Rows::Count(count),
24 }
25 }
26
27 pub fn value_stream(values: impl Into<stmt::ValueStream>) -> Self {
28 Self {
29 rows: Rows::value_stream(values),
30 }
31 }
32
33 pub fn empty_value_stream() -> Self {
34 Self {
35 rows: Rows::Stream(stmt::ValueStream::default()),
36 }
37 }
38}
39
40impl Rows {
41 pub fn value_stream(values: impl Into<stmt::ValueStream>) -> Self {
42 Self::Stream(values.into())
43 }
44
45 pub fn is_count(&self) -> bool {
46 matches!(self, Self::Count(_))
47 }
48
49 pub async fn dup(&mut self) -> Result<Self> {
50 match self {
51 Rows::Count(count) => Ok(Rows::Count(*count)),
52 Rows::Value(value) => Ok(Rows::Value(value.clone())),
53 Rows::Stream(values) => Ok(Rows::Stream(values.dup().await?)),
54 }
55 }
56
57 pub fn try_clone(&self) -> Option<Self> {
58 match self {
59 Rows::Count(count) => Some(Rows::Count(*count)),
60 Rows::Value(value) => Some(Rows::Value(value.clone())),
61 Rows::Stream(values) => values.try_clone().map(Rows::Stream),
62 }
63 }
64
65 #[track_caller]
66 pub fn into_count(self) -> u64 {
67 match self {
68 Rows::Count(count) => count,
69 _ => todo!("rows={self:#?}"),
70 }
71 }
72
73 pub async fn collect_as_value(self) -> Result<stmt::Value> {
74 match self {
75 Rows::Count(_) => panic!("expected value; actual={self:#?}"),
76 Rows::Value(value) => Ok(value),
77 Rows::Stream(stream) => Ok(stmt::Value::List(stream.collect().await?)),
78 }
79 }
80
81 pub fn into_value_stream(self) -> stmt::ValueStream {
82 match self {
83 Rows::Value(stmt::Value::List(items)) => stmt::ValueStream::from_vec(items),
84 Rows::Stream(stream) => stream,
85 _ => panic!("expected ValueStream; actual={self:#?}"),
86 }
87 }
88}