wresp 0.1.3

RESP (REdis Serialization Protocol) response writer and serializer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! libs/server/Resp/RespCommand.cs:RespCommand

#[repr(u16)]
#[derive(
  Debug,
  Clone,
  Copy,
  PartialEq,
  Eq,
  Hash,
  num_enum::TryFromPrimitive,
  num_enum::IntoPrimitive,
  strum::EnumString,
  strum::Display,
  strum::IntoStaticStr,
  strum::AsRefStr,
)]
#[strum(ascii_case_insensitive, serialize_all = "SCREAMING_SNAKE_CASE")]
pub enum RespCommand {
  None = 0,
  Append = 1,
  Bitfield = 2,
  Bzmpop = 3,
  Bzpopmax = 4,
  Bzpopmin = 5,
  Decr = 6,
  Decrby = 7,
  Del = 8,
  Delifexpim = 9,
  Delifgreater = 10,
  Expire = 11,
  Expireat = 12,
  Flushall = 13,
  Flushdb = 14,
  Geoadd = 15,
  Georadius = 16,
  Georadiusbymember = 17,
  Geosearchstore = 18,
  Getdel = 19,
  Getex = 20,
  Getset = 21,
  Hcollect = 22,
  Hdel = 23,
  Hexpire = 24,
  Hpexpire = 25,
  Hexpireat = 26,
  Hpexpireat = 27,
  Hpersist = 28,
  Hincrby = 29,
  Hincrbyfloat = 30,
  Hmset = 31,
  Hset = 32,
  Hsetnx = 33,
  Incr = 34,
  Incrby = 35,
  Incrbyfloat = 36,
  Linsert = 37,
  Lmove = 38,
  Lmpop = 39,
  Lpop = 40,
  Lpush = 41,
  Lpushx = 42,
  Lrem = 43,
  Lset = 44,
  Ltrim = 45,
  Blpop = 46,
  Brpop = 47,
  Blmove = 48,
  Brpoplpush = 49,
  Blmpop = 50,
  Migrate = 51,
  Mset = 52,
  Msetnx = 53,
  Persist = 54,
  Pexpire = 55,
  Pexpireat = 56,
  Pfadd = 57,
  Pfmerge = 58,
  Psetex = 59,
  Rename = 60,
  Ricreate = 61,
  Ridel = 62,
  // 编号 63/64(C# RIPROMOTE/RIRESTORE)空缺:MainStore 内部 RMW 僵尸命令,
  // rust 无 header-cmd 式存储层 RMW,RI 提升走独立 wbftree(ri_promote),不设成员
  Riset = 65,
  Restore = 66,
  Renamenx = 67,
  Rpop = 68,
  Rpoplpush = 69,
  Rpush = 70,
  Rpushx = 71,
  Sadd = 72,
  Sdiffstore = 73,
  Set = 74,
  Setbit = 75,
  Setex = 76,
  Setexnx = 77,
  /// 仅与 C# 枚举 1:1 占位(BasicCommands.cs:NetworkSET_Conditional 派发目标),
  /// 实际语义由 wnode 的 SetCmd 枚举承接,全仓无分发消费
  Setexxx = 78,
  Setnx = 79,
  Setifmatch = 80,
  Setifgreater = 81,
  Setwithetag = 82,
  /// 仅与 C# 枚举 1:1 占位(BasicCommands.cs:NetworkSET_Conditional 派发目标),
  /// 实际语义由 wnode 的 SetCmd 枚举承接,全仓无分发消费
  Setkeepttl = 83,
  /// 仅与 C# 枚举 1:1 占位(BasicCommands.cs:NetworkSET_Conditional 派发目标),
  /// 实际语义由 wnode 的 SetCmd 枚举承接,全仓无分发消费
  Setkeepttlxx = 84,
  Setrange = 85,
  Sinterstore = 86,
  Smove = 87,
  Spop = 88,
  Srem = 89,
  Sunionstore = 90,
  Swapdb = 91,
  Unlink = 92,
  Vadd = 93,
  Vrem = 94,
  Vsetattr = 95,
  Zadd = 96,
  Zcollect = 97,
  Zdiffstore = 98,
  Zexpire = 99,
  Zpexpire = 100,
  Zexpireat = 101,
  Zpexpireat = 102,
  Zpersist = 103,
  Zincrby = 104,
  Zmpop = 105,
  Zinterstore = 106,
  Zpopmax = 107,
  Zpopmin = 108,
  Zrangestore = 109,
  Zrem = 110,
  Zremrangebylex = 111,
  Zremrangebyrank = 112,
  Zremrangebyscore = 113,
  Zunionstore = 114,
  Bitop = 115,
  BitopAnd = 116,
  BitopOr = 117,
  BitopXor = 118,
  BitopNot = 119,
  BitopDiff = 120,
  Bitcount = 121,
  BitfieldRo = 122,
  Bitpos = 123,
  Coscan = 124,
  Dbsize = 125,
  Dump = 126,
  Exists = 127,
  Expiretime = 128,
  Geodist = 129,
  Geohash = 130,
  Geopos = 131,
  GeoradiusRo = 132,
  GeoradiusbymemberRo = 133,
  Geosearch = 134,
  Get = 135,
  Getbit = 136,
  Getifnotmatch = 137,
  Getrange = 138,
  Getwithetag = 139,
  Hexists = 140,
  Hget = 141,
  Hgetall = 142,
  Hkeys = 143,
  Hlen = 144,
  Hmget = 145,
  Hrandfield = 146,
  Hscan = 147,
  Hstrlen = 148,
  Hvals = 149,
  Keys = 150,
  Lcs = 151,
  Httl = 152,
  Hpttl = 153,
  Hexpiretime = 154,
  Hpexpiretime = 155,
  Lindex = 156,
  Llen = 157,
  Lpos = 158,
  Lrange = 159,
  MemoryUsage = 160,
  Mget = 161,
  ObjectEncoding = 162,
  ObjectFreq = 163,
  ObjectIdletime = 164,
  ObjectRefcount = 165,
  Pexpiretime = 166,
  Pfcount = 167,
  Pttl = 168,
  Scan = 169,
  Scard = 170,
  Sdiff = 171,
  Sinter = 172,
  Sintercard = 173,
  Sismember = 174,
  Smembers = 175,
  Smismember = 176,
  Spublish = 177,
  Srandmember = 178,
  Sscan = 179,
  Ssubscribe = 180,
  Strlen = 181,
  Substr = 182,
  Sunion = 183,
  Ttl = 184,
  Type = 185,
  Vcard = 186,
  Vdim = 187,
  Vemb = 188,
  Vgetattr = 189,
  Vinfo = 190,
  Vismember = 191,
  Vlinks = 192,
  Vrandmember = 193,
  Vsim = 194,
  Watch = 195,
  Watchms = 196,
  Watchos = 197,
  Zcard = 198,
  Zcount = 199,
  Zdiff = 200,
  Zinter = 201,
  Zintercard = 202,
  Zlexcount = 203,
  Zmscore = 204,
  Zrandmember = 205,
  Zrange = 206,
  Zrangebylex = 207,
  Zrangebyscore = 208,
  Zrank = 209,
  Zrevrange = 210,
  Zrevrangebylex = 211,
  Zrevrangebyscore = 212,
  Zrevrank = 213,
  Zttl = 214,
  Zpttl = 215,
  Zexpiretime = 216,
  Zpexpiretime = 217,
  Zscan = 218,
  Zscore = 219,
  Zunion = 220,
  Riconfig = 221,
  Riexists = 222,
  Riget = 223,
  Rimetrics = 224,
  Rirange = 225,
  Riscan = 226,
  Eval = 227,
  Evalsha = 228,
  Async = 229,
  Ping = 230,
  Pubsub = 231,
  PubsubChannels = 232,
  PubsubNumpat = 233,
  PubsubNumsub = 234,
  Publish = 235,
  Subscribe = 236,
  Psubscribe = 237,
  Unsubscribe = 238,
  Punsubscribe = 239,
  Asking = 240,
  Select = 241,
  Echo = 242,
  Client = 243,
  ClientId = 244,
  ClientInfo = 245,
  ClientList = 246,
  ClientKill = 247,
  ClientGetname = 248,
  ClientSetname = 249,
  ClientSetinfo = 250,
  ClientUnblock = 251,
  Monitor = 252,
  Multi = 256,
  Exec = 257,
  Discard = 258,
  Unwatch = 259,
  Runtxp = 260,
  Readonly = 261,
  Readwrite = 262,
  Replicaof = 263,
  Secondaryof = 264,
  Info = 265,
  Time = 266,
  Role = 267,
  Save = 268,
  Expdelscan = 269,
  Lastsave = 270,
  Bgsave = 271,
  Commitaof = 272,
  Failover = 273,
  Customtxn = 274,
  Customrawstringcmd = 275,
  Customobjcmd = 276,
  Customprocedure = 277,
  Script = 278,
  ScriptExists = 279,
  ScriptFlush = 280,
  ScriptLoad = 281,
  Acl = 282,
  AclCat = 283,
  AclDeluser = 284,
  AclGenpass = 285,
  AclGetuser = 286,
  AclList = 287,
  AclLoad = 288,
  AclSave = 289,
  AclSetuser = 290,
  AclUsers = 291,
  AclWhoami = 292,
  Command = 293,
  CommandCount = 294,
  CommandDocs = 295,
  CommandInfo = 296,
  CommandGetkeys = 297,
  CommandGetkeysandflags = 298,
  Memory = 299,
  Object = 300,
  ObjectHelp = 301,
  Config = 302,
  ConfigGet = 303,
  ConfigRewrite = 304,
  ConfigSet = 305,
  Debug = 306,
  Latency = 307,
  LatencyHelp = 308,
  LatencyHistogram = 309,
  LatencyReset = 310,
  Slowlog = 311,
  SlowlogHelp = 312,
  SlowlogLen = 313,
  SlowlogGet = 314,
  SlowlogReset = 315,
  Cluster = 316,
  ClusterAddslots = 317,
  ClusterAddslotsrange = 318,
  ClusterAdvanceTime = 319,
  ClusterAppendlog = 320,
  ClusterAttachSync = 321,
  ClusterBanlist = 322,
  ClusterBeginReplicaRecover = 323,
  ClusterBumpepoch = 324,
  ClusterCountkeysinslot = 325,
  ClusterDelkeysinslot = 326,
  ClusterDelkeysinslotrange = 327,
  ClusterDelslots = 328,
  ClusterDelslotsrange = 329,
  ClusterEndpoint = 330,
  ClusterFailover = 331,
  ClusterFailreplicationoffset = 332,
  ClusterFailstopwrites = 333,
  ClusterFlushall = 334,
  ClusterForget = 335,
  ClusterGetkeysinslot = 336,
  ClusterGossip = 337,
  ClusterHelp = 338,
  ClusterInfo = 339,
  ClusterInitiateReplicaSync = 340,
  ClusterKeyslot = 341,
  ClusterMeet = 342,
  ClusterMigrate = 343,
  ClusterMlogKeyTime = 344,
  ClusterMtasks = 345,
  ClusterMyid = 346,
  ClusterMyparentid = 347,
  ClusterNodes = 348,
  ClusterPublish = 349,
  ClusterSpublish = 350,
  ClusterReplicas = 351,
  ClusterReplicate = 352,
  ClusterReserve = 353,
  ClusterReset = 354,
  ClusterSendCkptFileSegment = 355,
  ClusterSendCkptMetadata = 356,
  ClusterSetconfigepoch = 357,
  ClusterSetslot = 358,
  ClusterSetslotsrange = 359,
  ClusterShards = 360,
  ClusterSlots = 361,
  ClusterSlotstate = 362,
  ClusterSnapshotData = 363,
  ClusterSync = 364,
  Auth = 365,
  Hello = 366,
  Quit = 367,
  Invalid = 65535,
}

