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
use crate::args::RootArgs;
use crate::error::Result;
use crate::script_repo::{DBEnv, ScriptRepo};
use crate::{path, util};
use hyper_scripter_historian::Historian;
pub enum Resource {
None,
Repo(ScriptRepo),
Historian(Historian),
Env(DBEnv),
}
impl Resource {
pub async fn close(self) {
match self {
Self::None => (),
Self::Repo(repo) => repo.close().await,
Self::Historian(historian) => historian.close().await,
Self::Env(env) => env.close().await,
}
}
}
pub struct RepoHolder<'a> {
pub root_args: RootArgs,
pub need_journal: bool,
pub resource: &'a mut Resource,
}
impl<'a> RepoHolder<'a> {
pub async fn init(self) -> Result<&'a mut ScriptRepo> {
let repo = util::init_repo(self.root_args, self.need_journal).await?;
*self.resource = Resource::Repo(repo);
match self.resource {
Resource::Repo(repo) => Ok(repo),
_ => unreachable!(),
}
}
pub async fn historian(self) -> Result<&'a mut Historian> {
let historian = Historian::new(path::get_home().to_owned()).await?;
*self.resource = Resource::Historian(historian);
match self.resource {
Resource::Historian(historian) => Ok(historian),
_ => unreachable!(),
}
}
pub async fn env(self) -> Result<&'a mut DBEnv> {
let (env, init) = util::init_env(self.need_journal).await?;
if init {
log::error!("還沒初始化就想做進階操作 ==");
std::process::exit(0);
}
*self.resource = Resource::Env(env);
match self.resource {
Resource::Env(env) => Ok(env),
_ => unreachable!(),
}
}
}