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 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
use crate::{
client::{prepare_command, PreparedCommand},
commands::SetCondition,
resp::{
cmd, CollectionResponse, CommandArgs, PrimitiveResponse, SingleArg, SingleArgCollection,
ToArgs, Value,
},
};
use serde::de::DeserializeOwned;
/// A group of Redis commands related to [`RedisJson`](https://redis.io/docs/stack/json/)
///
/// # See Also
/// [RedisJson Commands](https://redis.io/commands/?group=json)
pub trait JsonCommands<'a> {
/// Append the json `values` into the array at `path` after the last element in it
///
/// # Arguments
/// * `key` - The key to modify.
/// * `path`- The JSONPath to specify.
/// * `values` - one or more values to append to one or more arrays.
///
/// # Return
/// A collection of integer replies for each path, the array's new size,
/// or nil, if the matching JSON value is not an array.
///
/// # See Also
/// [<https://redis.io/commands/json.arrappend/>](https://redis.io/commands/json.arrappend/)
#[must_use]
fn json_arrappend<K, P, V, VV, R>(
self,
key: K,
path: P,
values: VV,
) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
V: SingleArg,
VV: SingleArgCollection<V>,
R: CollectionResponse<Option<usize>>,
{
prepare_command(self, cmd("JSON.ARRAPPEND").arg(key).arg(path).arg(values))
}
/// Search for the first occurrence of a scalar JSON value in an array
///
/// # Arguments
/// * `key` - The key to parse.
/// * `path`- The JSONPath to specify.
/// * `value` - value index to find in one or more arrays.
///
/// # Return
/// A collection of integer replies for each path,
///
/// the first position in the array of each JSON value that matches the path,
/// -1 if unfound in the array, or nil, if the matching JSON value is not an array.
///
/// # See Also
/// [<https://redis.io/commands/json.arrindex/>](https://redis.io/commands/json.arrindex/)
#[must_use]
fn json_arrindex<K, P, V, R>(
self,
key: K,
path: P,
value: V,
options: JsonArrIndexOptions,
) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
V: SingleArg,
R: CollectionResponse<Option<isize>>,
{
prepare_command(
self,
cmd("JSON.ARRINDEX")
.arg(key)
.arg(path)
.arg(value)
.arg(options),
)
}
/// Insert the json `values` into the array at `path` before the `index` (shifts to the right)
///
/// # Arguments
/// * `key` - The key to modify.
/// * `path`- The JSONPath to specify.
/// * `index`- The position in the array where you want to insert a value.
/// The index must be in the array's range.
/// Inserting at index 0 prepends to the array.
/// Negative index values start from the end of the array.
/// * `values` - one or more values to insert in one or more arrays.
///
/// # Return
/// A collection of integer replies for each path,
/// the array's new size, or nil,
/// if the matching JSON value is not an array.
///
/// # See Also
/// [<https://redis.io/commands/json.arrinsert/>](https://redis.io/commands/json.arrinsert/)
#[must_use]
fn json_arrinsert<K, P, V, VV, R>(
self,
key: K,
path: P,
index: isize,
values: VV,
) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
V: SingleArg,
VV: SingleArgCollection<V>,
R: CollectionResponse<Option<usize>>,
{
prepare_command(
self,
cmd("JSON.ARRINSERT")
.arg(key)
.arg(path)
.arg(index)
.arg(values),
)
}
/// Report the length of the JSON array at `path` in `key`
///
/// # Arguments
/// * `key` - The key to parse.
/// * `path`- The JSONPath to specify.
///
/// # Return
/// A collection of integer replies, an integer for each matching value,
/// each is the array's length, or nil, if the matching value is not an array.
///
/// # See Also
/// [<https://redis.io/commands/json.arrlen/>](https://redis.io/commands/json.arrlen/)
#[must_use]
fn json_arrlen<K, P, R>(self, key: K, path: P) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
R: CollectionResponse<Option<usize>>,
{
prepare_command(self, cmd("JSON.ARRLEN").arg(key).arg(path))
}
/// Remove and return an element from the index in the array
///
/// # Arguments
/// * `key` - The key to modify.
/// * `path`- The JSONPath to specify.
/// * `index`- is position in the array to start popping from.
/// Default is -1, meaning the last element.
/// Out-of-range indexes round to their respective array ends.
/// Popping an empty array returns null.
///
/// # Return
/// A collection of bulk string replies for each path, each reply is the popped JSON value,
/// or nil, if the matching JSON value is not an array.
///
/// # See Also
/// [<https://redis.io/commands/json.arrpop/>](https://redis.io/commands/json.arrpop/)
#[must_use]
fn json_arrpop<K, P, R, RR>(
self,
key: K,
path: P,
index: isize,
) -> PreparedCommand<'a, Self, RR>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
R: PrimitiveResponse + DeserializeOwned,
RR: CollectionResponse<R>,
{
prepare_command(self, cmd("JSON.ARRPOP").arg(key).arg(path).arg(index))
}
/// Remove and return an element from the index in the array
///
/// # Arguments
/// * `key` - The key to modify.
/// * `path`- The JSONPath to specify.
/// * `start`- The index of the first element to keep (previous elements are trimmed).
/// * `stop` - the index of the last element to keep (following elements are trimmed), including the last element.
/// Negative values are interpreted as starting from the end.
///
/// # Return
/// A collection of integer replies for each path, the array's new size,
/// or nil, if the matching JSON value is not an array.
///
/// # See Also
/// [<https://redis.io/commands/json.arrtrim/>](https://redis.io/commands/json.arrtrim/)
#[must_use]
fn json_arrtrim<K, P, R>(
self,
key: K,
path: P,
start: isize,
stop: isize,
) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
R: CollectionResponse<Option<usize>>,
{
prepare_command(
self,
cmd("JSON.ARRTRIM").arg(key).arg(path).arg(start).arg(stop),
)
}
/// Clear container values (arrays/objects) and set numeric values to 0
///
/// # Arguments
/// * `key` - The key to modify.
/// * `path`- The JSONPath to specify.
///
/// # Return
/// The number of values cleared.
///
/// # See Also
/// [<https://redis.io/commands/json.clear/>](https://redis.io/commands/json.clear/)
#[must_use]
fn json_clear<K, P>(self, key: K, path: P) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
{
prepare_command(self, cmd("JSON.CLEAR").arg(key).arg(path))
}
/// Report a value's memory usage in bytes
///
/// # Arguments
/// * `key` - The key to modify.
/// * `path`- The JSONPath to specify.
///
/// # Return
/// A collection of integer replies for each path, the value size in bytes
///
/// # See Also
/// [<https://redis.io/commands/json.debug-memory/>](https://redis.io/commands/json.debug-memory/)
#[must_use]
fn json_debug_memory<K, P, R>(self, key: K, path: P) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
R: CollectionResponse<usize>,
{
prepare_command(self, cmd("JSON.DEBUG").arg("MEMORY").arg(key).arg(path))
}
/// Delete a value
///
/// # Arguments
/// * `key` - The key to modify.
/// * `path`- The JSONPath to specify.
///
/// # Return
/// The number of paths deleted (0 or more).
///
/// # See Also
/// [<https://redis.io/commands/json.del/>](https://redis.io/commands/json.del/)
#[must_use]
fn json_del<K, P>(self, key: K, path: P) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
{
prepare_command(self, cmd("JSON.DEL").arg(key).arg(path))
}
/// See [`json_del`](JsonCommands::json_del)
///
/// # Arguments
/// * `key` - The key to modify.
/// * `path`- The JSONPath to specify.
///
/// # Return
/// The number of paths deleted (0 or more).
///
/// # See Also
/// [<https://redis.io/commands/json.forget/>](https://redis.io/commands/json.forget/)
#[must_use]
fn json_forget<K, P>(self, key: K, path: P) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
{
prepare_command(self, cmd("JSON.FORGET").arg(key).arg(path))
}
/// Return the value at path in JSON serialized form
///
/// # Arguments
/// * `key` - The key to parse.
/// * `options`- See [`JsonOptions`](JsonGetOptions)
///
/// # Return
/// A collection of bulk string replies. Each string is the JSON serialization of each JSON value that matches a path
///
/// # See Also
/// [<https://redis.io/commands/json.get/>](https://redis.io/commands/json.get/)
#[must_use]
fn json_get<K, V>(self, key: K, options: JsonGetOptions) -> PreparedCommand<'a, Self, V>
where
Self: Sized,
K: SingleArg,
V: PrimitiveResponse,
{
prepare_command(self, cmd("JSON.GET").arg(key).arg(options))
}
/// Return the values at `path` from multiple `key` arguments
///
/// # Arguments
/// * `key` - The key to parse.
/// * `options`- See [`JsonOptions`](JsonGetOptions)
///
/// # Return
/// A collection of bulk string replies specified as the JSON serialization of the value at each key's path.
///
/// # See Also
/// [<https://redis.io/commands/json.mget/>](https://redis.io/commands/json.mget/)
#[must_use]
fn json_mget<K, KK, P, V, VV>(self, keys: KK, path: P) -> PreparedCommand<'a, Self, VV>
where
Self: Sized,
K: SingleArg,
KK: SingleArgCollection<K>,
P: SingleArg,
V: PrimitiveResponse + DeserializeOwned,
VV: CollectionResponse<V>,
{
prepare_command(self, cmd("JSON.MGET").arg(keys).arg(path))
}
/// Increment the number value stored at path by number
///
/// # Arguments
/// * `key` - The key to modify.
/// * `path`- The JSONPath to specify.
/// * `value` - number value to increment.
///
/// # Return
/// A bulk string reply specified as a stringified new value for each path,
/// or nil, if the matching JSON value is not a number.
///
/// # See Also
/// [<https://redis.io/commands/json.numincrby/>](https://redis.io/commands/json.numincrby/)
#[must_use]
fn json_numincrby<K, P, V, R>(self, key: K, path: P, value: V) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
V: SingleArg,
R: PrimitiveResponse,
{
prepare_command(self, cmd("JSON.NUMINCRBY").arg(key).arg(path).arg(value))
}
/// Multiply the number value stored at path by number
///
/// # Arguments
/// * `key` - The key to modify.
/// * `path`- The JSONPath to specify.
/// * `value` - number value to increment.
///
/// # Return
/// A bulk string reply specified as a stringified new value for each path,
/// or nil, if the matching JSON value is not a number.
///
/// # See Also
/// [<https://redis.io/commands/json.nummultby/>](https://redis.io/commands/json.nummultby/)
#[must_use]
fn json_nummultby<K, P, V, R>(self, key: K, path: P, value: V) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
V: SingleArg,
R: PrimitiveResponse,
{
prepare_command(self, cmd("JSON.NUMMULTBY").arg(key).arg(path).arg(value))
}
/// Return the keys in the object that's referenced by `path`
///
/// # Arguments
/// * `key` - The key to parse.
/// * `path`- The JSONPath to specify.
///
/// # Return
/// A collection of collection replies for each path,
/// a collection of the key names in the object as a bulk string reply,
/// or an empty collection if the matching JSON value is not an object.
///
/// # See Also
/// [<https://redis.io/commands/json.objkeys/>](https://redis.io/commands/json.objkeys/)
#[must_use]
fn json_objkeys<K, P, R, RR>(self, key: K, path: P) -> PreparedCommand<'a, Self, RR>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
R: PrimitiveResponse + DeserializeOwned,
RR: CollectionResponse<Vec<R>>,
{
prepare_command(self, cmd("JSON.OBJKEYS").arg(key).arg(path))
}
/// Report the number of keys in the JSON object at `path` in `key`
///
/// # Arguments
/// * `key` - The key to parse.
/// * `path`- The JSONPath to specify.
///
/// # Return
/// A collection of integer replies for each path specified as the number of keys in the object or nil,
/// if the matching JSON value is not an object.
///
/// # See Also
/// [<https://redis.io/commands/json.objlen/>](https://redis.io/commands/json.objlen/)
#[must_use]
fn json_objlen<K, P, R>(self, key: K, path: P) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
R: CollectionResponse<Option<usize>>,
{
prepare_command(self, cmd("JSON.OBJLEN").arg(key).arg(path))
}
/// Return the JSON in key in
/// [`Redis serialization protocol specification`](https://redis.io/docs/reference/protocol-spec) form
///
/// # Arguments
/// * `key` - The key to parse.
/// * `path`- The JSONPath to specify.
///
/// # Return
/// A collection of [`Values`](crate::resp::Value)
///
/// This command uses the following mapping from JSON to RESP:
/// * JSON `null` maps to the bulk string reply.
/// * JSON `false` and `true` values map to the simple string reply.
/// * JSON number maps to the integer reply or bulk string reply, depending on type.
/// * JSON string maps to the bulk string reply.
/// * JSON array is represented as an array reply in which the first element is the simple string reply `[`, followed by the array's elements.
/// * JSON object is represented as an array reply in which the first element is the simple string reply `{`.
/// Each successive entry represents a key-value pair as a two-entry array reply of the bulk string reply.
///
/// # See Also
/// [<https://redis.io/commands/json.resp/>](https://redis.io/commands/json.resp/)
#[must_use]
fn json_resp<K, P, VV>(self, key: K, path: P) -> PreparedCommand<'a, Self, VV>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
VV: CollectionResponse<Value>,
{
prepare_command(self, cmd("JSON.RESP").arg(key).arg(path))
}
/// Set the JSON value at `path` in `key`
///
/// # Arguments
/// * `key` - The key to modify.
/// * `path` - JSONPath to specify.\
/// For new Redis keys the path must be the root.\
/// For existing keys, when the entire path exists, the value that it contains is replaced with the json value.\
/// For existing keys, when the path exists, except for the last element, a new child is added with the json value.
/// * `value`- The value to set at the specified path
/// * `condition`- See [`SetCondition`](crate::commands::SetCondition)
///
/// # See Also
/// [<https://redis.io/commands/json.set/>](https://redis.io/commands/json.set/)
#[must_use]
fn json_set<K, P, V>(
self,
key: K,
path: P,
value: V,
condition: SetCondition,
) -> PreparedCommand<'a, Self, ()>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
V: SingleArg,
{
prepare_command(
self,
cmd("JSON.SET").arg(key).arg(path).arg(value).arg(condition),
)
}
/// Append the json-string values to the string at path
///
/// # Arguments
/// * `key` - The key to modify.
/// * `path`- The JSONPath to specify.
/// * `value` - number value to increment.
///
/// # Return
/// A collection of integer replies for each path, the string's new length, or nil, if the matching JSON value is not a string.
///
/// # See Also
/// [<https://redis.io/commands/json.strappend/>](https://redis.io/commands/json.strappend/)
#[must_use]
fn json_strappend<K, P, V, R>(self, key: K, path: P, value: V) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
V: SingleArg,
R: CollectionResponse<Option<usize>>,
{
prepare_command(self, cmd("JSON.STRAPPEND").arg(key).arg(path).arg(value))
}
/// Report the length of the JSON String at `path` in `key`
///
/// # Arguments
/// * `key` - The key to parse.
/// * `path`- The JSONPath to specify.
///
/// # Return
/// returns by recursive descent a collection of integer replies for each path,
/// the array's length, or nil, if the matching JSON value is not a string.
///
/// # See Also
/// [<https://redis.io/commands/json.strlen/>](https://redis.io/commands/json.strlen/)
#[must_use]
fn json_strlen<K, P, R>(self, key: K, path: P) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
R: CollectionResponse<Option<usize>>,
{
prepare_command(self, cmd("JSON.STRLEN").arg(key).arg(path))
}
/// Toggle a Boolean value stored at `path`
///
/// # Arguments
/// * `key` - The key to modify.
/// * `path`- The JSONPath to specify.
///
/// # Return
/// A collection of integer replies for each path, the new value (0 if false or 1 if true),
/// or nil for JSON values matching the path that are not Boolean.
///
/// # See Also
/// [<https://redis.io/commands/json.toggle/>](https://redis.io/commands/json.toggle/)
#[must_use]
fn json_toggle<K, P, R>(self, key: K, path: P) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
R: CollectionResponse<Option<usize>>,
{
prepare_command(self, cmd("JSON.TOGGLE").arg(key).arg(path))
}
/// Report the type of JSON value at `path`
///
/// # Arguments
/// * `key` - The key to parse.
/// * `path`- The JSONPath to specify.
///
/// # Return
/// A collection of string replies for each path, specified as the value's type.
///
/// # See Also
/// [<https://redis.io/commands/json.type/>](https://redis.io/commands/json.type/)
#[must_use]
fn json_type<K, P, R, RR>(self, key: K, path: P) -> PreparedCommand<'a, Self, RR>
where
Self: Sized,
K: SingleArg,
P: SingleArg,
R: PrimitiveResponse + DeserializeOwned,
RR: CollectionResponse<R>,
{
prepare_command(self, cmd("JSON.TYPE").arg(key).arg(path))
}
}
/// Options for the [`json_get`](JsonCommands::json_get) command
#[derive(Default)]
pub struct JsonGetOptions {
command_args: CommandArgs,
}
impl JsonGetOptions {
/// Sets the indentation string for nested levels.
#[must_use]
pub fn indent<I: SingleArg>(mut self, indent: I) -> Self {
Self {
command_args: self.command_args.arg("INDENT").arg(indent).build(),
}
}
/// Sets the string that's printed at the end of each line.
#[must_use]
pub fn newline<NL: SingleArg>(mut self, newline: NL) -> Self {
Self {
command_args: self.command_args.arg("NEWLINE").arg(newline).build(),
}
}
/// Sets the string that's put between a key and a value.
#[must_use]
pub fn space<S: SingleArg>(mut self, space: S) -> Self {
Self {
command_args: self.command_args.arg("SPACE").arg(space).build(),
}
}
/// JSONPath to specify
#[must_use]
pub fn path<P: SingleArg, PP: SingleArgCollection<P>>(mut self, paths: PP) -> Self {
Self {
command_args: self.command_args.arg(paths).build(),
}
}
}
impl ToArgs for JsonGetOptions {
fn write_args(&self, args: &mut CommandArgs) {
args.arg(&self.command_args);
}
}
/// Options for the [`json_arrindex`](JsonCommands::json_arrindex) command
#[derive(Default)]
pub struct JsonArrIndexOptions {
command_args: CommandArgs,
}
impl JsonArrIndexOptions {
/// Inclusive start value to specify in a slice of the array to search.
///
/// Default is 0.
#[must_use]
pub fn start(mut self, start: usize) -> Self {
Self {
command_args: self.command_args.arg(start).build(),
}
}
/// Exclusive stop value to specify in a slice of the array to search, including the last element.
///
/// Default is 0.
/// Negative values are interpreted as starting from the end.
#[must_use]
pub fn stop(mut self, stop: isize) -> Self {
Self {
command_args: self.command_args.arg(stop).build(),
}
}
}
impl ToArgs for JsonArrIndexOptions {
fn write_args(&self, args: &mut CommandArgs) {
args.arg(&self.command_args);
}
}