|
1 | 1 | extern crate termion; |
2 | 2 |
|
3 | 3 | use clap::Parser; |
4 | | -use sled::Db; |
| 4 | +use redis; |
| 5 | +use redis::Commands; |
5 | 6 |
|
6 | 7 | struct DataRepository { |
7 | | - tree: Db, |
| 8 | + client: redis::Client, |
8 | 9 | } |
9 | 10 | 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 | + }) |
15 | 16 | } |
16 | 17 |
|
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 | + |
18 | 24 | 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"); |
20 | 28 | }); |
21 | 29 |
|
22 | | - let _ = self.tree.flush(); |
| 30 | + Ok(()) |
23 | 31 | } |
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(()) |
26 | 41 | } |
27 | 42 | } |
28 | 43 |
|
|
0 commit comments