1use bytes::Bytes;
22use redis_tower_core::{Command, Frame, RedisError};
23use redis_tower_protocol::helpers::{array, bulk};
24
25use crate::CommandArg;
26
27#[derive(Clone)]
32pub struct SAdd {
33 key: CommandArg,
34 members: Vec<CommandArg>,
35}
36
37impl SAdd {
38 pub fn new(key: impl Into<CommandArg>, member: impl Into<CommandArg>) -> Self {
40 Self {
41 key: key.into(),
42 members: vec![member.into()],
43 }
44 }
45
46 pub fn members(
48 key: impl Into<CommandArg>,
49 members: impl IntoIterator<Item = impl Into<CommandArg>>,
50 ) -> Self {
51 Self {
52 key: key.into(),
53 members: members.into_iter().map(Into::into).collect(),
54 }
55 }
56}
57
58impl Command for SAdd {
59 type Response = i64;
60
61 fn to_frame(&self) -> Frame {
62 let mut args = vec![bulk("SADD"), bulk(self.key.as_bytes())];
63 for member in &self.members {
64 args.push(bulk(member.as_bytes()));
65 }
66 array(args)
67 }
68
69 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
70 match frame {
71 Frame::Integer(n) => Ok(n),
72 other => Err(RedisError::UnexpectedResponse {
73 expected: "integer",
74 actual: format!("{other:?}"),
75 }),
76 }
77 }
78
79 fn name(&self) -> &str {
80 "SADD"
81 }
82}
83
84#[derive(Clone)]
89pub struct SRem {
90 key: CommandArg,
91 members: Vec<CommandArg>,
92}
93
94impl SRem {
95 pub fn new(key: impl Into<CommandArg>, member: impl Into<CommandArg>) -> Self {
97 Self {
98 key: key.into(),
99 members: vec![member.into()],
100 }
101 }
102
103 pub fn members(
105 key: impl Into<CommandArg>,
106 members: impl IntoIterator<Item = impl Into<CommandArg>>,
107 ) -> Self {
108 Self {
109 key: key.into(),
110 members: members.into_iter().map(Into::into).collect(),
111 }
112 }
113}
114
115impl Command for SRem {
116 type Response = i64;
117
118 fn to_frame(&self) -> Frame {
119 let mut args = vec![bulk("SREM"), bulk(self.key.as_bytes())];
120 for member in &self.members {
121 args.push(bulk(member.as_bytes()));
122 }
123 array(args)
124 }
125
126 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
127 match frame {
128 Frame::Integer(n) => Ok(n),
129 other => Err(RedisError::UnexpectedResponse {
130 expected: "integer",
131 actual: format!("{other:?}"),
132 }),
133 }
134 }
135
136 fn name(&self) -> &str {
137 "SREM"
138 }
139}
140
141#[derive(Clone)]
145pub struct SMembers {
146 key: CommandArg,
147}
148
149impl SMembers {
150 pub fn new(key: impl Into<CommandArg>) -> Self {
152 Self { key: key.into() }
153 }
154}
155
156impl Command for SMembers {
157 type Response = Vec<Bytes>;
158
159 fn to_frame(&self) -> Frame {
160 array(vec![bulk("SMEMBERS"), bulk(self.key.as_bytes())])
161 }
162
163 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
164 match frame {
165 Frame::Array(Some(frames)) => frames
166 .into_iter()
167 .map(|f| match f {
168 Frame::BulkString(Some(data)) => Ok(data),
169 other => Err(RedisError::UnexpectedResponse {
170 expected: "bulk string",
171 actual: format!("{other:?}"),
172 }),
173 })
174 .collect(),
175 Frame::Set(items) => items
176 .into_iter()
177 .map(|f| match f {
178 Frame::BulkString(Some(data)) => Ok(data),
179 other => Err(RedisError::UnexpectedResponse {
180 expected: "bulk string",
181 actual: format!("{other:?}"),
182 }),
183 })
184 .collect(),
185 other => Err(RedisError::UnexpectedResponse {
186 expected: "array or set",
187 actual: format!("{other:?}"),
188 }),
189 }
190 }
191
192 fn name(&self) -> &str {
193 "SMEMBERS"
194 }
195
196 fn idempotent(&self) -> bool {
197 true
198 }
199}
200
201#[derive(Clone)]
205pub struct SIsMember {
206 key: CommandArg,
207 member: CommandArg,
208}
209
210impl SIsMember {
211 pub fn new(key: impl Into<CommandArg>, member: impl Into<CommandArg>) -> Self {
213 Self {
214 key: key.into(),
215 member: member.into(),
216 }
217 }
218}
219
220impl Command for SIsMember {
221 type Response = bool;
222
223 fn to_frame(&self) -> Frame {
224 array(vec![
225 bulk("SISMEMBER"),
226 bulk(self.key.as_bytes()),
227 bulk(self.member.as_bytes()),
228 ])
229 }
230
231 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
232 match frame {
233 Frame::Integer(n) => Ok(n == 1),
234 Frame::Boolean(b) => Ok(b),
235 other => Err(RedisError::UnexpectedResponse {
236 expected: "integer or boolean",
237 actual: format!("{other:?}"),
238 }),
239 }
240 }
241
242 fn name(&self) -> &str {
243 "SISMEMBER"
244 }
245
246 fn idempotent(&self) -> bool {
247 true
248 }
249}
250
251#[derive(Clone)]
255pub struct SCard {
256 key: CommandArg,
257}
258
259impl SCard {
260 pub fn new(key: impl Into<CommandArg>) -> Self {
262 Self { key: key.into() }
263 }
264}
265
266impl Command for SCard {
267 type Response = i64;
268
269 fn to_frame(&self) -> Frame {
270 array(vec![bulk("SCARD"), bulk(self.key.as_bytes())])
271 }
272
273 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
274 match frame {
275 Frame::Integer(n) => Ok(n),
276 other => Err(RedisError::UnexpectedResponse {
277 expected: "integer",
278 actual: format!("{other:?}"),
279 }),
280 }
281 }
282
283 fn name(&self) -> &str {
284 "SCARD"
285 }
286
287 fn idempotent(&self) -> bool {
288 true
289 }
290}
291
292#[derive(Clone)]
297pub struct SInter {
298 keys: Vec<CommandArg>,
299}
300
301impl SInter {
302 pub fn new(key: impl Into<CommandArg>) -> Self {
304 Self {
305 keys: vec![key.into()],
306 }
307 }
308
309 pub fn keys(keys: impl IntoIterator<Item = impl Into<CommandArg>>) -> Self {
311 Self {
312 keys: keys.into_iter().map(Into::into).collect(),
313 }
314 }
315}
316
317impl Command for SInter {
318 type Response = Vec<Bytes>;
319
320 fn to_frame(&self) -> Frame {
321 let mut args = vec![bulk("SINTER")];
322 for key in &self.keys {
323 args.push(bulk(key.as_bytes()));
324 }
325 array(args)
326 }
327
328 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
329 match frame {
330 Frame::Array(Some(frames)) => frames
331 .into_iter()
332 .map(|f| match f {
333 Frame::BulkString(Some(data)) => Ok(data),
334 other => Err(RedisError::UnexpectedResponse {
335 expected: "bulk string",
336 actual: format!("{other:?}"),
337 }),
338 })
339 .collect(),
340 Frame::Set(items) => items
341 .into_iter()
342 .map(|f| match f {
343 Frame::BulkString(Some(data)) => Ok(data),
344 other => Err(RedisError::UnexpectedResponse {
345 expected: "bulk string",
346 actual: format!("{other:?}"),
347 }),
348 })
349 .collect(),
350 other => Err(RedisError::UnexpectedResponse {
351 expected: "array or set",
352 actual: format!("{other:?}"),
353 }),
354 }
355 }
356
357 fn name(&self) -> &str {
358 "SINTER"
359 }
360
361 fn idempotent(&self) -> bool {
362 true
363 }
364}
365
366#[derive(Clone)]
372pub struct SRandMember {
373 key: CommandArg,
374 count: Option<i64>,
375}
376
377impl SRandMember {
378 pub fn new(key: impl Into<CommandArg>) -> Self {
380 Self {
381 key: key.into(),
382 count: None,
383 }
384 }
385
386 pub fn count(mut self, count: i64) -> Self {
388 self.count = Some(count);
389 self
390 }
391}
392
393impl Command for SRandMember {
394 type Response = Vec<Bytes>;
395
396 fn to_frame(&self) -> Frame {
397 let mut args = vec![bulk("SRANDMEMBER"), bulk(self.key.as_bytes())];
398 if let Some(count) = self.count {
399 args.push(bulk(count.to_string().as_bytes()));
400 }
401 array(args)
402 }
403
404 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
405 match frame {
406 Frame::BulkString(Some(data)) => Ok(vec![data]),
408 Frame::BulkString(None) => Ok(vec![]),
409 Frame::Array(Some(frames)) => frames
410 .into_iter()
411 .map(|f| match f {
412 Frame::BulkString(Some(data)) => Ok(data),
413 other => Err(RedisError::UnexpectedResponse {
414 expected: "bulk string",
415 actual: format!("{other:?}"),
416 }),
417 })
418 .collect(),
419 other => Err(RedisError::UnexpectedResponse {
420 expected: "bulk string or array",
421 actual: format!("{other:?}"),
422 }),
423 }
424 }
425
426 fn name(&self) -> &str {
427 "SRANDMEMBER"
428 }
429
430 fn idempotent(&self) -> bool {
431 true
432 }
433}
434
435#[derive(Clone)]
441pub struct SPop {
442 key: CommandArg,
443 count: Option<u64>,
444}
445
446impl SPop {
447 pub fn new(key: impl Into<CommandArg>) -> Self {
449 Self {
450 key: key.into(),
451 count: None,
452 }
453 }
454
455 pub fn count(mut self, count: u64) -> Self {
457 self.count = Some(count);
458 self
459 }
460}
461
462impl Command for SPop {
463 type Response = Vec<Bytes>;
464
465 fn to_frame(&self) -> Frame {
466 let mut args = vec![bulk("SPOP"), bulk(self.key.as_bytes())];
467 if let Some(count) = self.count {
468 args.push(bulk(count.to_string().as_bytes()));
469 }
470 array(args)
471 }
472
473 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
474 match frame {
475 Frame::BulkString(Some(data)) => Ok(vec![data]),
477 Frame::BulkString(None) => Ok(vec![]),
478 Frame::Array(Some(frames)) => frames
479 .into_iter()
480 .map(|f| match f {
481 Frame::BulkString(Some(data)) => Ok(data),
482 other => Err(RedisError::UnexpectedResponse {
483 expected: "bulk string",
484 actual: format!("{other:?}"),
485 }),
486 })
487 .collect(),
488 other => Err(RedisError::UnexpectedResponse {
489 expected: "bulk string or array",
490 actual: format!("{other:?}"),
491 }),
492 }
493 }
494
495 fn name(&self) -> &str {
496 "SPOP"
497 }
498}
499
500#[derive(Clone)]
505pub struct SDiff {
506 keys: Vec<CommandArg>,
507}
508
509impl SDiff {
510 pub fn new(key: impl Into<CommandArg>) -> Self {
512 Self {
513 keys: vec![key.into()],
514 }
515 }
516
517 pub fn keys(keys: impl IntoIterator<Item = impl Into<CommandArg>>) -> Self {
519 Self {
520 keys: keys.into_iter().map(Into::into).collect(),
521 }
522 }
523}
524
525impl Command for SDiff {
526 type Response = Vec<Bytes>;
527
528 fn to_frame(&self) -> Frame {
529 let mut args = vec![bulk("SDIFF")];
530 for key in &self.keys {
531 args.push(bulk(key.as_bytes()));
532 }
533 array(args)
534 }
535
536 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
537 match frame {
538 Frame::Array(Some(frames)) => frames
539 .into_iter()
540 .map(|f| match f {
541 Frame::BulkString(Some(data)) => Ok(data),
542 other => Err(RedisError::UnexpectedResponse {
543 expected: "bulk string",
544 actual: format!("{other:?}"),
545 }),
546 })
547 .collect(),
548 Frame::Set(items) => items
549 .into_iter()
550 .map(|f| match f {
551 Frame::BulkString(Some(data)) => Ok(data),
552 other => Err(RedisError::UnexpectedResponse {
553 expected: "bulk string",
554 actual: format!("{other:?}"),
555 }),
556 })
557 .collect(),
558 other => Err(RedisError::UnexpectedResponse {
559 expected: "array or set",
560 actual: format!("{other:?}"),
561 }),
562 }
563 }
564
565 fn name(&self) -> &str {
566 "SDIFF"
567 }
568
569 fn idempotent(&self) -> bool {
570 true
571 }
572}
573
574#[derive(Clone)]
580pub struct SDiffStore {
581 destination: CommandArg,
582 keys: Vec<CommandArg>,
583}
584
585impl SDiffStore {
586 pub fn new(
588 destination: impl Into<CommandArg>,
589 keys: impl IntoIterator<Item = impl Into<CommandArg>>,
590 ) -> Self {
591 Self {
592 destination: destination.into(),
593 keys: keys.into_iter().map(Into::into).collect(),
594 }
595 }
596}
597
598impl Command for SDiffStore {
599 type Response = i64;
600
601 fn to_frame(&self) -> Frame {
602 let mut args = vec![bulk("SDIFFSTORE"), bulk(self.destination.as_bytes())];
603 for key in &self.keys {
604 args.push(bulk(key.as_bytes()));
605 }
606 array(args)
607 }
608
609 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
610 match frame {
611 Frame::Integer(n) => Ok(n),
612 other => Err(RedisError::UnexpectedResponse {
613 expected: "integer",
614 actual: format!("{other:?}"),
615 }),
616 }
617 }
618
619 fn name(&self) -> &str {
620 "SDIFFSTORE"
621 }
622}
623
624#[derive(Clone)]
629pub struct SUnion {
630 keys: Vec<CommandArg>,
631}
632
633impl SUnion {
634 pub fn new(key: impl Into<CommandArg>) -> Self {
636 Self {
637 keys: vec![key.into()],
638 }
639 }
640
641 pub fn keys(keys: impl IntoIterator<Item = impl Into<CommandArg>>) -> Self {
643 Self {
644 keys: keys.into_iter().map(Into::into).collect(),
645 }
646 }
647}
648
649impl Command for SUnion {
650 type Response = Vec<Bytes>;
651
652 fn to_frame(&self) -> Frame {
653 let mut args = vec![bulk("SUNION")];
654 for key in &self.keys {
655 args.push(bulk(key.as_bytes()));
656 }
657 array(args)
658 }
659
660 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
661 match frame {
662 Frame::Array(Some(frames)) => frames
663 .into_iter()
664 .map(|f| match f {
665 Frame::BulkString(Some(data)) => Ok(data),
666 other => Err(RedisError::UnexpectedResponse {
667 expected: "bulk string",
668 actual: format!("{other:?}"),
669 }),
670 })
671 .collect(),
672 Frame::Set(items) => items
673 .into_iter()
674 .map(|f| match f {
675 Frame::BulkString(Some(data)) => Ok(data),
676 other => Err(RedisError::UnexpectedResponse {
677 expected: "bulk string",
678 actual: format!("{other:?}"),
679 }),
680 })
681 .collect(),
682 other => Err(RedisError::UnexpectedResponse {
683 expected: "array or set",
684 actual: format!("{other:?}"),
685 }),
686 }
687 }
688
689 fn name(&self) -> &str {
690 "SUNION"
691 }
692
693 fn idempotent(&self) -> bool {
694 true
695 }
696}
697
698#[derive(Clone)]
703pub struct SUnionStore {
704 destination: CommandArg,
705 keys: Vec<CommandArg>,
706}
707
708impl SUnionStore {
709 pub fn new(
711 destination: impl Into<CommandArg>,
712 keys: impl IntoIterator<Item = impl Into<CommandArg>>,
713 ) -> Self {
714 Self {
715 destination: destination.into(),
716 keys: keys.into_iter().map(Into::into).collect(),
717 }
718 }
719}
720
721impl Command for SUnionStore {
722 type Response = i64;
723
724 fn to_frame(&self) -> Frame {
725 let mut args = vec![bulk("SUNIONSTORE"), bulk(self.destination.as_bytes())];
726 for key in &self.keys {
727 args.push(bulk(key.as_bytes()));
728 }
729 array(args)
730 }
731
732 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
733 match frame {
734 Frame::Integer(n) => Ok(n),
735 other => Err(RedisError::UnexpectedResponse {
736 expected: "integer",
737 actual: format!("{other:?}"),
738 }),
739 }
740 }
741
742 fn name(&self) -> &str {
743 "SUNIONSTORE"
744 }
745}
746
747#[derive(Clone)]
753pub struct SMove {
754 source: CommandArg,
755 destination: CommandArg,
756 member: CommandArg,
757}
758
759impl SMove {
760 pub fn new(
762 source: impl Into<CommandArg>,
763 destination: impl Into<CommandArg>,
764 member: impl Into<CommandArg>,
765 ) -> Self {
766 Self {
767 source: source.into(),
768 destination: destination.into(),
769 member: member.into(),
770 }
771 }
772}
773
774impl Command for SMove {
775 type Response = bool;
776
777 fn to_frame(&self) -> Frame {
778 array(vec![
779 bulk("SMOVE"),
780 bulk(self.source.as_bytes()),
781 bulk(self.destination.as_bytes()),
782 bulk(self.member.as_bytes()),
783 ])
784 }
785
786 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
787 match frame {
788 Frame::Integer(n) => Ok(n == 1),
789 Frame::Boolean(b) => Ok(b),
790 other => Err(RedisError::UnexpectedResponse {
791 expected: "integer or boolean",
792 actual: format!("{other:?}"),
793 }),
794 }
795 }
796
797 fn name(&self) -> &str {
798 "SMOVE"
799 }
800}
801
802#[derive(Clone)]
807pub struct SMisMember {
808 key: CommandArg,
809 members: Vec<CommandArg>,
810}
811
812impl SMisMember {
813 pub fn new(key: impl Into<CommandArg>, member: impl Into<CommandArg>) -> Self {
815 Self {
816 key: key.into(),
817 members: vec![member.into()],
818 }
819 }
820
821 pub fn members(
823 key: impl Into<CommandArg>,
824 members: impl IntoIterator<Item = impl Into<CommandArg>>,
825 ) -> Self {
826 Self {
827 key: key.into(),
828 members: members.into_iter().map(Into::into).collect(),
829 }
830 }
831}
832
833impl Command for SMisMember {
834 type Response = Vec<bool>;
835
836 fn to_frame(&self) -> Frame {
837 let mut args = vec![bulk("SMISMEMBER"), bulk(self.key.as_bytes())];
838 for member in &self.members {
839 args.push(bulk(member.as_bytes()));
840 }
841 array(args)
842 }
843
844 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
845 match frame {
846 Frame::Array(Some(frames)) => frames
847 .into_iter()
848 .map(|f| match f {
849 Frame::Integer(n) => Ok(n == 1),
850 Frame::Boolean(b) => Ok(b),
851 other => Err(RedisError::UnexpectedResponse {
852 expected: "integer or boolean",
853 actual: format!("{other:?}"),
854 }),
855 })
856 .collect(),
857 other => Err(RedisError::UnexpectedResponse {
858 expected: "array",
859 actual: format!("{other:?}"),
860 }),
861 }
862 }
863
864 fn name(&self) -> &str {
865 "SMISMEMBER"
866 }
867
868 fn idempotent(&self) -> bool {
869 true
870 }
871}
872
873#[derive(Clone)]
879pub struct SInterStore {
880 destination: CommandArg,
881 keys: Vec<CommandArg>,
882}
883
884impl SInterStore {
885 pub fn new(
887 destination: impl Into<CommandArg>,
888 keys: impl IntoIterator<Item = impl Into<CommandArg>>,
889 ) -> Self {
890 Self {
891 destination: destination.into(),
892 keys: keys.into_iter().map(Into::into).collect(),
893 }
894 }
895}
896
897impl Command for SInterStore {
898 type Response = i64;
899
900 fn to_frame(&self) -> Frame {
901 let mut args = vec![bulk("SINTERSTORE"), bulk(self.destination.as_bytes())];
902 for key in &self.keys {
903 args.push(bulk(key.as_bytes()));
904 }
905 array(args)
906 }
907
908 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
909 match frame {
910 Frame::Integer(n) => Ok(n),
911 other => Err(RedisError::UnexpectedResponse {
912 expected: "integer",
913 actual: format!("{other:?}"),
914 }),
915 }
916 }
917
918 fn name(&self) -> &str {
919 "SINTERSTORE"
920 }
921}
922
923#[derive(Clone)]
929pub struct SInterCard {
930 keys: Vec<CommandArg>,
931 limit: Option<u64>,
932}
933
934impl SInterCard {
935 pub fn new(keys: impl IntoIterator<Item = impl Into<CommandArg>>) -> Self {
937 Self {
938 keys: keys.into_iter().map(Into::into).collect(),
939 limit: None,
940 }
941 }
942
943 pub fn limit(mut self, limit: u64) -> Self {
945 self.limit = Some(limit);
946 self
947 }
948}
949
950impl Command for SInterCard {
951 type Response = i64;
952
953 fn to_frame(&self) -> Frame {
954 let mut args = vec![bulk("SINTERCARD"), bulk(self.keys.len().to_string())];
955 for key in &self.keys {
956 args.push(bulk(key.as_bytes()));
957 }
958 if let Some(limit) = self.limit {
959 args.push(bulk("LIMIT"));
960 args.push(bulk(limit.to_string()));
961 }
962 array(args)
963 }
964
965 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
966 match frame {
967 Frame::Integer(n) => Ok(n),
968 other => Err(RedisError::UnexpectedResponse {
969 expected: "integer",
970 actual: format!("{other:?}"),
971 }),
972 }
973 }
974
975 fn name(&self) -> &str {
976 "SINTERCARD"
977 }
978
979 fn idempotent(&self) -> bool {
980 true
981 }
982}
983
984#[cfg(test)]
985mod tests {
986 use super::*;
987 use redis_tower_core::Command;
988 use redis_tower_protocol::Frame;
989 use redis_tower_protocol::helpers::{array, bulk};
990
991 #[test]
994 fn sadd_single_to_frame() {
995 let cmd = SAdd::new("myset", "a");
996 assert_eq!(
997 cmd.to_frame(),
998 array(vec![bulk("SADD"), bulk("myset"), bulk("a")])
999 );
1000 }
1001
1002 #[test]
1003 fn sadd_multiple_to_frame() {
1004 let cmd = SAdd::members("myset", vec!["a", "b", "c"]);
1005 assert_eq!(
1006 cmd.to_frame(),
1007 array(vec![
1008 bulk("SADD"),
1009 bulk("myset"),
1010 bulk("a"),
1011 bulk("b"),
1012 bulk("c"),
1013 ])
1014 );
1015 }
1016
1017 #[test]
1018 fn sadd_parse_integer() {
1019 let cmd = SAdd::new("myset", "a");
1020 assert_eq!(cmd.parse_response(Frame::Integer(1)).unwrap(), 1);
1021 }
1022
1023 #[test]
1024 fn sadd_parse_error_on_string() {
1025 let cmd = SAdd::new("myset", "a");
1026 assert!(
1027 cmd.parse_response(Frame::SimpleString(Bytes::from("OK")))
1028 .is_err()
1029 );
1030 }
1031
1032 #[test]
1035 fn srem_to_frame() {
1036 let cmd = SRem::new("myset", "a");
1037 assert_eq!(
1038 cmd.to_frame(),
1039 array(vec![bulk("SREM"), bulk("myset"), bulk("a")])
1040 );
1041 }
1042
1043 #[test]
1046 fn smembers_to_frame() {
1047 let cmd = SMembers::new("myset");
1048 assert_eq!(cmd.to_frame(), array(vec![bulk("SMEMBERS"), bulk("myset")]));
1049 }
1050
1051 #[test]
1052 fn smembers_parse_array() {
1053 let cmd = SMembers::new("myset");
1054 let frame = array(vec![
1055 Frame::BulkString(Some(Bytes::from("a"))),
1056 Frame::BulkString(Some(Bytes::from("b"))),
1057 ]);
1058 let result = cmd.parse_response(frame).unwrap();
1059 assert_eq!(result, vec![Bytes::from("a"), Bytes::from("b")]);
1060 }
1061
1062 #[test]
1063 fn smembers_parse_set_frame() {
1064 let cmd = SMembers::new("myset");
1065 let frame = Frame::Set(vec![Frame::BulkString(Some(Bytes::from("x")))]);
1066 let result = cmd.parse_response(frame).unwrap();
1067 assert_eq!(result, vec![Bytes::from("x")]);
1068 }
1069
1070 #[test]
1071 fn smembers_parse_error_on_integer() {
1072 let cmd = SMembers::new("myset");
1073 assert!(cmd.parse_response(Frame::Integer(1)).is_err());
1074 }
1075
1076 #[test]
1079 fn sismember_to_frame() {
1080 let cmd = SIsMember::new("myset", "a");
1081 assert_eq!(
1082 cmd.to_frame(),
1083 array(vec![bulk("SISMEMBER"), bulk("myset"), bulk("a")])
1084 );
1085 }
1086
1087 #[test]
1088 fn sismember_parse_true() {
1089 let cmd = SIsMember::new("myset", "a");
1090 assert!(cmd.parse_response(Frame::Integer(1)).unwrap());
1091 }
1092
1093 #[test]
1094 fn sismember_parse_false() {
1095 let cmd = SIsMember::new("myset", "a");
1096 assert!(!cmd.parse_response(Frame::Integer(0)).unwrap());
1097 }
1098
1099 #[test]
1100 fn sismember_parse_boolean() {
1101 let cmd = SIsMember::new("myset", "a");
1102 assert!(cmd.parse_response(Frame::Boolean(true)).unwrap());
1103 }
1104
1105 #[test]
1108 fn scard_to_frame() {
1109 let cmd = SCard::new("myset");
1110 assert_eq!(cmd.to_frame(), array(vec![bulk("SCARD"), bulk("myset")]));
1111 }
1112
1113 #[test]
1116 fn sinterstore_to_frame() {
1117 let cmd = SInterStore::new("dest", vec!["s1", "s2"]);
1118 assert_eq!(
1119 cmd.to_frame(),
1120 array(vec![
1121 bulk("SINTERSTORE"),
1122 bulk("dest"),
1123 bulk("s1"),
1124 bulk("s2"),
1125 ])
1126 );
1127 }
1128
1129 #[test]
1130 fn sinterstore_parse_integer() {
1131 let cmd = SInterStore::new("dest", vec!["s1"]);
1132 assert_eq!(cmd.parse_response(Frame::Integer(3)).unwrap(), 3);
1133 }
1134
1135 #[test]
1138 fn smismember_to_frame() {
1139 let cmd = SMisMember::members("myset", vec!["a", "b"]);
1140 assert_eq!(
1141 cmd.to_frame(),
1142 array(vec![
1143 bulk("SMISMEMBER"),
1144 bulk("myset"),
1145 bulk("a"),
1146 bulk("b"),
1147 ])
1148 );
1149 }
1150
1151 #[test]
1152 fn smismember_parse_response() {
1153 let cmd = SMisMember::members("myset", vec!["a", "b"]);
1154 let frame = array(vec![Frame::Integer(1), Frame::Integer(0)]);
1155 let result = cmd.parse_response(frame).unwrap();
1156 assert_eq!(result, vec![true, false]);
1157 }
1158
1159 #[test]
1162 fn smove_to_frame() {
1163 let cmd = SMove::new("src", "dst", "member");
1164 assert_eq!(
1165 cmd.to_frame(),
1166 array(vec![
1167 bulk("SMOVE"),
1168 bulk("src"),
1169 bulk("dst"),
1170 bulk("member"),
1171 ])
1172 );
1173 }
1174
1175 #[test]
1176 fn smove_parse_true() {
1177 let cmd = SMove::new("src", "dst", "m");
1178 assert!(cmd.parse_response(Frame::Integer(1)).unwrap());
1179 }
1180
1181 #[test]
1184 fn sintercard_to_frame() {
1185 let cmd = SInterCard::new(vec!["s1", "s2"]).limit(10);
1186 assert_eq!(
1187 cmd.to_frame(),
1188 array(vec![
1189 bulk("SINTERCARD"),
1190 bulk("2"),
1191 bulk("s1"),
1192 bulk("s2"),
1193 bulk("LIMIT"),
1194 bulk("10"),
1195 ])
1196 );
1197 }
1198
1199 #[test]
1202 fn sdiff_to_frame() {
1203 let cmd = SDiff::keys(vec!["s1", "s2"]);
1204 assert_eq!(
1205 cmd.to_frame(),
1206 array(vec![bulk("SDIFF"), bulk("s1"), bulk("s2")])
1207 );
1208 }
1209
1210 #[test]
1213 fn sunion_to_frame() {
1214 let cmd = SUnion::keys(vec!["s1", "s2"]);
1215 assert_eq!(
1216 cmd.to_frame(),
1217 array(vec![bulk("SUNION"), bulk("s1"), bulk("s2")])
1218 );
1219 }
1220
1221 #[test]
1224 fn spop_to_frame_no_count() {
1225 let cmd = SPop::new("myset");
1226 assert_eq!(cmd.to_frame(), array(vec![bulk("SPOP"), bulk("myset")]));
1227 }
1228
1229 #[test]
1230 fn spop_parse_single() {
1231 let cmd = SPop::new("myset");
1232 let frame = Frame::BulkString(Some(Bytes::from("member")));
1233 let result = cmd.parse_response(frame).unwrap();
1234 assert_eq!(result, vec![Bytes::from("member")]);
1235 }
1236
1237 #[test]
1238 fn spop_parse_empty() {
1239 let cmd = SPop::new("myset");
1240 let frame = Frame::BulkString(None);
1241 let result = cmd.parse_response(frame).unwrap();
1242 assert!(result.is_empty());
1243 }
1244}