Skip to main content

glide/commands/
string.rs

1// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
2//! String commands. Mirrors Python's string command surface.
3
4use crate::ValkeyResult;
5use crate::cmd::Cmd;
6use crate::executor::CommandExecutor;
7use crate::value::FromValkeyValue;
8use crate::value::ValkeyValue;
9use crate::write::ToValkeyArgs;
10use async_trait::async_trait;
11use bytes::Bytes;
12
13/// String commands (`GET`, `SET`, `APPEND`, `INCR`, ...).
14#[async_trait]
15pub trait StringCommands: CommandExecutor {
16    /// Longest common subsequence length between two keys (`LCS ... LEN`).
17    async fn lcs_len<K1: ToValkeyArgs + Send, K2: ToValkeyArgs + Send>(
18        &self,
19        key1: K1,
20        key2: K2,
21    ) -> ValkeyResult<i64> {
22        let mut cmd = Cmd::new();
23        cmd.arg("LCS").arg(key1).arg(key2).arg("LEN");
24        i64::from_owned_valkey_value(self.execute_command(cmd, None).await?)
25    }
26
27    /// Get the longest common subsequence of two keys (`LCS`).
28    async fn lcs<K1: ToValkeyArgs + Send, K2: ToValkeyArgs + Send>(
29        &self,
30        key1: K1,
31        key2: K2,
32    ) -> ValkeyResult<Bytes> {
33        let mut cmd = Cmd::new();
34        cmd.arg("LCS").arg(key1).arg(key2);
35        Bytes::from_owned_valkey_value(self.execute_command(cmd, None).await?)
36    }
37
38    /// Get the LCS match positions between two keys (`LCS ... IDX`). Returns the
39    /// raw structured reply (a map of `matches`/`len`). Pass `min_match_len` to
40    /// filter short matches, and `with_match_len` to include per-match lengths.
41    async fn lcs_idx<K1: ToValkeyArgs + Send, K2: ToValkeyArgs + Send>(
42        &self,
43        key1: K1,
44        key2: K2,
45        min_match_len: Option<i64>,
46        with_match_len: bool,
47    ) -> ValkeyResult<ValkeyValue> {
48        let mut cmd = Cmd::new();
49        cmd.arg("LCS").arg(key1).arg(key2).arg("IDX");
50        if let Some(m) = min_match_len {
51            cmd.arg("MINMATCHLEN").arg(m);
52        }
53        if with_match_len {
54            cmd.arg("WITHMATCHLEN");
55        }
56        self.execute_command(cmd, None).await
57    }
58}
59
60impl<T: CommandExecutor + ?Sized> StringCommands for T {}