Skip to main content

couchbase_core/memdx/
packet.rs

1/*
2 *
3 *  * Copyright (c) 2025 Couchbase, Inc.
4 *  *
5 *  * Licensed under the Apache License, Version 2.0 (the "License");
6 *  * you may not use this file except in compliance with the License.
7 *  * You may obtain a copy of the License at
8 *  *
9 *  *    http://www.apache.org/licenses/LICENSE-2.0
10 *  *
11 *  * Unless required by applicable law or agreed to in writing, software
12 *  * distributed under the License is distributed on an "AS IS" BASIS,
13 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  * See the License for the specific language governing permissions and
15 *  * limitations under the License.
16 *
17 */
18
19use std::fmt::Debug;
20
21use bytes::Bytes;
22
23use crate::memdx::magic::Magic;
24use crate::memdx::opcode::OpCode;
25use crate::memdx::status::Status;
26
27#[derive(Debug, Clone, PartialEq, Eq, Hash)]
28pub struct ResponsePacket {
29    pub magic: Magic,
30    pub op_code: OpCode,
31    pub datatype: u8,
32    pub status: Status,
33    pub opaque: u32,
34    pub vbucket_id: Option<u16>,
35    pub cas: Option<u64>,
36    pub extras: Option<Bytes>,
37    pub key: Option<Bytes>,
38    pub value: Option<Bytes>,
39    pub framing_extras: Option<Bytes>,
40}
41
42impl ResponsePacket {
43    pub fn new(magic: Magic, op_code: OpCode, datatype: u8, status: Status, opaque: u32) -> Self {
44        Self {
45            magic,
46            op_code,
47            datatype,
48            status,
49            opaque,
50            vbucket_id: None,
51            cas: None,
52            extras: None,
53            key: None,
54            value: None,
55            framing_extras: None,
56        }
57    }
58}
59
60#[derive(Debug, Clone, PartialEq, Eq, Hash)]
61pub struct RequestPacket<'a> {
62    pub(crate) magic: Magic,
63    pub(crate) op_code: OpCode,
64    pub(crate) datatype: u8,
65    pub(crate) vbucket_id: Option<u16>,
66    pub(crate) cas: Option<u64>,
67    pub(crate) extras: Option<&'a [u8]>,
68    pub(crate) key: Option<&'a [u8]>,
69    pub(crate) value: Option<&'a [u8]>,
70    pub(crate) framing_extras: Option<&'a [u8]>,
71    pub(crate) opaque: Option<u32>,
72}
73
74impl<'a> RequestPacket<'a> {
75    pub fn new(magic: Magic, op_code: OpCode, datatype: u8) -> Self {
76        Self {
77            magic,
78            op_code,
79            datatype,
80            vbucket_id: None,
81            cas: None,
82            extras: None,
83            key: None,
84            value: None,
85            framing_extras: None,
86            opaque: None,
87        }
88    }
89
90    pub fn magic(mut self, magic: Magic) -> Self {
91        self.magic = magic;
92        self
93    }
94
95    pub fn op_code(mut self, op_code: OpCode) -> Self {
96        self.op_code = op_code;
97        self
98    }
99
100    pub fn datatype(mut self, datatype: u8) -> Self {
101        self.datatype = datatype;
102        self
103    }
104
105    pub fn vbucket_id(mut self, vbucket_id: u16) -> Self {
106        self.vbucket_id = Some(vbucket_id);
107        self
108    }
109
110    pub fn cas(mut self, cas: u64) -> Self {
111        self.cas = Some(cas);
112        self
113    }
114
115    pub fn extras(mut self, extras: &'a [u8]) -> Self {
116        self.extras = Some(extras);
117        self
118    }
119
120    pub fn key(mut self, key: &'a [u8]) -> Self {
121        self.key = Some(key);
122        self
123    }
124
125    pub fn value(mut self, value: &'a [u8]) -> Self {
126        self.value = Some(value);
127        self
128    }
129
130    pub fn framing_extras(mut self, framing_extras: &'a [u8]) -> Self {
131        self.framing_extras = Some(framing_extras);
132        self
133    }
134
135    pub fn opaque(mut self, opaque: u32) -> Self {
136        self.opaque = Some(opaque);
137        self
138    }
139}