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
use std::io::Cursor;

use crate::frame::message_auth_challenge::BodyResAuthChallenge;
use crate::frame::message_auth_success::BodyReqAuthSuccess;
use crate::frame::message_authenticate::BodyResAuthenticate;
use crate::frame::message_error::ErrorBody;
use crate::frame::message_event::BodyResEvent;
use crate::frame::message_result::{
    BodyResResultPrepared, BodyResResultRows, BodyResResultSetKeyspace, ResResultBody, RowsMetadata,
};
use crate::frame::message_supported::BodyResSupported;
use crate::frame::{FromCursor, Opcode, Version};
use crate::types::rows::Row;
use crate::{error, Error};

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum ResponseBody {
    Error(ErrorBody),
    Ready,
    Authenticate(BodyResAuthenticate),
    Supported(BodyResSupported),
    Result(ResResultBody),
    Event(BodyResEvent),
    AuthChallenge(BodyResAuthChallenge),
    AuthSuccess(BodyReqAuthSuccess),
}

// This implementation is incomplete so only enable in tests
#[cfg(test)]
use crate::frame::Serialize;
#[cfg(test)]
impl Serialize for ResponseBody {
    fn serialize(&self, cursor: &mut Cursor<&mut Vec<u8>>, version: Version) {
        match self {
            ResponseBody::Error(error_body) => {
                error_body.serialize(cursor, version);
            }
            ResponseBody::Ready => {}
            ResponseBody::Authenticate(auth) => {
                auth.serialize(cursor, version);
            }
            ResponseBody::Supported(supported) => {
                supported.serialize(cursor, version);
            }
            ResponseBody::Result(result) => {
                result.serialize(cursor, version);
            }
            ResponseBody::Event(event) => {
                event.serialize(cursor, version);
            }
            ResponseBody::AuthChallenge(auth_challenge) => {
                auth_challenge.serialize(cursor, version);
            }
            ResponseBody::AuthSuccess(auth_success) => {
                auth_success.serialize(cursor, version);
            }
        }
    }
}

impl ResponseBody {
    pub fn try_from(
        bytes: &[u8],
        response_type: Opcode,
        version: Version,
    ) -> error::Result<ResponseBody> {
        let mut cursor: Cursor<&[u8]> = Cursor::new(bytes);
        match response_type {
            Opcode::Error => ErrorBody::from_cursor(&mut cursor, version).map(ResponseBody::Error),
            Opcode::Ready => Ok(ResponseBody::Ready),
            Opcode::Authenticate => BodyResAuthenticate::from_cursor(&mut cursor, version)
                .map(ResponseBody::Authenticate),
            Opcode::Supported => {
                BodyResSupported::from_cursor(&mut cursor, version).map(ResponseBody::Supported)
            }
            Opcode::Result => {
                ResResultBody::from_cursor(&mut cursor, version).map(ResponseBody::Result)
            }
            Opcode::Event => {
                BodyResEvent::from_cursor(&mut cursor, version).map(ResponseBody::Event)
            }
            Opcode::AuthChallenge => BodyResAuthChallenge::from_cursor(&mut cursor, version)
                .map(ResponseBody::AuthChallenge),
            Opcode::AuthSuccess => {
                BodyReqAuthSuccess::from_cursor(&mut cursor, version).map(ResponseBody::AuthSuccess)
            }
            _ => Err(Error::NonResponseOpcode(response_type)),
        }
    }

    pub fn into_rows(self) -> Option<Vec<Row>> {
        match self {
            ResponseBody::Result(res) => res.into_rows(),
            _ => None,
        }
    }

    pub fn as_rows_metadata(&self) -> Option<&RowsMetadata> {
        match self {
            ResponseBody::Result(res) => res.as_rows_metadata(),
            _ => None,
        }
    }

    pub fn as_cols(&self) -> Option<&BodyResResultRows> {
        match *self {
            ResponseBody::Result(ResResultBody::Rows(ref rows)) => Some(rows),
            _ => None,
        }
    }

    /// Unwraps body and returns BodyResResultPrepared which contains an exact result of
    /// PREPARE query.
    pub fn into_prepared(self) -> Option<BodyResResultPrepared> {
        match self {
            ResponseBody::Result(res) => res.into_prepared(),
            _ => None,
        }
    }

    /// Unwraps body and returns BodyResResultPrepared which contains an exact result of
    /// use keyspace query.
    pub fn into_set_keyspace(self) -> Option<BodyResResultSetKeyspace> {
        match self {
            ResponseBody::Result(res) => res.into_set_keyspace(),
            _ => None,
        }
    }

    /// Unwraps body and returns BodyResEvent.
    pub fn into_server_event(self) -> Option<BodyResEvent> {
        match self {
            ResponseBody::Event(event) => Some(event),
            _ => None,
        }
    }

    pub fn authenticator(&self) -> Option<&str> {
        match *self {
            ResponseBody::Authenticate(ref auth) => Some(auth.data.as_str()),
            _ => None,
        }
    }

    pub fn into_error(self) -> Option<ErrorBody> {
        match self {
            ResponseBody::Error(err) => Some(err),
            _ => None,
        }
    }
}