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 crate::memdx::magic::Magic;
22use crate::memdx::opcode::OpCode;
23use crate::memdx::status::Status;
24
25#[derive(Debug, Clone, PartialEq, Eq, Hash)]
26pub struct ResponsePacket {
27    pub magic: Magic,
28    pub op_code: OpCode,
29    pub datatype: u8,
30    pub status: Status,
31    pub opaque: u32,
32    pub vbucket_id: Option<u16>,
33    pub cas: Option<u64>,
34    pub extras: Option<Vec<u8>>,
35    pub key: Option<Vec<u8>>,
36    pub value: Option<Vec<u8>>,
37    pub framing_extras: Option<Vec<u8>>,
38}
39
40impl ResponsePacket {
41    pub fn new(magic: Magic, op_code: OpCode, datatype: u8, status: Status, opaque: u32) -> Self {
42        Self {
43            magic,
44            op_code,
45            datatype,
46            status,
47            opaque,
48            vbucket_id: None,
49            cas: None,
50            extras: None,
51            key: None,
52            value: None,
53            framing_extras: None,
54        }
55    }
56}
57
58#[derive(Debug, Clone, PartialEq, Eq, Hash)]
59pub struct RequestPacket<'a> {
60    pub(crate) magic: Magic,
61    pub(crate) op_code: OpCode,
62    pub(crate) datatype: u8,
63    pub(crate) vbucket_id: Option<u16>,
64    pub(crate) cas: Option<u64>,
65    pub(crate) extras: Option<&'a [u8]>,
66    pub(crate) key: Option<&'a [u8]>,
67    pub(crate) value: Option<&'a [u8]>,
68    pub(crate) framing_extras: Option<&'a [u8]>,
69    pub(crate) opaque: Option<u32>,
70}
71
72impl<'a> RequestPacket<'a> {
73    pub fn new(magic: Magic, op_code: OpCode, datatype: u8) -> Self {
74        Self {
75            magic,
76            op_code,
77            datatype,
78            vbucket_id: None,
79            cas: None,
80            extras: None,
81            key: None,
82            value: None,
83            framing_extras: None,
84            opaque: None,
85        }
86    }
87
88    pub fn magic(mut self, magic: Magic) -> Self {
89        self.magic = magic;
90        self
91    }
92
93    pub fn op_code(mut self, op_code: OpCode) -> Self {
94        self.op_code = op_code;
95        self
96    }
97
98    pub fn datatype(mut self, datatype: u8) -> Self {
99        self.datatype = datatype;
100        self
101    }
102
103    pub fn vbucket_id(mut self, vbucket_id: u16) -> Self {
104        self.vbucket_id = Some(vbucket_id);
105        self
106    }
107
108    pub fn cas(mut self, cas: u64) -> Self {
109        self.cas = Some(cas);
110        self
111    }
112
113    pub fn extras(mut self, extras: &'a [u8]) -> Self {
114        self.extras = Some(extras);
115        self
116    }
117
118    pub fn key(mut self, key: &'a [u8]) -> Self {
119        self.key = Some(key);
120        self
121    }
122
123    pub fn value(mut self, value: &'a [u8]) -> Self {
124        self.value = Some(value);
125        self
126    }
127
128    pub fn framing_extras(mut self, framing_extras: &'a [u8]) -> Self {
129        self.framing_extras = Some(framing_extras);
130        self
131    }
132
133    pub fn opaque(mut self, opaque: u32) -> Self {
134        self.opaque = Some(opaque);
135        self
136    }
137}