Skip to content

Commit 467d72f

Browse files
committed
Added redis dependency
Signed-off-by: carlosb1 <mcflurry0@gmail.com>
1 parent 1ceca4a commit 467d72f

3 files changed

Lines changed: 149 additions & 12 deletions

File tree

interactive-rs/Cargo.lock

Lines changed: 121 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

interactive-rs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ edition = "2021"
88
[dependencies]
99
clap = { version = "4.0.29", features = ["derive"] }
1010
termion="2.0.1"
11+
redis = "0.22.1"
1112
sled = "0.34"

interactive-rs/src/main.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
extern crate termion;
22

33
use clap::Parser;
4-
use sled::Db;
4+
use redis;
5+
use redis::Commands;
56

67
struct DataRepository {
7-
tree: Db,
8+
client: redis::Client,
89
}
910
impl DataRepository {
10-
pub fn new(file_path: &str) -> Result<DataRepository, String> {
11-
match sled::open(file_path) {
12-
Ok(tr) => Ok(DataRepository { tree: tr }),
13-
Err(e) => Err(format!("{:?}", e)),
14-
}
11+
pub fn new(file_path: &str) -> Result<DataRepository, &'static str> {
12+
Ok(DataRepository {
13+
client: redis::Client::open(file_path)
14+
.map_err(|e| "It was not possible to open the file")?,
15+
})
1516
}
1617

17-
pub fn add(&mut self, link: String, tags: Vec<String>) {
18+
pub fn add(&mut self, link: String, tags: Vec<String>) -> Result<(), &'static str> {
19+
let mut con = self
20+
.client
21+
.get_connection()
22+
.map_err(|e| "It could not get a connection")?;
23+
1824
tags.iter().for_each(|tag| {
19-
let _ = self.tree.insert(tag.as_str(), link.as_str());
25+
let _: () = con
26+
.set(tag.as_str(), link.as_str())
27+
.expect("It could not set the values in the connection");
2028
});
2129

22-
let _ = self.tree.flush();
30+
Ok(())
2331
}
24-
pub fn list(&mut self) {
25-
//let all_values: Vec<&str> = self.tree.iter().map(|val| val.unwrap().into()).collect();
32+
pub fn list(&mut self) -> Result<(), &'static str> {
33+
let mut con = self
34+
.client
35+
.get_connection()
36+
.map_err(|e| "It could not get a connection")?;
37+
38+
let _: () = redis::cmd("KEYS").arg("*").query(&mut con).unwrap();
39+
40+
Ok(())
2641
}
2742
}
2843

0 commit comments

Comments
 (0)