1use crate::CommandArg;
20use bytes::Bytes;
21use redis_tower_core::{Command, Frame, RedisError};
22use redis_tower_protocol::helpers::{array, bulk};
23
24#[derive(Clone)]
29pub struct ClusterInfo;
30
31impl ClusterInfo {
32 pub fn new() -> Self {
34 Self
35 }
36}
37
38impl Default for ClusterInfo {
39 fn default() -> Self {
40 Self::new()
41 }
42}
43
44impl Command for ClusterInfo {
45 type Response = String;
46
47 fn to_frame(&self) -> Frame {
48 array(vec![bulk("CLUSTER"), bulk("INFO")])
49 }
50
51 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
52 match frame {
53 Frame::BulkString(Some(data)) => Ok(String::from_utf8_lossy(&data).into_owned()),
54 other => Err(RedisError::UnexpectedResponse {
55 expected: "bulk string",
56 actual: format!("{other:?}"),
57 }),
58 }
59 }
60
61 fn name(&self) -> &str {
62 "CLUSTER INFO"
63 }
64}
65
66#[derive(Clone)]
71pub struct ClusterNodes;
72
73impl ClusterNodes {
74 pub fn new() -> Self {
76 Self
77 }
78}
79
80impl Default for ClusterNodes {
81 fn default() -> Self {
82 Self::new()
83 }
84}
85
86impl Command for ClusterNodes {
87 type Response = String;
88
89 fn to_frame(&self) -> Frame {
90 array(vec![bulk("CLUSTER"), bulk("NODES")])
91 }
92
93 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
94 match frame {
95 Frame::BulkString(Some(data)) => Ok(String::from_utf8_lossy(&data).into_owned()),
96 other => Err(RedisError::UnexpectedResponse {
97 expected: "bulk string",
98 actual: format!("{other:?}"),
99 }),
100 }
101 }
102
103 fn name(&self) -> &str {
104 "CLUSTER NODES"
105 }
106}
107
108#[derive(Clone)]
116pub struct ClusterSlots;
117
118impl ClusterSlots {
119 pub fn new() -> Self {
121 Self
122 }
123}
124
125impl Default for ClusterSlots {
126 fn default() -> Self {
127 Self::new()
128 }
129}
130
131impl Command for ClusterSlots {
132 type Response = Frame;
133
134 fn to_frame(&self) -> Frame {
135 array(vec![bulk("CLUSTER"), bulk("SLOTS")])
136 }
137
138 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
139 match frame {
140 Frame::Array(_) => Ok(frame),
141 other => Err(RedisError::UnexpectedResponse {
142 expected: "array",
143 actual: format!("{other:?}"),
144 }),
145 }
146 }
147
148 fn name(&self) -> &str {
149 "CLUSTER SLOTS"
150 }
151}
152
153#[derive(Clone)]
158pub struct ClusterShards;
159
160impl ClusterShards {
161 pub fn new() -> Self {
163 Self
164 }
165}
166
167impl Default for ClusterShards {
168 fn default() -> Self {
169 Self::new()
170 }
171}
172
173impl Command for ClusterShards {
174 type Response = Frame;
175
176 fn to_frame(&self) -> Frame {
177 array(vec![bulk("CLUSTER"), bulk("SHARDS")])
178 }
179
180 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
181 match frame {
182 Frame::Array(_) | Frame::Map(_) => Ok(frame),
183 other => Err(RedisError::UnexpectedResponse {
184 expected: "array or map",
185 actual: format!("{other:?}"),
186 }),
187 }
188 }
189
190 fn name(&self) -> &str {
191 "CLUSTER SHARDS"
192 }
193}
194
195#[derive(Clone)]
199pub struct ClusterMyId;
200
201impl ClusterMyId {
202 pub fn new() -> Self {
204 Self
205 }
206}
207
208impl Default for ClusterMyId {
209 fn default() -> Self {
210 Self::new()
211 }
212}
213
214impl Command for ClusterMyId {
215 type Response = String;
216
217 fn to_frame(&self) -> Frame {
218 array(vec![bulk("CLUSTER"), bulk("MYID")])
219 }
220
221 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
222 match frame {
223 Frame::BulkString(Some(data)) => Ok(String::from_utf8_lossy(&data).into_owned()),
224 Frame::SimpleString(data) => Ok(String::from_utf8_lossy(&data).into_owned()),
225 other => Err(RedisError::UnexpectedResponse {
226 expected: "bulk string or simple string",
227 actual: format!("{other:?}"),
228 }),
229 }
230 }
231
232 fn name(&self) -> &str {
233 "CLUSTER MYID"
234 }
235}
236
237#[derive(Clone)]
242pub struct ClusterMeet {
243 ip: String,
244 port: u16,
245}
246
247impl ClusterMeet {
248 pub fn new(ip: impl Into<String>, port: u16) -> Self {
250 Self {
251 ip: ip.into(),
252 port,
253 }
254 }
255}
256
257impl Command for ClusterMeet {
258 type Response = ();
259
260 fn to_frame(&self) -> Frame {
261 array(vec![
262 bulk("CLUSTER"),
263 bulk("MEET"),
264 bulk(self.ip.as_str()),
265 bulk(self.port.to_string()),
266 ])
267 }
268
269 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
270 match frame {
271 Frame::SimpleString(s) if &s[..] == b"OK" => Ok(()),
272 other => Err(RedisError::UnexpectedResponse {
273 expected: "OK",
274 actual: format!("{other:?}"),
275 }),
276 }
277 }
278
279 fn name(&self) -> &str {
280 "CLUSTER MEET"
281 }
282}
283
284#[derive(Clone)]
288pub struct ClusterForget {
289 node_id: String,
290}
291
292impl ClusterForget {
293 pub fn new(node_id: impl Into<String>) -> Self {
295 Self {
296 node_id: node_id.into(),
297 }
298 }
299}
300
301impl Command for ClusterForget {
302 type Response = ();
303
304 fn to_frame(&self) -> Frame {
305 array(vec![
306 bulk("CLUSTER"),
307 bulk("FORGET"),
308 bulk(self.node_id.as_str()),
309 ])
310 }
311
312 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
313 match frame {
314 Frame::SimpleString(s) if &s[..] == b"OK" => Ok(()),
315 other => Err(RedisError::UnexpectedResponse {
316 expected: "OK",
317 actual: format!("{other:?}"),
318 }),
319 }
320 }
321
322 fn name(&self) -> &str {
323 "CLUSTER FORGET"
324 }
325}
326
327#[derive(Clone)]
331pub struct ClusterReplicate {
332 node_id: String,
333}
334
335impl ClusterReplicate {
336 pub fn new(node_id: impl Into<String>) -> Self {
338 Self {
339 node_id: node_id.into(),
340 }
341 }
342}
343
344impl Command for ClusterReplicate {
345 type Response = ();
346
347 fn to_frame(&self) -> Frame {
348 array(vec![
349 bulk("CLUSTER"),
350 bulk("REPLICATE"),
351 bulk(self.node_id.as_str()),
352 ])
353 }
354
355 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
356 match frame {
357 Frame::SimpleString(s) if &s[..] == b"OK" => Ok(()),
358 other => Err(RedisError::UnexpectedResponse {
359 expected: "OK",
360 actual: format!("{other:?}"),
361 }),
362 }
363 }
364
365 fn name(&self) -> &str {
366 "CLUSTER REPLICATE"
367 }
368}
369
370#[derive(Clone)]
375pub struct ClusterFailover {
376 option: Option<FailoverOption>,
377}
378
379#[derive(Clone)]
381pub enum FailoverOption {
382 Force,
384 Takeover,
386}
387
388impl ClusterFailover {
389 pub fn new() -> Self {
391 Self { option: None }
392 }
393
394 pub fn force() -> Self {
396 Self {
397 option: Some(FailoverOption::Force),
398 }
399 }
400
401 pub fn takeover() -> Self {
403 Self {
404 option: Some(FailoverOption::Takeover),
405 }
406 }
407}
408
409impl Default for ClusterFailover {
410 fn default() -> Self {
411 Self::new()
412 }
413}
414
415impl Command for ClusterFailover {
416 type Response = ();
417
418 fn to_frame(&self) -> Frame {
419 let mut args = vec![bulk("CLUSTER"), bulk("FAILOVER")];
420 match &self.option {
421 Some(FailoverOption::Force) => args.push(bulk("FORCE")),
422 Some(FailoverOption::Takeover) => args.push(bulk("TAKEOVER")),
423 None => {}
424 }
425 array(args)
426 }
427
428 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
429 match frame {
430 Frame::SimpleString(s) if &s[..] == b"OK" => Ok(()),
431 other => Err(RedisError::UnexpectedResponse {
432 expected: "OK",
433 actual: format!("{other:?}"),
434 }),
435 }
436 }
437
438 fn name(&self) -> &str {
439 "CLUSTER FAILOVER"
440 }
441}
442
443#[derive(Clone)]
448pub struct ClusterReset {
449 hard: bool,
450}
451
452impl ClusterReset {
453 pub fn soft() -> Self {
455 Self { hard: false }
456 }
457
458 pub fn hard() -> Self {
460 Self { hard: true }
461 }
462}
463
464impl Default for ClusterReset {
465 fn default() -> Self {
466 Self::soft()
467 }
468}
469
470impl Command for ClusterReset {
471 type Response = ();
472
473 fn to_frame(&self) -> Frame {
474 let mut args = vec![bulk("CLUSTER"), bulk("RESET")];
475 if self.hard {
476 args.push(bulk("HARD"));
477 } else {
478 args.push(bulk("SOFT"));
479 }
480 array(args)
481 }
482
483 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
484 match frame {
485 Frame::SimpleString(s) if &s[..] == b"OK" => Ok(()),
486 other => Err(RedisError::UnexpectedResponse {
487 expected: "OK",
488 actual: format!("{other:?}"),
489 }),
490 }
491 }
492
493 fn name(&self) -> &str {
494 "CLUSTER RESET"
495 }
496}
497
498#[derive(Clone)]
502pub struct ClusterCountKeysInSlot {
503 slot: u16,
504}
505
506impl ClusterCountKeysInSlot {
507 pub fn new(slot: u16) -> Self {
509 Self { slot }
510 }
511}
512
513impl Command for ClusterCountKeysInSlot {
514 type Response = i64;
515
516 fn to_frame(&self) -> Frame {
517 array(vec![
518 bulk("CLUSTER"),
519 bulk("COUNTKEYSINSLOT"),
520 bulk(self.slot.to_string()),
521 ])
522 }
523
524 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
525 match frame {
526 Frame::Integer(n) => Ok(n),
527 other => Err(RedisError::UnexpectedResponse {
528 expected: "integer",
529 actual: format!("{other:?}"),
530 }),
531 }
532 }
533
534 fn name(&self) -> &str {
535 "CLUSTER COUNTKEYSINSLOT"
536 }
537}
538
539#[derive(Clone)]
543pub struct ClusterGetKeysInSlot {
544 slot: u16,
545 count: u32,
546}
547
548impl ClusterGetKeysInSlot {
549 pub fn new(slot: u16, count: u32) -> Self {
551 Self { slot, count }
552 }
553}
554
555impl Command for ClusterGetKeysInSlot {
556 type Response = Vec<Bytes>;
557
558 fn to_frame(&self) -> Frame {
559 array(vec![
560 bulk("CLUSTER"),
561 bulk("GETKEYSINSLOT"),
562 bulk(self.slot.to_string()),
563 bulk(self.count.to_string()),
564 ])
565 }
566
567 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
568 match frame {
569 Frame::Array(Some(frames)) => {
570 let mut keys = Vec::with_capacity(frames.len());
571 for f in frames {
572 match f {
573 Frame::BulkString(Some(data)) => keys.push(data),
574 other => {
575 return Err(RedisError::UnexpectedResponse {
576 expected: "bulk string",
577 actual: format!("{other:?}"),
578 });
579 }
580 }
581 }
582 Ok(keys)
583 }
584 Frame::Array(None) => Ok(Vec::new()),
585 other => Err(RedisError::UnexpectedResponse {
586 expected: "array",
587 actual: format!("{other:?}"),
588 }),
589 }
590 }
591
592 fn name(&self) -> &str {
593 "CLUSTER GETKEYSINSLOT"
594 }
595}
596
597#[derive(Clone)]
601pub struct ClusterKeySlot {
602 key: CommandArg,
603}
604
605impl ClusterKeySlot {
606 pub fn new(key: impl Into<CommandArg>) -> Self {
608 Self { key: key.into() }
609 }
610}
611
612impl Command for ClusterKeySlot {
613 type Response = i64;
614
615 fn to_frame(&self) -> Frame {
616 array(vec![bulk("CLUSTER"), bulk("KEYSLOT"), bulk(&self.key)])
617 }
618
619 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
620 match frame {
621 Frame::Integer(n) => Ok(n),
622 other => Err(RedisError::UnexpectedResponse {
623 expected: "integer",
624 actual: format!("{other:?}"),
625 }),
626 }
627 }
628
629 fn name(&self) -> &str {
630 "CLUSTER KEYSLOT"
631 }
632}
633
634#[derive(Clone)]
638pub struct ClusterHelp;
639
640impl ClusterHelp {
641 pub fn new() -> Self {
643 Self
644 }
645}
646
647impl Default for ClusterHelp {
648 fn default() -> Self {
649 Self::new()
650 }
651}
652
653impl Command for ClusterHelp {
654 type Response = Vec<Bytes>;
655
656 fn to_frame(&self) -> Frame {
657 array(vec![bulk("CLUSTER"), bulk("HELP")])
658 }
659
660 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
661 crate::help::parse_help_lines(frame)
662 }
663
664 fn name(&self) -> &str {
665 "CLUSTER HELP"
666 }
667
668 fn idempotent(&self) -> bool {
669 true
670 }
671}
672
673#[derive(Clone)]
675pub enum SetSlotState {
676 Importing(String),
678 Migrating(String),
680 Stable,
682 Node(String),
684}
685
686#[derive(Clone)]
691pub struct ClusterSetSlot {
692 slot: u16,
693 state: SetSlotState,
694}
695
696impl ClusterSetSlot {
697 pub fn new(slot: u16, state: SetSlotState) -> Self {
699 Self { slot, state }
700 }
701}
702
703impl Command for ClusterSetSlot {
704 type Response = ();
705
706 fn to_frame(&self) -> Frame {
707 let mut args = vec![
708 bulk("CLUSTER"),
709 bulk("SETSLOT"),
710 bulk(self.slot.to_string()),
711 ];
712 match &self.state {
713 SetSlotState::Importing(id) => {
714 args.push(bulk("IMPORTING"));
715 args.push(bulk(id.as_str()));
716 }
717 SetSlotState::Migrating(id) => {
718 args.push(bulk("MIGRATING"));
719 args.push(bulk(id.as_str()));
720 }
721 SetSlotState::Stable => args.push(bulk("STABLE")),
722 SetSlotState::Node(id) => {
723 args.push(bulk("NODE"));
724 args.push(bulk(id.as_str()));
725 }
726 }
727 array(args)
728 }
729
730 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
731 parse_ok(frame)
732 }
733
734 fn name(&self) -> &str {
735 "CLUSTER SETSLOT"
736 }
737}
738
739#[derive(Clone)]
743pub struct ClusterAddSlots {
744 slots: Vec<u16>,
745}
746
747impl ClusterAddSlots {
748 pub fn new(slots: impl IntoIterator<Item = u16>) -> Self {
750 Self {
751 slots: slots.into_iter().collect(),
752 }
753 }
754}
755
756impl Command for ClusterAddSlots {
757 type Response = ();
758
759 fn to_frame(&self) -> Frame {
760 let mut args = vec![bulk("CLUSTER"), bulk("ADDSLOTS")];
761 args.extend(self.slots.iter().map(|s| bulk(s.to_string())));
762 array(args)
763 }
764
765 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
766 parse_ok(frame)
767 }
768
769 fn name(&self) -> &str {
770 "CLUSTER ADDSLOTS"
771 }
772}
773
774#[derive(Clone)]
778pub struct ClusterDelSlots {
779 slots: Vec<u16>,
780}
781
782impl ClusterDelSlots {
783 pub fn new(slots: impl IntoIterator<Item = u16>) -> Self {
785 Self {
786 slots: slots.into_iter().collect(),
787 }
788 }
789}
790
791impl Command for ClusterDelSlots {
792 type Response = ();
793
794 fn to_frame(&self) -> Frame {
795 let mut args = vec![bulk("CLUSTER"), bulk("DELSLOTS")];
796 args.extend(self.slots.iter().map(|s| bulk(s.to_string())));
797 array(args)
798 }
799
800 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
801 parse_ok(frame)
802 }
803
804 fn name(&self) -> &str {
805 "CLUSTER DELSLOTS"
806 }
807}
808
809#[derive(Clone)]
813pub struct ClusterAddSlotsRange {
814 ranges: Vec<(u16, u16)>,
815}
816
817impl ClusterAddSlotsRange {
818 pub fn new(ranges: impl IntoIterator<Item = (u16, u16)>) -> Self {
820 Self {
821 ranges: ranges.into_iter().collect(),
822 }
823 }
824}
825
826impl Command for ClusterAddSlotsRange {
827 type Response = ();
828
829 fn to_frame(&self) -> Frame {
830 let mut args = vec![bulk("CLUSTER"), bulk("ADDSLOTSRANGE")];
831 for (start, end) in &self.ranges {
832 args.push(bulk(start.to_string()));
833 args.push(bulk(end.to_string()));
834 }
835 array(args)
836 }
837
838 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
839 parse_ok(frame)
840 }
841
842 fn name(&self) -> &str {
843 "CLUSTER ADDSLOTSRANGE"
844 }
845}
846
847#[derive(Clone)]
851pub struct ClusterDelSlotsRange {
852 ranges: Vec<(u16, u16)>,
853}
854
855impl ClusterDelSlotsRange {
856 pub fn new(ranges: impl IntoIterator<Item = (u16, u16)>) -> Self {
858 Self {
859 ranges: ranges.into_iter().collect(),
860 }
861 }
862}
863
864impl Command for ClusterDelSlotsRange {
865 type Response = ();
866
867 fn to_frame(&self) -> Frame {
868 let mut args = vec![bulk("CLUSTER"), bulk("DELSLOTSRANGE")];
869 for (start, end) in &self.ranges {
870 args.push(bulk(start.to_string()));
871 args.push(bulk(end.to_string()));
872 }
873 array(args)
874 }
875
876 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
877 parse_ok(frame)
878 }
879
880 fn name(&self) -> &str {
881 "CLUSTER DELSLOTSRANGE"
882 }
883}
884
885#[derive(Clone)]
890pub struct ClusterReplicas {
891 node_id: String,
892}
893
894impl ClusterReplicas {
895 pub fn new(node_id: impl Into<String>) -> Self {
897 Self {
898 node_id: node_id.into(),
899 }
900 }
901}
902
903impl Command for ClusterReplicas {
904 type Response = Vec<String>;
905
906 fn to_frame(&self) -> Frame {
907 array(vec![
908 bulk("CLUSTER"),
909 bulk("REPLICAS"),
910 bulk(self.node_id.as_str()),
911 ])
912 }
913
914 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
915 parse_string_array(frame)
916 }
917
918 fn name(&self) -> &str {
919 "CLUSTER REPLICAS"
920 }
921
922 fn idempotent(&self) -> bool {
923 true
924 }
925}
926
927#[derive(Clone)]
932pub struct ClusterSlaves {
933 node_id: String,
934}
935
936impl ClusterSlaves {
937 pub fn new(node_id: impl Into<String>) -> Self {
939 Self {
940 node_id: node_id.into(),
941 }
942 }
943}
944
945impl Command for ClusterSlaves {
946 type Response = Vec<String>;
947
948 fn to_frame(&self) -> Frame {
949 array(vec![
950 bulk("CLUSTER"),
951 bulk("SLAVES"),
952 bulk(self.node_id.as_str()),
953 ])
954 }
955
956 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
957 parse_string_array(frame)
958 }
959
960 fn name(&self) -> &str {
961 "CLUSTER SLAVES"
962 }
963
964 fn idempotent(&self) -> bool {
965 true
966 }
967}
968
969#[derive(Clone)]
975pub struct ClusterLinks;
976
977impl ClusterLinks {
978 pub fn new() -> Self {
980 Self
981 }
982}
983
984impl Default for ClusterLinks {
985 fn default() -> Self {
986 Self::new()
987 }
988}
989
990impl Command for ClusterLinks {
991 type Response = Frame;
992
993 fn to_frame(&self) -> Frame {
994 array(vec![bulk("CLUSTER"), bulk("LINKS")])
995 }
996
997 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
998 match frame {
999 Frame::Array(_) => Ok(frame),
1000 other => Err(RedisError::UnexpectedResponse {
1001 expected: "array",
1002 actual: format!("{other:?}"),
1003 }),
1004 }
1005 }
1006
1007 fn name(&self) -> &str {
1008 "CLUSTER LINKS"
1009 }
1010
1011 fn idempotent(&self) -> bool {
1012 true
1013 }
1014}
1015
1016#[derive(Clone)]
1021pub struct ClusterSetConfigEpoch {
1022 epoch: u64,
1023}
1024
1025impl ClusterSetConfigEpoch {
1026 pub fn new(epoch: u64) -> Self {
1028 Self { epoch }
1029 }
1030}
1031
1032impl Command for ClusterSetConfigEpoch {
1033 type Response = ();
1034
1035 fn to_frame(&self) -> Frame {
1036 array(vec![
1037 bulk("CLUSTER"),
1038 bulk("SET-CONFIG-EPOCH"),
1039 bulk(self.epoch.to_string()),
1040 ])
1041 }
1042
1043 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
1044 parse_ok(frame)
1045 }
1046
1047 fn name(&self) -> &str {
1048 "CLUSTER SET-CONFIG-EPOCH"
1049 }
1050}
1051
1052#[derive(Clone)]
1057pub struct ClusterBumpEpoch;
1058
1059impl ClusterBumpEpoch {
1060 pub fn new() -> Self {
1062 Self
1063 }
1064}
1065
1066impl Default for ClusterBumpEpoch {
1067 fn default() -> Self {
1068 Self::new()
1069 }
1070}
1071
1072impl Command for ClusterBumpEpoch {
1073 type Response = String;
1074
1075 fn to_frame(&self) -> Frame {
1076 array(vec![bulk("CLUSTER"), bulk("BUMPEPOCH")])
1077 }
1078
1079 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
1080 match frame {
1081 Frame::SimpleString(s) => Ok(String::from_utf8_lossy(&s).into_owned()),
1082 Frame::BulkString(Some(data)) => Ok(String::from_utf8_lossy(&data).into_owned()),
1083 other => Err(RedisError::UnexpectedResponse {
1084 expected: "status string",
1085 actual: format!("{other:?}"),
1086 }),
1087 }
1088 }
1089
1090 fn name(&self) -> &str {
1091 "CLUSTER BUMPEPOCH"
1092 }
1093}
1094
1095#[derive(Clone)]
1100pub struct ClusterFlushSlots;
1101
1102impl ClusterFlushSlots {
1103 pub fn new() -> Self {
1105 Self
1106 }
1107}
1108
1109impl Default for ClusterFlushSlots {
1110 fn default() -> Self {
1111 Self::new()
1112 }
1113}
1114
1115impl Command for ClusterFlushSlots {
1116 type Response = ();
1117
1118 fn to_frame(&self) -> Frame {
1119 array(vec![bulk("CLUSTER"), bulk("FLUSHSLOTS")])
1120 }
1121
1122 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
1123 parse_ok(frame)
1124 }
1125
1126 fn name(&self) -> &str {
1127 "CLUSTER FLUSHSLOTS"
1128 }
1129}
1130
1131#[derive(Clone)]
1135pub struct ClusterSaveConfig;
1136
1137impl ClusterSaveConfig {
1138 pub fn new() -> Self {
1140 Self
1141 }
1142}
1143
1144impl Default for ClusterSaveConfig {
1145 fn default() -> Self {
1146 Self::new()
1147 }
1148}
1149
1150impl Command for ClusterSaveConfig {
1151 type Response = ();
1152
1153 fn to_frame(&self) -> Frame {
1154 array(vec![bulk("CLUSTER"), bulk("SAVECONFIG")])
1155 }
1156
1157 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
1158 parse_ok(frame)
1159 }
1160
1161 fn name(&self) -> &str {
1162 "CLUSTER SAVECONFIG"
1163 }
1164}
1165
1166fn parse_ok(frame: Frame) -> Result<(), RedisError> {
1169 match frame {
1170 Frame::SimpleString(s) if &s[..] == b"OK" => Ok(()),
1171 Frame::BulkString(Some(data)) if &data[..] == b"OK" => Ok(()),
1172 other => Err(RedisError::UnexpectedResponse {
1173 expected: "OK",
1174 actual: format!("{other:?}"),
1175 }),
1176 }
1177}
1178
1179fn parse_string_array(frame: Frame) -> Result<Vec<String>, RedisError> {
1181 match frame {
1182 Frame::Array(Some(frames)) => {
1183 let mut out = Vec::with_capacity(frames.len());
1184 for f in frames {
1185 match f {
1186 Frame::BulkString(Some(data)) => {
1187 out.push(String::from_utf8_lossy(&data).into_owned())
1188 }
1189 Frame::SimpleString(s) => out.push(String::from_utf8_lossy(&s).into_owned()),
1190 other => {
1191 return Err(RedisError::UnexpectedResponse {
1192 expected: "bulk string",
1193 actual: format!("{other:?}"),
1194 });
1195 }
1196 }
1197 }
1198 Ok(out)
1199 }
1200 Frame::Array(None) => Ok(Vec::new()),
1201 other => Err(RedisError::UnexpectedResponse {
1202 expected: "array",
1203 actual: format!("{other:?}"),
1204 }),
1205 }
1206}
1207
1208#[cfg(test)]
1209mod tests {
1210 use super::*;
1211
1212 #[test]
1213 fn cluster_info_frame() {
1214 let cmd = ClusterInfo::new();
1215 let frame = cmd.to_frame();
1216 assert_eq!(frame, array(vec![bulk("CLUSTER"), bulk("INFO")]));
1217 assert_eq!(cmd.name(), "CLUSTER INFO");
1218 }
1219
1220 #[test]
1221 fn cluster_info_parse_response() {
1222 let cmd = ClusterInfo::new();
1223 let frame = Frame::BulkString(Some(Bytes::from("cluster_enabled:0\r\n")));
1224 let resp = cmd.parse_response(frame).unwrap();
1225 assert!(resp.contains("cluster_enabled:0"));
1226 }
1227
1228 #[test]
1229 fn cluster_nodes_frame() {
1230 let cmd = ClusterNodes::new();
1231 let frame = cmd.to_frame();
1232 assert_eq!(frame, array(vec![bulk("CLUSTER"), bulk("NODES")]));
1233 assert_eq!(cmd.name(), "CLUSTER NODES");
1234 }
1235
1236 #[test]
1237 fn cluster_slots_frame() {
1238 let cmd = ClusterSlots::new();
1239 let frame = cmd.to_frame();
1240 assert_eq!(frame, array(vec![bulk("CLUSTER"), bulk("SLOTS")]));
1241 assert_eq!(cmd.name(), "CLUSTER SLOTS");
1242 }
1243
1244 #[test]
1245 fn cluster_shards_frame() {
1246 let cmd = ClusterShards::new();
1247 let frame = cmd.to_frame();
1248 assert_eq!(frame, array(vec![bulk("CLUSTER"), bulk("SHARDS")]));
1249 assert_eq!(cmd.name(), "CLUSTER SHARDS");
1250 }
1251
1252 #[test]
1253 fn cluster_myid_frame() {
1254 let cmd = ClusterMyId::new();
1255 let frame = cmd.to_frame();
1256 assert_eq!(frame, array(vec![bulk("CLUSTER"), bulk("MYID")]));
1257 assert_eq!(cmd.name(), "CLUSTER MYID");
1258 }
1259
1260 #[test]
1261 fn cluster_myid_parse_response() {
1262 let cmd = ClusterMyId::new();
1263 let frame = Frame::BulkString(Some(Bytes::from(
1264 "e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca",
1265 )));
1266 let resp = cmd.parse_response(frame).unwrap();
1267 assert_eq!(resp, "e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca");
1268 }
1269
1270 #[test]
1271 fn cluster_meet_frame() {
1272 let cmd = ClusterMeet::new("127.0.0.1", 7001);
1273 let frame = cmd.to_frame();
1274 assert_eq!(
1275 frame,
1276 array(vec![
1277 bulk("CLUSTER"),
1278 bulk("MEET"),
1279 bulk("127.0.0.1"),
1280 bulk("7001"),
1281 ])
1282 );
1283 assert_eq!(cmd.name(), "CLUSTER MEET");
1284 }
1285
1286 #[test]
1287 fn cluster_meet_parse_ok() {
1288 let cmd = ClusterMeet::new("127.0.0.1", 7001);
1289 let frame = Frame::SimpleString(Bytes::from("OK"));
1290 cmd.parse_response(frame).unwrap();
1291 }
1292
1293 #[test]
1294 fn cluster_forget_frame() {
1295 let cmd = ClusterForget::new("e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca");
1296 let frame = cmd.to_frame();
1297 assert_eq!(
1298 frame,
1299 array(vec![
1300 bulk("CLUSTER"),
1301 bulk("FORGET"),
1302 bulk("e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca"),
1303 ])
1304 );
1305 assert_eq!(cmd.name(), "CLUSTER FORGET");
1306 }
1307
1308 #[test]
1309 fn cluster_replicate_frame() {
1310 let cmd = ClusterReplicate::new("e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca");
1311 let frame = cmd.to_frame();
1312 assert_eq!(
1313 frame,
1314 array(vec![
1315 bulk("CLUSTER"),
1316 bulk("REPLICATE"),
1317 bulk("e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca"),
1318 ])
1319 );
1320 assert_eq!(cmd.name(), "CLUSTER REPLICATE");
1321 }
1322
1323 #[test]
1324 fn cluster_failover_frame() {
1325 let cmd = ClusterFailover::new();
1326 assert_eq!(
1327 cmd.to_frame(),
1328 array(vec![bulk("CLUSTER"), bulk("FAILOVER")])
1329 );
1330 assert_eq!(cmd.name(), "CLUSTER FAILOVER");
1331 }
1332
1333 #[test]
1334 fn cluster_failover_force_frame() {
1335 let cmd = ClusterFailover::force();
1336 assert_eq!(
1337 cmd.to_frame(),
1338 array(vec![bulk("CLUSTER"), bulk("FAILOVER"), bulk("FORCE")])
1339 );
1340 }
1341
1342 #[test]
1343 fn cluster_failover_takeover_frame() {
1344 let cmd = ClusterFailover::takeover();
1345 assert_eq!(
1346 cmd.to_frame(),
1347 array(vec![bulk("CLUSTER"), bulk("FAILOVER"), bulk("TAKEOVER")])
1348 );
1349 }
1350
1351 #[test]
1352 fn cluster_reset_soft_frame() {
1353 let cmd = ClusterReset::soft();
1354 assert_eq!(
1355 cmd.to_frame(),
1356 array(vec![bulk("CLUSTER"), bulk("RESET"), bulk("SOFT")])
1357 );
1358 assert_eq!(cmd.name(), "CLUSTER RESET");
1359 }
1360
1361 #[test]
1362 fn cluster_reset_hard_frame() {
1363 let cmd = ClusterReset::hard();
1364 assert_eq!(
1365 cmd.to_frame(),
1366 array(vec![bulk("CLUSTER"), bulk("RESET"), bulk("HARD")])
1367 );
1368 }
1369
1370 #[test]
1371 fn cluster_count_keys_in_slot_frame() {
1372 let cmd = ClusterCountKeysInSlot::new(7000);
1373 let frame = cmd.to_frame();
1374 assert_eq!(
1375 frame,
1376 array(vec![bulk("CLUSTER"), bulk("COUNTKEYSINSLOT"), bulk("7000"),])
1377 );
1378 assert_eq!(cmd.name(), "CLUSTER COUNTKEYSINSLOT");
1379 }
1380
1381 #[test]
1382 fn cluster_count_keys_in_slot_parse_response() {
1383 let cmd = ClusterCountKeysInSlot::new(7000);
1384 let frame = Frame::Integer(42);
1385 let resp = cmd.parse_response(frame).unwrap();
1386 assert_eq!(resp, 42);
1387 }
1388
1389 #[test]
1390 fn cluster_get_keys_in_slot_frame() {
1391 let cmd = ClusterGetKeysInSlot::new(7000, 10);
1392 let frame = cmd.to_frame();
1393 assert_eq!(
1394 frame,
1395 array(vec![
1396 bulk("CLUSTER"),
1397 bulk("GETKEYSINSLOT"),
1398 bulk("7000"),
1399 bulk("10"),
1400 ])
1401 );
1402 assert_eq!(cmd.name(), "CLUSTER GETKEYSINSLOT");
1403 }
1404
1405 #[test]
1406 fn cluster_get_keys_in_slot_parse_response() {
1407 let cmd = ClusterGetKeysInSlot::new(7000, 10);
1408 let frame = Frame::Array(Some(vec![
1409 Frame::BulkString(Some(Bytes::from("key1"))),
1410 Frame::BulkString(Some(Bytes::from("key2"))),
1411 ]));
1412 let resp = cmd.parse_response(frame).unwrap();
1413 assert_eq!(resp, vec![Bytes::from("key1"), Bytes::from("key2")]);
1414 }
1415
1416 #[test]
1417 fn cluster_get_keys_in_slot_parse_empty() {
1418 let cmd = ClusterGetKeysInSlot::new(7000, 10);
1419 let frame = Frame::Array(Some(vec![]));
1420 let resp = cmd.parse_response(frame).unwrap();
1421 assert!(resp.is_empty());
1422 }
1423
1424 #[test]
1425 fn cluster_keyslot_frame() {
1426 let cmd = ClusterKeySlot::new("mykey");
1427 let frame = cmd.to_frame();
1428 assert_eq!(
1429 frame,
1430 array(vec![bulk("CLUSTER"), bulk("KEYSLOT"), bulk("mykey")])
1431 );
1432 assert_eq!(cmd.name(), "CLUSTER KEYSLOT");
1433 }
1434
1435 #[test]
1436 fn cluster_keyslot_parse_response() {
1437 let cmd = ClusterKeySlot::new("mykey");
1438 let frame = Frame::Integer(14687);
1439 let resp = cmd.parse_response(frame).unwrap();
1440 assert_eq!(resp, 14687);
1441 }
1442
1443 #[test]
1444 fn cluster_help_to_frame() {
1445 let cmd = ClusterHelp::new();
1446 assert_eq!(cmd.to_frame(), array(vec![bulk("CLUSTER"), bulk("HELP")]));
1447 assert!(cmd.idempotent());
1448 }
1449
1450 #[test]
1451 fn cluster_help_parse_lines() {
1452 let cmd = ClusterHelp::new();
1453 let reply = array(vec![bulk("CLUSTER <subcommand>"), bulk("INFO")]);
1454 let lines = cmd.parse_response(reply).unwrap();
1455 assert_eq!(lines.len(), 2);
1456 assert_eq!(&lines[1][..], b"INFO");
1457 }
1458
1459 #[test]
1460 fn cluster_setslot_variants_frame() {
1461 assert_eq!(
1462 ClusterSetSlot::new(42, SetSlotState::Stable).to_frame(),
1463 array(vec![
1464 bulk("CLUSTER"),
1465 bulk("SETSLOT"),
1466 bulk("42"),
1467 bulk("STABLE")
1468 ])
1469 );
1470 assert_eq!(
1471 ClusterSetSlot::new(42, SetSlotState::Importing("node-a".into())).to_frame(),
1472 array(vec![
1473 bulk("CLUSTER"),
1474 bulk("SETSLOT"),
1475 bulk("42"),
1476 bulk("IMPORTING"),
1477 bulk("node-a"),
1478 ])
1479 );
1480 assert_eq!(
1481 ClusterSetSlot::new(42, SetSlotState::Migrating("node-b".into())).to_frame(),
1482 array(vec![
1483 bulk("CLUSTER"),
1484 bulk("SETSLOT"),
1485 bulk("42"),
1486 bulk("MIGRATING"),
1487 bulk("node-b"),
1488 ])
1489 );
1490 let node = ClusterSetSlot::new(42, SetSlotState::Node("node-c".into()));
1491 assert_eq!(
1492 node.to_frame(),
1493 array(vec![
1494 bulk("CLUSTER"),
1495 bulk("SETSLOT"),
1496 bulk("42"),
1497 bulk("NODE"),
1498 bulk("node-c"),
1499 ])
1500 );
1501 assert_eq!(node.name(), "CLUSTER SETSLOT");
1502 assert!(!node.idempotent());
1503 assert!(
1504 node.parse_response(Frame::SimpleString(Bytes::from("OK")))
1505 .is_ok()
1506 );
1507 }
1508
1509 #[test]
1510 fn cluster_addslots_delslots_frame() {
1511 assert_eq!(
1512 ClusterAddSlots::new([1, 2, 3]).to_frame(),
1513 array(vec![
1514 bulk("CLUSTER"),
1515 bulk("ADDSLOTS"),
1516 bulk("1"),
1517 bulk("2"),
1518 bulk("3"),
1519 ])
1520 );
1521 assert_eq!(
1522 ClusterDelSlots::new([7]).to_frame(),
1523 array(vec![bulk("CLUSTER"), bulk("DELSLOTS"), bulk("7")])
1524 );
1525 }
1526
1527 #[test]
1528 fn cluster_slots_range_frame() {
1529 assert_eq!(
1530 ClusterAddSlotsRange::new([(0, 100), (200, 300)]).to_frame(),
1531 array(vec![
1532 bulk("CLUSTER"),
1533 bulk("ADDSLOTSRANGE"),
1534 bulk("0"),
1535 bulk("100"),
1536 bulk("200"),
1537 bulk("300"),
1538 ])
1539 );
1540 assert_eq!(
1541 ClusterDelSlotsRange::new([(0, 16383)]).to_frame(),
1542 array(vec![
1543 bulk("CLUSTER"),
1544 bulk("DELSLOTSRANGE"),
1545 bulk("0"),
1546 bulk("16383"),
1547 ])
1548 );
1549 }
1550
1551 #[test]
1552 fn cluster_replicas_frame_and_parse() {
1553 let cmd = ClusterReplicas::new("master-id");
1554 assert_eq!(
1555 cmd.to_frame(),
1556 array(vec![bulk("CLUSTER"), bulk("REPLICAS"), bulk("master-id")])
1557 );
1558 assert!(cmd.idempotent());
1559 let reply = Frame::Array(Some(vec![
1560 Frame::BulkString(Some(Bytes::from("id1 1.2.3.4:6379@16379 slave ..."))),
1561 Frame::BulkString(Some(Bytes::from("id2 1.2.3.5:6379@16379 slave ..."))),
1562 ]));
1563 let out = cmd.parse_response(reply).unwrap();
1564 assert_eq!(out.len(), 2);
1565 assert!(out[0].starts_with("id1"));
1566 }
1567
1568 #[test]
1569 fn cluster_slaves_is_replicas_alias() {
1570 let cmd = ClusterSlaves::new("master-id");
1571 assert_eq!(
1572 cmd.to_frame(),
1573 array(vec![bulk("CLUSTER"), bulk("SLAVES"), bulk("master-id")])
1574 );
1575 assert!(cmd.idempotent());
1576 assert!(cmd.parse_response(Frame::Array(None)).unwrap().is_empty());
1577 }
1578
1579 #[test]
1580 fn cluster_links_frame_and_parse() {
1581 let cmd = ClusterLinks::new();
1582 assert_eq!(cmd.to_frame(), array(vec![bulk("CLUSTER"), bulk("LINKS")]));
1583 assert!(cmd.idempotent());
1584 let reply = Frame::Array(Some(vec![]));
1585 assert!(matches!(
1586 cmd.parse_response(reply).unwrap(),
1587 Frame::Array(_)
1588 ));
1589 }
1590
1591 #[test]
1592 fn cluster_epoch_commands_frame() {
1593 assert_eq!(
1594 ClusterSetConfigEpoch::new(5).to_frame(),
1595 array(vec![bulk("CLUSTER"), bulk("SET-CONFIG-EPOCH"), bulk("5"),])
1596 );
1597 let bump = ClusterBumpEpoch::new();
1598 assert_eq!(
1599 bump.to_frame(),
1600 array(vec![bulk("CLUSTER"), bulk("BUMPEPOCH")])
1601 );
1602 let resp = bump
1603 .parse_response(Frame::SimpleString(Bytes::from("BUMPED 6")))
1604 .unwrap();
1605 assert_eq!(resp, "BUMPED 6");
1606 }
1607
1608 #[test]
1609 fn cluster_flushslots_saveconfig_frame() {
1610 assert_eq!(
1611 ClusterFlushSlots::new().to_frame(),
1612 array(vec![bulk("CLUSTER"), bulk("FLUSHSLOTS")])
1613 );
1614 assert_eq!(
1615 ClusterSaveConfig::new().to_frame(),
1616 array(vec![bulk("CLUSTER"), bulk("SAVECONFIG")])
1617 );
1618 assert!(
1619 ClusterSaveConfig::new()
1620 .parse_response(Frame::SimpleString(Bytes::from("OK")))
1621 .is_ok()
1622 );
1623 }
1624
1625 #[test]
1626 fn parse_ok_accepts_bulk_and_simple() {
1627 assert!(parse_ok(Frame::SimpleString(Bytes::from("OK"))).is_ok());
1628 assert!(parse_ok(Frame::BulkString(Some(Bytes::from("OK")))).is_ok());
1629 assert!(parse_ok(Frame::Integer(1)).is_err());
1630 }
1631}