1#![forbid(unsafe_code)]
10#![warn(missing_docs)]
11
12pub use kevy_resp::Reply;
13
14pub mod backup;
17
18pub mod migrate;
21
22pub const DEFAULT_HOST: &str = "127.0.0.1";
26pub const DEFAULT_PORT: u16 = 6379;
28
29pub mod bulk;
32
33pub mod shadow;
36
37pub mod backfill_keys;
40pub(crate) mod collections;
41pub mod doctor;
42pub mod lint;
43
44pub fn route_tool(args: &[String]) -> Option<std::process::ExitCode> {
48 let rest = args.get(1..).unwrap_or(&[]);
49 match args.first().map(String::as_str)? {
50 "doctor" => Some(doctor::run_doctor_cli(rest)),
51 "shadow" => Some(shadow::run_shadow_cli(rest)),
52 "lint" => Some(lint::run_lint_cli(rest)),
53 "backfill-keys" => Some(backfill_keys::run_backfill_keys_cli(rest)),
54 _ => None,
55 }
56}
57
58pub fn format_reply(reply: &Reply, indent: usize) -> String {
61 match reply {
62 Reply::Simple(s) => String::from_utf8_lossy(s).into_owned(),
63 Reply::Error(s) | Reply::BlobError(s) => {
64 format!("(error) {}", String::from_utf8_lossy(s))
65 }
66 Reply::Int(n) => format!("(integer) {n}"),
67 Reply::Bulk(b) => format!("\"{}\"", String::from_utf8_lossy(b)),
68 Reply::Nil | Reply::Null => "(nil)".to_string(),
69 Reply::Array(items) if items.is_empty() => "(empty array)".to_string(),
70 Reply::Array(items) | Reply::Set(items) | Reply::Push(items) => {
71 let pad = " ".repeat(indent);
72 items
73 .iter()
74 .enumerate()
75 .map(|(i, it)| format!("{pad}{}) {}", i + 1, format_reply(it, indent + 1)))
76 .collect::<Vec<_>>()
77 .join("\n")
78 }
79 Reply::Map(pairs) if pairs.is_empty() => "(empty map)".to_string(),
81 Reply::Map(pairs) => {
82 let pad = " ".repeat(indent);
83 pairs
84 .iter()
85 .enumerate()
86 .map(|(i, (k, v))| {
87 format!(
88 "{pad}{}) {} => {}",
89 i + 1,
90 format_reply(k, indent + 1),
91 format_reply(v, indent + 1)
92 )
93 })
94 .collect::<Vec<_>>()
95 .join("\n")
96 }
97 Reply::Double(v) => format!("(double) {v}"),
98 Reply::Boolean(b) => format!("(boolean) {}", if *b { "t" } else { "f" }),
99 Reply::Verbatim { fmt, data } => format!(
100 "(verbatim/{}) \"{}\"",
101 String::from_utf8_lossy(fmt),
102 String::from_utf8_lossy(data)
103 ),
104 Reply::BigNumber(s) => format!("(bignum) {}", String::from_utf8_lossy(s)),
105 }
106}
107
108#[cfg(test)]
109mod format_reply_tests {
110 use super::format_reply;
111 use kevy_resp::Reply;
112
113 fn f(r: &Reply) -> String {
114 format_reply(r, 0)
115 }
116
117 #[test]
123 fn every_reply_variant_renders() {
124 assert_eq!(f(&Reply::Simple(b"OK".to_vec())), "OK");
125 assert_eq!(f(&Reply::Error(b"ERR nope".to_vec())), "(error) ERR nope");
126 assert_eq!(f(&Reply::Int(-7)), "(integer) -7");
127 assert_eq!(f(&Reply::Bulk(b"hi".to_vec())), "\"hi\"");
128 assert_eq!(f(&Reply::Nil), "(nil)");
129 assert_eq!(f(&Reply::Array(vec![])), "(empty array)");
130 assert_eq!(f(&Reply::Double(1.5)), "(double) 1.5");
131 assert_eq!(f(&Reply::Boolean(true)), "(boolean) t");
132 assert_eq!(f(&Reply::Boolean(false)), "(boolean) f");
133 assert_eq!(f(&Reply::BigNumber(b"123456789012345678901".to_vec())),
134 "(bignum) 123456789012345678901");
135
136 assert_eq!(f(&Reply::Null), "(nil)");
140 assert_eq!(f(&Reply::BlobError(b"ERR nope".to_vec())), "(error) ERR nope");
141
142 assert_eq!(
143 f(&Reply::Verbatim { fmt: *b"txt", data: b"hello".to_vec() }),
144 "(verbatim/txt) \"hello\""
145 );
146
147 assert_eq!(f(&Reply::Set(vec![Reply::Int(4)])), "1) (integer) 4");
149 assert_eq!(
150 f(&Reply::Push(vec![Reply::Bulk(b"message".to_vec())])),
151 "1) \"message\""
152 );
153 }
154
155 #[test]
158 fn arrays_number_from_one_and_nest() {
159 let flat = Reply::Array(vec![Reply::Int(1), Reply::Bulk(b"x".to_vec())]);
160 assert_eq!(f(&flat), "1) (integer) 1\n2) \"x\"");
161
162 let nested = Reply::Array(vec![Reply::Array(vec![Reply::Int(9)])]);
163 assert_eq!(f(&nested), "1) 1) (integer) 9");
165 }
166
167 #[test]
170 fn maps_render_as_pairs() {
171 assert_eq!(f(&Reply::Map(vec![])), "(empty map)");
172 let m = Reply::Map(vec![(Reply::Bulk(b"k".to_vec()), Reply::Int(1))]);
173 assert_eq!(f(&m), "1) \"k\" => (integer) 1");
174 }
175}