/
context.rs
60 lines (52 loc) · 1.61 KB
/
context.rs
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
use crate::db;
use std::sync::Arc;
use tokio::sync::RwLock;
pub type SharedContext = Arc<RwLock<Context>>;
#[derive(Clone)]
pub struct Context {
pub db_connection_pool: db::DBConnectionPool,
pub block0_path: String,
pub block0: Vec<u8>,
}
impl Context {
pub fn new(
db_connection_pool: db::DBConnectionPool,
block0_path: &str,
block0: Vec<u8>,
) -> Self {
Self {
db_connection_pool,
block0_path: block0_path.to_string(),
block0,
}
}
}
pub fn new_shared_context(
db_connection_pool: db::DBConnectionPool,
block0_path: &str,
) -> SharedContext {
let block0 = std::fs::read(block0_path).unwrap_or_default();
let context = Context::new(db_connection_pool, block0_path, block0);
Arc::new(RwLock::new(context))
}
#[cfg(test)]
pub mod test {
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use super::*;
use crate::db;
pub fn new_in_memmory_db_test_shared_context() -> SharedContext {
let name: String = thread_rng()
.sample_iter(Alphanumeric)
.take(5)
.map(char::from)
.collect();
let db_url = format!("file:{}?mode=memory&cache=shared", name);
let pool = db::load_db_connection_pool(&db_url).unwrap();
let block0: Vec<u8> = vec![1, 2, 3, 4, 5];
Arc::new(RwLock::new(Context::new(pool, "", block0)))
}
pub fn new_test_shared_context(db_url: &str, block0_path: &str) -> SharedContext {
let pool = db::load_db_connection_pool(db_url).unwrap();
new_shared_context(pool, block0_path)
}
}