-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathstartup.rs
47 lines (38 loc) · 1.42 KB
/
startup.rs
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
use thiserror::Error;
use crate::frame::frame_errors::CqlRequestSerializationError;
use std::{borrow::Cow, collections::HashMap, num::TryFromIntError};
use crate::{
frame::request::{RequestOpcode, SerializableRequest},
frame::types,
};
use super::DeserializableRequest;
pub struct Startup<'a> {
pub options: HashMap<Cow<'a, str>, Cow<'a, str>>,
}
impl SerializableRequest for Startup<'_> {
const OPCODE: RequestOpcode = RequestOpcode::Startup;
fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), CqlRequestSerializationError> {
types::write_string_map(&self.options, buf)
.map_err(StartupSerializationError::OptionsSerialization)?;
Ok(())
}
}
/// An error type returned when serialization of STARTUP request fails.
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum StartupSerializationError {
/// Failed to serialize startup options.
#[error("Malformed startup options: {0}")]
OptionsSerialization(TryFromIntError),
}
impl DeserializableRequest for Startup<'_> {
fn deserialize(buf: &mut &[u8]) -> Result<Self, super::RequestDeserializationError> {
// Note: this is inefficient, but it's only used for tests and it's not common
// to deserialize STARTUP frames anyway.
let options = types::read_string_map(buf)?
.into_iter()
.map(|(k, v)| (k.into(), v.into()))
.collect();
Ok(Self { options })
}
}