/// 数据命令区间下界(C# FirstDataCommand = FirstWriteCommand = APPEND)
///
/// libs/server/Resp/Parser/RespCommand.cs:FirstDataCommand
pub const FIRST_DATA_COMMAND: RespCommand = RespCommand::Append;
/// 数据命令区间上界(C# LastDataCommand = EVALSHA)
///
/// libs/server/Resp/Parser/RespCommand.cs:LastDataCommand
pub const LAST_DATA_COMMAND: RespCommand = RespCommand::Evalsha;
/// 读命令区间下界(C# FirstReadCommand = LastWriteCommand + 1)
const FIRST_READ_COMMAND: RespCommand = RespCommand::Bitcount;
/// 读命令区间上界(C# LastReadCommand = EVAL - 1)
const LAST_READ_COMMAND: RespCommand = RespCommand::Riscan;

/// 最后一个有效命令(除 INVALID 外枚举最大值 = QUIT = 367)
///
/// libs/server/Resp/Parser/RespCommand.cs:RespCommandExtensions.LastValidCommand
/// (`Enum.GetValues<RespCommand>().Where(cmd => cmd != INVALID).Max()`)
pub const LAST_VALID_COMMAND: RespCommand = RespCommand::Quit;

/// 判定命令是否为只读命令(读区间双侧判定,无符号下溢天然出界)
///
/// libs/server/Resp/Parser/RespCommand.cs:IsReadOnly
#[inline]
pub const fn is_read_only(cmd: RespCommand) -> bool {
  let v = (cmd as u16).wrapping_sub(FIRST_READ_COMMAND as u16);
  v <= (LAST_READ_COMMAND as u16 - FIRST_READ_COMMAND as u16)
}

