1use crate::CommandArg;
20use bytes::Bytes;
21use redis_tower_core::{Command, Frame, RedisError};
22use redis_tower_protocol::helpers::{array, bulk};
23
24#[derive(Clone)]
28pub struct AclList;
29
30impl AclList {
31 pub fn new() -> Self {
33 Self
34 }
35}
36
37impl Default for AclList {
38 fn default() -> Self {
39 Self::new()
40 }
41}
42
43impl Command for AclList {
44 type Response = Vec<Bytes>;
45
46 fn to_frame(&self) -> Frame {
47 array(vec![bulk("ACL"), bulk("LIST")])
48 }
49
50 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
51 match frame {
52 Frame::Array(Some(frames)) => frames
53 .into_iter()
54 .map(|f| match f {
55 Frame::BulkString(Some(data)) => Ok(data),
56 other => Err(RedisError::UnexpectedResponse {
57 expected: "bulk string",
58 actual: format!("{other:?}"),
59 }),
60 })
61 .collect(),
62 other => Err(RedisError::UnexpectedResponse {
63 expected: "array",
64 actual: format!("{other:?}"),
65 }),
66 }
67 }
68
69 fn name(&self) -> &str {
70 "ACL LIST"
71 }
72}
73
74#[derive(Debug, Clone)]
78pub struct AclUsers;
79
80impl AclUsers {
81 pub fn new() -> Self {
83 Self
84 }
85}
86
87impl Default for AclUsers {
88 fn default() -> Self {
89 Self::new()
90 }
91}
92
93impl Command for AclUsers {
94 type Response = Vec<Bytes>;
95
96 fn to_frame(&self) -> Frame {
97 array(vec![bulk("ACL"), bulk("USERS")])
98 }
99
100 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
101 match frame {
102 Frame::Array(Some(users)) => users
103 .into_iter()
104 .map(|user| match user {
105 Frame::BulkString(Some(user)) | Frame::SimpleString(user) => Ok(user),
106 other => Err(RedisError::UnexpectedResponse {
107 expected: "bulk or simple string",
108 actual: format!("{other:?}"),
109 }),
110 })
111 .collect(),
112 Frame::Array(None) => Ok(Vec::new()),
113 other => Err(RedisError::UnexpectedResponse {
114 expected: "array",
115 actual: format!("{other:?}"),
116 }),
117 }
118 }
119
120 fn name(&self) -> &str {
121 "ACL USERS"
122 }
123
124 fn idempotent(&self) -> bool {
125 true
126 }
127}
128
129#[derive(Clone)]
133pub struct AclGetUser {
134 username: String,
135}
136
137impl AclGetUser {
138 pub fn new(username: impl Into<String>) -> Self {
140 Self {
141 username: username.into(),
142 }
143 }
144}
145
146impl Command for AclGetUser {
147 type Response = Frame;
148
149 fn to_frame(&self) -> Frame {
150 array(vec![
151 bulk("ACL"),
152 bulk("GETUSER"),
153 bulk(self.username.as_str()),
154 ])
155 }
156
157 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
158 Ok(frame)
159 }
160
161 fn name(&self) -> &str {
162 "ACL GETUSER"
163 }
164}
165
166#[derive(Clone)]
170pub struct AclSetUser {
171 username: String,
172 rules: Vec<String>,
173}
174
175impl AclSetUser {
176 pub fn new(username: impl Into<String>) -> Self {
178 Self {
179 username: username.into(),
180 rules: Vec::new(),
181 }
182 }
183
184 pub fn rule(mut self, rule: impl Into<String>) -> Self {
186 self.rules.push(rule.into());
187 self
188 }
189
190 pub fn rules(mut self, rules: impl IntoIterator<Item = impl Into<String>>) -> Self {
192 self.rules.extend(rules.into_iter().map(Into::into));
193 self
194 }
195}
196
197impl Command for AclSetUser {
198 type Response = ();
199
200 fn to_frame(&self) -> Frame {
201 let mut args = vec![bulk("ACL"), bulk("SETUSER"), bulk(self.username.as_str())];
202 for rule in &self.rules {
203 args.push(bulk(rule.as_str()));
204 }
205 array(args)
206 }
207
208 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
209 match frame {
210 Frame::SimpleString(s) if &s[..] == b"OK" => Ok(()),
211 other => Err(RedisError::UnexpectedResponse {
212 expected: "OK",
213 actual: format!("{other:?}"),
214 }),
215 }
216 }
217
218 fn name(&self) -> &str {
219 "ACL SETUSER"
220 }
221}
222
223#[derive(Clone)]
227pub struct AclDelUser {
228 usernames: Vec<String>,
229}
230
231impl AclDelUser {
232 pub fn new(username: impl Into<String>) -> Self {
234 Self {
235 usernames: vec![username.into()],
236 }
237 }
238
239 pub fn usernames(usernames: impl IntoIterator<Item = impl Into<String>>) -> Self {
241 Self {
242 usernames: usernames.into_iter().map(Into::into).collect(),
243 }
244 }
245}
246
247impl Command for AclDelUser {
248 type Response = i64;
249
250 fn to_frame(&self) -> Frame {
251 let mut args = vec![bulk("ACL"), bulk("DELUSER")];
252 for u in &self.usernames {
253 args.push(bulk(u.as_str()));
254 }
255 array(args)
256 }
257
258 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
259 match frame {
260 Frame::Integer(n) => Ok(n),
261 other => Err(RedisError::UnexpectedResponse {
262 expected: "integer",
263 actual: format!("{other:?}"),
264 }),
265 }
266 }
267
268 fn name(&self) -> &str {
269 "ACL DELUSER"
270 }
271}
272
273#[derive(Clone)]
277pub struct AclCat {
278 category: Option<String>,
279}
280
281impl AclCat {
282 pub fn new() -> Self {
284 Self { category: None }
285 }
286
287 pub fn category(category: impl Into<String>) -> Self {
289 Self {
290 category: Some(category.into()),
291 }
292 }
293}
294
295impl Default for AclCat {
296 fn default() -> Self {
297 Self::new()
298 }
299}
300
301impl Command for AclCat {
302 type Response = Vec<Bytes>;
303
304 fn to_frame(&self) -> Frame {
305 let mut args = vec![bulk("ACL"), bulk("CAT")];
306 if let Some(ref cat) = self.category {
307 args.push(bulk(cat.as_str()));
308 }
309 array(args)
310 }
311
312 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
313 match frame {
314 Frame::Array(Some(frames)) => frames
315 .into_iter()
316 .map(|f| match f {
317 Frame::BulkString(Some(data)) => Ok(data),
318 other => Err(RedisError::UnexpectedResponse {
319 expected: "bulk string",
320 actual: format!("{other:?}"),
321 }),
322 })
323 .collect(),
324 other => Err(RedisError::UnexpectedResponse {
325 expected: "array",
326 actual: format!("{other:?}"),
327 }),
328 }
329 }
330
331 fn name(&self) -> &str {
332 "ACL CAT"
333 }
334}
335
336#[derive(Clone)]
340pub struct AclLog {
341 count: Option<u64>,
342}
343
344impl AclLog {
345 pub fn new() -> Self {
347 Self { count: None }
348 }
349
350 pub fn count(count: u64) -> Self {
352 Self { count: Some(count) }
353 }
354}
355
356impl Default for AclLog {
357 fn default() -> Self {
358 Self::new()
359 }
360}
361
362impl Command for AclLog {
363 type Response = Frame;
364
365 fn to_frame(&self) -> Frame {
366 let mut args = vec![bulk("ACL"), bulk("LOG")];
367 if let Some(count) = self.count {
368 args.push(bulk(count.to_string()));
369 }
370 array(args)
371 }
372
373 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
374 Ok(frame)
375 }
376
377 fn name(&self) -> &str {
378 "ACL LOG"
379 }
380}
381
382#[derive(Clone)]
386pub struct AclLogReset;
387
388impl AclLogReset {
389 pub fn new() -> Self {
391 Self
392 }
393}
394
395impl Default for AclLogReset {
396 fn default() -> Self {
397 Self::new()
398 }
399}
400
401impl Command for AclLogReset {
402 type Response = ();
403
404 fn to_frame(&self) -> Frame {
405 array(vec![bulk("ACL"), bulk("LOG"), bulk("RESET")])
406 }
407
408 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
409 match frame {
410 Frame::SimpleString(s) if &s[..] == b"OK" => Ok(()),
411 other => Err(RedisError::UnexpectedResponse {
412 expected: "OK",
413 actual: format!("{other:?}"),
414 }),
415 }
416 }
417
418 fn name(&self) -> &str {
419 "ACL LOG RESET"
420 }
421}
422
423#[derive(Clone)]
427pub struct AclSave;
428
429impl AclSave {
430 pub fn new() -> Self {
432 Self
433 }
434}
435
436impl Default for AclSave {
437 fn default() -> Self {
438 Self::new()
439 }
440}
441
442impl Command for AclSave {
443 type Response = ();
444
445 fn to_frame(&self) -> Frame {
446 array(vec![bulk("ACL"), bulk("SAVE")])
447 }
448
449 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
450 match frame {
451 Frame::SimpleString(s) if &s[..] == b"OK" => Ok(()),
452 other => Err(RedisError::UnexpectedResponse {
453 expected: "OK",
454 actual: format!("{other:?}"),
455 }),
456 }
457 }
458
459 fn name(&self) -> &str {
460 "ACL SAVE"
461 }
462}
463
464#[derive(Clone)]
468pub struct AclLoad;
469
470impl AclLoad {
471 pub fn new() -> Self {
473 Self
474 }
475}
476
477impl Default for AclLoad {
478 fn default() -> Self {
479 Self::new()
480 }
481}
482
483impl Command for AclLoad {
484 type Response = ();
485
486 fn to_frame(&self) -> Frame {
487 array(vec![bulk("ACL"), bulk("LOAD")])
488 }
489
490 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
491 match frame {
492 Frame::SimpleString(s) if &s[..] == b"OK" => Ok(()),
493 other => Err(RedisError::UnexpectedResponse {
494 expected: "OK",
495 actual: format!("{other:?}"),
496 }),
497 }
498 }
499
500 fn name(&self) -> &str {
501 "ACL LOAD"
502 }
503}
504
505#[derive(Clone)]
509pub struct AclWhoAmI;
510
511impl AclWhoAmI {
512 pub fn new() -> Self {
514 Self
515 }
516}
517
518impl Default for AclWhoAmI {
519 fn default() -> Self {
520 Self::new()
521 }
522}
523
524impl Command for AclWhoAmI {
525 type Response = String;
526
527 fn to_frame(&self) -> Frame {
528 array(vec![bulk("ACL"), bulk("WHOAMI")])
529 }
530
531 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
532 match frame {
533 Frame::BulkString(Some(s)) => Ok(String::from_utf8_lossy(&s).into_owned()),
534 other => Err(RedisError::UnexpectedResponse {
535 expected: "bulk string",
536 actual: format!("{other:?}"),
537 }),
538 }
539 }
540
541 fn name(&self) -> &str {
542 "ACL WHOAMI"
543 }
544}
545
546#[derive(Clone)]
551pub struct AclGenPass {
552 bits: Option<u32>,
553}
554
555impl AclGenPass {
556 pub fn new() -> Self {
558 Self { bits: None }
559 }
560
561 pub fn bits(bits: u32) -> Self {
563 Self { bits: Some(bits) }
564 }
565}
566
567impl Default for AclGenPass {
568 fn default() -> Self {
569 Self::new()
570 }
571}
572
573impl Command for AclGenPass {
574 type Response = String;
575
576 fn to_frame(&self) -> Frame {
577 let mut args = vec![bulk("ACL"), bulk("GENPASS")];
578 if let Some(bits) = self.bits {
579 args.push(bulk(bits.to_string()));
580 }
581 array(args)
582 }
583
584 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
585 match frame {
586 Frame::BulkString(Some(s)) => Ok(String::from_utf8_lossy(&s).into_owned()),
587 other => Err(RedisError::UnexpectedResponse {
588 expected: "bulk string",
589 actual: format!("{other:?}"),
590 }),
591 }
592 }
593
594 fn name(&self) -> &str {
595 "ACL GENPASS"
596 }
597}
598
599#[derive(Clone)]
605pub struct AclDryRun {
606 username: String,
607 command: String,
608 args: Vec<CommandArg>,
609}
610
611impl AclDryRun {
612 pub fn new(username: impl Into<String>, command: impl Into<String>) -> Self {
614 Self {
615 username: username.into(),
616 command: command.into(),
617 args: Vec::new(),
618 }
619 }
620
621 pub fn arg(mut self, arg: impl Into<CommandArg>) -> Self {
623 self.args.push(arg.into());
624 self
625 }
626
627 pub fn args(mut self, args: impl IntoIterator<Item = impl Into<CommandArg>>) -> Self {
629 self.args.extend(args.into_iter().map(Into::into));
630 self
631 }
632}
633
634impl Command for AclDryRun {
635 type Response = String;
636
637 fn to_frame(&self) -> Frame {
638 let mut frame_args = vec![
639 bulk("ACL"),
640 bulk("DRYRUN"),
641 bulk(self.username.as_str()),
642 bulk(self.command.as_str()),
643 ];
644 for arg in &self.args {
645 frame_args.push(bulk(arg));
646 }
647 array(frame_args)
648 }
649
650 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
651 match frame {
652 Frame::SimpleString(s) => Ok(String::from_utf8_lossy(&s).into_owned()),
653 Frame::BulkString(Some(s)) => Ok(String::from_utf8_lossy(&s).into_owned()),
654 other => Err(RedisError::UnexpectedResponse {
655 expected: "simple string or bulk string",
656 actual: format!("{other:?}"),
657 }),
658 }
659 }
660
661 fn name(&self) -> &str {
662 "ACL DRYRUN"
663 }
664}
665
666#[derive(Clone)]
670pub struct AclHelp;
671
672impl AclHelp {
673 pub fn new() -> Self {
675 Self
676 }
677}
678
679impl Default for AclHelp {
680 fn default() -> Self {
681 Self::new()
682 }
683}
684
685impl Command for AclHelp {
686 type Response = Vec<Bytes>;
687
688 fn to_frame(&self) -> Frame {
689 array(vec![bulk("ACL"), bulk("HELP")])
690 }
691
692 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
693 crate::help::parse_help_lines(frame)
694 }
695
696 fn name(&self) -> &str {
697 "ACL HELP"
698 }
699
700 fn idempotent(&self) -> bool {
701 true
702 }
703}
704
705#[cfg(test)]
706mod tests {
707 use super::*;
708 use redis_tower_core::Command;
709 use redis_tower_protocol::Frame;
710 use redis_tower_protocol::helpers::{array, bulk};
711
712 #[test]
715 fn acl_whoami_to_frame() {
716 let cmd = AclWhoAmI::new();
717 assert_eq!(cmd.to_frame(), array(vec![bulk("ACL"), bulk("WHOAMI")]));
718 }
719
720 #[test]
721 fn acl_whoami_parse_response() {
722 let cmd = AclWhoAmI::new();
723 let frame = Frame::BulkString(Some(Bytes::from("default")));
724 assert_eq!(cmd.parse_response(frame).unwrap(), "default");
725 }
726
727 #[test]
728 fn acl_whoami_parse_error_on_integer() {
729 let cmd = AclWhoAmI::new();
730 assert!(cmd.parse_response(Frame::Integer(1)).is_err());
731 }
732
733 #[test]
736 fn acl_cat_to_frame_no_category() {
737 let cmd = AclCat::new();
738 assert_eq!(cmd.to_frame(), array(vec![bulk("ACL"), bulk("CAT")]));
739 }
740
741 #[test]
742 fn acl_cat_to_frame_with_category() {
743 let cmd = AclCat::category("string");
744 assert_eq!(
745 cmd.to_frame(),
746 array(vec![bulk("ACL"), bulk("CAT"), bulk("string")])
747 );
748 }
749
750 #[test]
751 fn acl_cat_parse_response() {
752 let cmd = AclCat::new();
753 let frame = Frame::Array(Some(vec![
754 Frame::BulkString(Some(Bytes::from("string"))),
755 Frame::BulkString(Some(Bytes::from("list"))),
756 ]));
757 let result = cmd.parse_response(frame).unwrap();
758 assert_eq!(result, vec![Bytes::from("string"), Bytes::from("list")]);
759 }
760
761 #[test]
762 fn acl_cat_parse_error_on_integer() {
763 let cmd = AclCat::new();
764 assert!(cmd.parse_response(Frame::Integer(1)).is_err());
765 }
766
767 #[test]
770 fn acl_genpass_to_frame_default() {
771 let cmd = AclGenPass::new();
772 assert_eq!(cmd.to_frame(), array(vec![bulk("ACL"), bulk("GENPASS")]));
773 }
774
775 #[test]
776 fn acl_genpass_to_frame_with_bits() {
777 let cmd = AclGenPass::bits(128);
778 assert_eq!(
779 cmd.to_frame(),
780 array(vec![bulk("ACL"), bulk("GENPASS"), bulk("128")])
781 );
782 }
783
784 #[test]
785 fn acl_genpass_parse_response() {
786 let cmd = AclGenPass::new();
787 let frame = Frame::BulkString(Some(Bytes::from("abcdef1234567890")));
788 assert_eq!(cmd.parse_response(frame).unwrap(), "abcdef1234567890");
789 }
790
791 #[test]
794 fn acl_list_to_frame() {
795 let cmd = AclList::new();
796 assert_eq!(cmd.to_frame(), array(vec![bulk("ACL"), bulk("LIST")]));
797 }
798
799 #[test]
800 fn acl_list_parse_response() {
801 let cmd = AclList::new();
802 let frame = Frame::Array(Some(vec![Frame::BulkString(Some(Bytes::from(
803 "user default on ~* +@all",
804 )))]));
805 let result = cmd.parse_response(frame).unwrap();
806 assert_eq!(result, vec![Bytes::from("user default on ~* +@all")]);
807 }
808
809 #[test]
812 fn acl_users_to_frame_and_metadata() {
813 let cmd = AclUsers::new();
814 assert_eq!(cmd.to_frame(), array(vec![bulk("ACL"), bulk("USERS")]));
815 assert_eq!(cmd.name(), "ACL USERS");
816 assert!(cmd.idempotent());
817 }
818
819 #[test]
820 fn acl_users_parses_resp2_and_defensive_simple_strings() {
821 let cmd = AclUsers::new();
822 let frame = Frame::Array(Some(vec![
823 Frame::BulkString(Some(Bytes::from("default"))),
824 Frame::SimpleString(Bytes::from("application")),
825 ]));
826 assert_eq!(
827 cmd.parse_response(frame).unwrap(),
828 vec![Bytes::from("default"), Bytes::from("application")]
829 );
830 assert_eq!(
831 cmd.parse_response(Frame::Array(None)).unwrap(),
832 Vec::<Bytes>::new()
833 );
834 assert!(cmd.parse_response(Frame::Integer(1)).is_err());
835 }
836
837 #[test]
840 fn acl_setuser_to_frame() {
841 let cmd = AclSetUser::new("testuser")
842 .rule("on")
843 .rule("+@all")
844 .rule("~*");
845 assert_eq!(
846 cmd.to_frame(),
847 array(vec![
848 bulk("ACL"),
849 bulk("SETUSER"),
850 bulk("testuser"),
851 bulk("on"),
852 bulk("+@all"),
853 bulk("~*"),
854 ])
855 );
856 }
857
858 #[test]
859 fn acl_setuser_parse_ok() {
860 let cmd = AclSetUser::new("testuser");
861 let frame = Frame::SimpleString(Bytes::from("OK"));
862 cmd.parse_response(frame).unwrap();
863 }
864
865 #[test]
868 fn acl_deluser_to_frame() {
869 let cmd = AclDelUser::new("testuser");
870 assert_eq!(
871 cmd.to_frame(),
872 array(vec![bulk("ACL"), bulk("DELUSER"), bulk("testuser")])
873 );
874 }
875
876 #[test]
877 fn acl_deluser_parse_response() {
878 let cmd = AclDelUser::new("testuser");
879 assert_eq!(cmd.parse_response(Frame::Integer(1)).unwrap(), 1);
880 }
881
882 #[test]
885 fn acl_dryrun_to_frame() {
886 let cmd = AclDryRun::new("default", "GET").arg("mykey");
887 assert_eq!(
888 cmd.to_frame(),
889 array(vec![
890 bulk("ACL"),
891 bulk("DRYRUN"),
892 bulk("default"),
893 bulk("GET"),
894 bulk("mykey"),
895 ])
896 );
897 }
898
899 #[test]
900 fn acl_dryrun_parse_simple_string() {
901 let cmd = AclDryRun::new("default", "GET");
902 let frame = Frame::SimpleString(Bytes::from("OK"));
903 assert_eq!(cmd.parse_response(frame).unwrap(), "OK");
904 }
905
906 #[test]
907 fn acl_dryrun_parse_bulk_string() {
908 let cmd = AclDryRun::new("default", "GET");
909 let frame = Frame::BulkString(Some(Bytes::from("OK")));
910 assert_eq!(cmd.parse_response(frame).unwrap(), "OK");
911 }
912
913 #[test]
916 fn acl_log_reset_to_frame() {
917 let cmd = AclLogReset::new();
918 assert_eq!(
919 cmd.to_frame(),
920 array(vec![bulk("ACL"), bulk("LOG"), bulk("RESET")])
921 );
922 }
923
924 #[test]
925 fn acl_log_reset_parse_ok() {
926 let cmd = AclLogReset::new();
927 let frame = Frame::SimpleString(Bytes::from("OK"));
928 cmd.parse_response(frame).unwrap();
929 }
930
931 #[test]
932 fn acl_help_to_frame() {
933 let cmd = AclHelp::new();
934 assert_eq!(cmd.to_frame(), array(vec![bulk("ACL"), bulk("HELP")]));
935 assert!(cmd.idempotent());
936 }
937
938 #[test]
939 fn acl_help_parse_lines() {
940 let cmd = AclHelp::new();
941 let reply = array(vec![bulk("ACL <subcommand>"), bulk("WHOAMI")]);
942 let lines = cmd.parse_response(reply).unwrap();
943 assert_eq!(lines.len(), 2);
944 assert_eq!(&lines[1][..], b"WHOAMI");
945 }
946}