Skip to content

Commit 905def0

Browse files
committed
update
1 parent 8d74989 commit 905def0

File tree

5 files changed

+108
-42
lines changed

5 files changed

+108
-42
lines changed

crates/rs-pkg/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod util;
12
pub mod x;
23

34
pub fn add(left: usize, right: usize) -> usize {

crates/rs-pkg/src/util/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub use path::*;
2+
3+
pub mod path;

crates/rs-pkg/src/util/path.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use std::{env, fs, path::PathBuf};
2+
3+
pub fn local_dir(folder: Option<&str>) -> PathBuf {
4+
let current_dir = env::current_dir().expect("get current directory failed");
5+
6+
let dest_dir = current_dir.join(folder.unwrap_or("tmp"));
7+
if !dest_dir.exists() {
8+
fs::create_dir(&dest_dir).expect("create dir failed");
9+
}
10+
dest_dir
11+
}
12+
13+
pub fn local_sqlite_url(db_name: Option<&str>) -> &str {
14+
let fp = db_name.unwrap_or("app.db");
15+
let prefix = "sqlite:";
16+
17+
// get or create:
18+
let d = local_dir(None);
19+
20+
// return db url
21+
let db_url = format!("{}{}", prefix, d.join(fp).to_str().unwrap());
22+
Box::leak(db_url.into_boxed_str())
23+
}
24+
25+
#[cfg(test)]
26+
mod test {
27+
use super::*;
28+
29+
#[test]
30+
fn test_get_local_sqlite_url() {
31+
let cases = [None, Some("test.db")];
32+
33+
// iter
34+
for case in cases.iter() {
35+
let url = local_sqlite_url(*case);
36+
println!("url: {}", url);
37+
}
38+
}
39+
}

crates/rs-pkg/src/x/storage/kv.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use std::{io::Write, path::PathBuf};
2+
3+
use pickledb::{PickleDb, PickleDbDumpPolicy, SerializationMethod};
4+
5+
use crate::util;
6+
7+
// kv存储方案:
8+
pub struct KvClient {
9+
pub cli: PickleDb,
10+
}
11+
12+
impl KvClient {
13+
pub fn default() -> Self {
14+
Self::new(None, None)
15+
}
16+
17+
pub fn new(
18+
db_path: Option<PathBuf>,
19+
serialization_method: Option<SerializationMethod>,
20+
) -> Self {
21+
let db_name = "app.kv.json";
22+
let fp = db_path.unwrap_or(util::local_dir(None).join(db_name));
23+
// println!("kv file: {:?}", fp);
24+
25+
// default = json
26+
let m = serialization_method.unwrap_or(SerializationMethod::Json);
27+
28+
// load file:
29+
if fp.exists() {
30+
let db = PickleDb::load(fp.clone(), PickleDbDumpPolicy::AutoDump, m)
31+
.expect("load kv db failed");
32+
return Self { cli: db }
33+
}
34+
35+
// new file:
36+
let db = PickleDb::new(fp, PickleDbDumpPolicy::AutoDump, m);
37+
Self { cli: db }
38+
}
39+
}
40+
41+
#[cfg(test)]
42+
mod tests {
43+
use super::*;
44+
45+
#[test]
46+
fn test_new_kv_client() {
47+
let mut kv = KvClient::default();
48+
49+
let case = [
50+
("test", "test value"),
51+
("test2", "test value2"),
52+
("test3", "test value3"),
53+
("test4", "test value4"),
54+
];
55+
56+
for (k, v) in case.iter() {
57+
kv.cli.set(k, v);
58+
59+
let v: String = kv.cli.get(k).unwrap();
60+
print!("k={}, v={}", k, v);
61+
}
62+
}
63+
}

crates/rs-pkg/src/x/storage/sql.rs

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use sqlx::{
66
ConnectOptions, SqlitePool,
77
};
88

9+
use crate::util;
10+
911
// db 存储方案: SqlClient
1012
pub struct SqliteClient {
1113
pub cli: SqlitePool,
@@ -74,48 +76,6 @@ impl SqliteClient {
7476
}
7577
}
7678

77-
pub mod util {
78-
use std::{env, path::PathBuf};
79-
80-
pub fn local_dir(folder: Option<&str>) -> PathBuf {
81-
let mut current_dir = env::current_dir().expect("get current directory failed");
82-
83-
let dest_dir = current_dir.join(folder.unwrap_or("tmp"));
84-
if !dest_dir.exists() {
85-
std::fs::create_dir(&dest_dir).expect("create tmp dir failed");
86-
}
87-
dest_dir
88-
}
89-
90-
pub fn local_sqlite_url(db_name: Option<&str>) -> &str {
91-
let fp = db_name.unwrap_or("app.db");
92-
let mut prefix = "sqlite:";
93-
94-
// get or create:
95-
let d = local_dir(None);
96-
97-
// return db url
98-
let db_url = format!("{}{}", prefix, d.join(fp).to_str().unwrap());
99-
Box::leak(db_url.into_boxed_str())
100-
}
101-
102-
#[cfg(test)]
103-
mod test {
104-
use super::*;
105-
106-
#[test]
107-
fn test_get_local_sqlite_url() {
108-
let cases = [None, Some("test.db")];
109-
110-
// iter
111-
for case in cases.iter() {
112-
let url = local_sqlite_url(*case);
113-
println!("url: {}", url);
114-
}
115-
}
116-
}
117-
}
118-
11979
#[cfg(test)]
12080
mod test {
12181
use tokio;

0 commit comments

Comments
 (0)