/// 判定命令是否为数据命令(写区间 + 读区间连续覆盖;C# 排除表逐一对标)
///
/// libs/server/Resp/Parser/RespCommand.cs:IsDataCommand
#[inline]
pub const fn is_data_command(cmd: RespCommand) -> bool {
  match cmd {
    // C# 排除表:MIGRATE / DBSIZE / MEMORY-USAGE / FLUSHDB / FLUSHALL / KEYS / SCAN / SWAPDB
    RespCommand::Migrate
    | RespCommand::Dbsize
    | RespCommand::MemoryUsage
    | RespCommand::Flushall
    | RespCommand::Flushdb
    | RespCommand::Keys
    | RespCommand::Scan
    | RespCommand::Swapdb => false,
    _ => {
      let v = (cmd as u16).wrapping_sub(FIRST_DATA_COMMAND as u16);
      v <= (LAST_DATA_COMMAND as u16 - FIRST_DATA_COMMAND as u16)
    }
  }
}

/// 判定命令是否为 CLUSTER 子命令(CLUSTER_ADDSLOTS..=CLUSTER_SYNC 连续区间)
///
/// libs/server/Resp/Parser/RespCommand.cs:IsClusterSubCommand
#[inline]
pub const fn is_cluster_sub_command(cmd: RespCommand) -> bool {
  let v = (cmd as u16).wrapping_sub(RespCommand::ClusterAddslots as u16);
  v <= (RespCommand::ClusterSync as u16 - RespCommand::ClusterAddslots as u16)
}

