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
/// Documentation for the info interface
use crate::RedisAIClient;
use redis::FromRedisValue;
use redis::{RedisResult, Value};
#[derive(Debug, PartialEq)]
pub struct AIInfo {
version: String,
low_level_api_version: String,
rdb_encoding_version: String,
}
impl FromRedisValue for AIInfo {
fn from_redis_value(v: &Value) -> RedisResult<Self> {
let mut it = v.as_sequence().unwrap().iter();
let (version, low_level_api_version, rdb_encoding_version): (String, String, String) =
match (
it.next(),
it.next(),
it.next(),
it.next(),
it.next(),
it.next(),
) {
(
_, //Some(version_field),
Some(version),
_, //Some(low_level_api_field),
Some(low_level_api_version),
_, //Some(rdb_encoding_field),
Some(rdb_encoding_version),
) => (
String::from_redis_value(version)?,
String::from_redis_value(low_level_api_version)?,
String::from_redis_value(rdb_encoding_version)?,
),
_ => {
return Err(redis::RedisError::from((
redis::ErrorKind::TypeError,
"Cannot convert field to some string",
)))
}
};
Ok(Self {
version,
low_level_api_version,
rdb_encoding_version,
})
}
}
impl RedisAIClient {
pub fn ai_infoget(
&self,
con: &mut redis::Connection,
key: Option<String>,
) -> RedisResult<AIInfo> {
// TODO: Should debug be a macro like the py decorator ?
let info: AIInfo = match key {
Some(key) => {
if self.debug {
println!("RedisAI module running: AI.INFO with the key {}", key)
}
redis::cmd("AI.INFO").arg(key).query(con)?
}
_ => {
if self.debug {
println!("RedisAI module running: AI.INFO")
}
redis::cmd("AI.INFO").arg(key).query(con)?
}
};
Ok(info)
}
pub fn ai_inforeset(&self, con: &mut redis::Connection, key: String) -> RedisResult<()> {
redis::cmd("AI.INFO").arg(key).arg("RESETSTAT").query(con)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ai_info_just_on_module() {
let aiclient: RedisAIClient = RedisAIClient { debug: false };
let client = redis::Client::open("redis://127.0.0.1/").unwrap();
let mut con = client.get_connection().unwrap();
assert_eq!(
AIInfo {
version: "99.99.99".to_string(),
low_level_api_version: "1".to_string(),
rdb_encoding_version: "1".to_string(),
},
aiclient.ai_infoget(&mut con, None).unwrap()
);
}
//#[test]
//#[should_panic(
// expected = "value: An error was signalled by the server: cannot find run info for key"
//)]
//fn ai_info_on_a_key() {
// let aiclient: RedisAIClient = RedisAIClient { debug: true };
// let client = redis::Client::open("redis://127.0.0.1/").unwrap();
// let mut con = client.get_connection().unwrap();
// assert_eq!(
// AIInfo {
// version: "99.99.99".to_string(),
// low_level_api_version: "1".to_string(),
// rdb_encoding_version: "1".to_string(),
// },
// aiclient
// .ai_infoget(&mut con, Some("m".to_string()))
// .unwrap()
// );
//}
}