/// 判定命令是否为纯写命令
///
/// libs/server/Resp/Parser/RespCommand.cs:IsWriteOnly
#[inline]
pub const fn is_write_only(cmd: RespCommand) -> bool {
  let v = (cmd as u16).wrapping_sub(FIRST_DATA_COMMAND as u16);
  v <= (RespCommand::BitopDiff as u16 - FIRST_DATA_COMMAND as u16)
}

/// 如果是写命令返回 1,否则返回 0
///
/// libs/server/Resp/Parser/RespCommand.cs:OneIfWrite
#[inline]
pub const fn one_if_write(cmd: RespCommand) -> u64 {
  if is_write_only(cmd) { 1 } else { 0 }
}

/// 如果是读命令返回 1,否则返回 0
///
/// libs/server/Resp/Parser/RespCommand.cs:OneIfRead
#[inline]
pub const fn one_if_read(cmd: RespCommand) -> u64 {
  if is_read_only(cmd) { 1 } else { 0 }
}

/// 判定命令是否为 RangeIndex 专用命令
///
/// libs/server/Resp/Parser/RespCommand.cs:IsRangeIndexCommand
#[inline]
pub const fn is_range_index_command(cmd: RespCommand) -> bool {
  matches!(
    cmd,
    RespCommand::Ricreate
      | RespCommand::Riset
      | RespCommand::Riget
      | RespCommand::Ridel
      | RespCommand::Riscan
      | RespCommand::Rirange
      | RespCommand::Riexists
      | RespCommand::Riconfig
      | RespCommand::Rimetrics
  )
}

/// 判定命令是否可在 RangeIndex 键上合法操作
///
/// libs/server/Resp/Parser/RespCommand.cs:IsLegalOnRangeIndex
#[inline]
pub const fn is_legal_on_range_index(cmd: RespCommand) -> bool {
  matches!(
    cmd,
    RespCommand::Del
      | RespCommand::Unlink
      | RespCommand::Type
      | RespCommand::Debug
      | RespCommand::Rename
      | RespCommand::Renamenx
  ) || is_range_index_command(cmd)
}

/// 判定命令是否为 VectorSet 专用命令
///
/// libs/server/Resp/Parser/RespCommand.cs:IsVectorSetCommand
#[inline]
pub const fn is_vector_set_command(cmd: RespCommand) -> bool {
  matches!(
    cmd,
    RespCommand::Vadd
      | RespCommand::Vcard
      | RespCommand::Vdim
      | RespCommand::Vemb
      | RespCommand::Vgetattr
      | RespCommand::Vinfo
      | RespCommand::Vismember
      | RespCommand::Vlinks
      | RespCommand::Vrandmember
      | RespCommand::Vrem
      | RespCommand::Vsetattr
      | RespCommand::Vsim
  )
}

/// 判定命令是否可在 VectorSet 键上合法操作
///
/// libs/server/Resp/Parser/RespCommand.cs:IsLegalOnVectorSet
#[inline]
pub const fn is_legal_on_vector_set(cmd: RespCommand) -> bool {
  matches!(
    cmd,
    RespCommand::Del
      | RespCommand::Unlink
      | RespCommand::Type
      | RespCommand::Debug
      | RespCommand::Rename
      | RespCommand::Renamenx
  ) || is_vector_set_command(cmd)
}

#[cfg(test)]
mod tests {
  use std::str::FromStr;

  use super::*;

  #[test]
  fn test_resp_command_strum_roundtrip() {
    assert_eq!(RespCommand::Append.as_ref(), "APPEND");
    assert_eq!(RespCommand::BitopAnd.as_ref(), "BITOP_AND");
    assert_eq!(RespCommand::AclCat.as_ref(), "ACL_CAT");
    assert_eq!(
      RespCommand::ClusterSendCkptFileSegment.as_ref(),
      "CLUSTER_SEND_CKPT_FILE_SEGMENT"
    );

    assert_eq!(RespCommand::from_str("APPEND"), Ok(RespCommand::Append));
    assert_eq!(RespCommand::from_str("append"), Ok(RespCommand::Append));
    assert_eq!(RespCommand::from_str("ApPeNd"), Ok(RespCommand::Append));
    assert_eq!(RespCommand::from_str("acl_cat"), Ok(RespCommand::AclCat));
    assert_eq!(
      RespCommand::from_str("bitop_and"),
      Ok(RespCommand::BitopAnd)
    );
    assert_eq!(
      RespCommand::from_str("cluster_send_ckpt_file_segment"),
      Ok(RespCommand::ClusterSendCkptFileSegment)
    );
    assert!(RespCommand::from_str("NON_EXISTENT_COMMAND").is_err());

    let static_str: &'static str = RespCommand::BitopAnd.into();
    assert_eq!(static_str, "BITOP_AND");
    assert_eq!(RespCommand::BitopAnd.to_string(), "BITOP_AND");
  }
}