1use std::{fmt, io};
4
5use log::debug;
6
7use koibumi_node::db;
8
9use crate::{config::create_data_dir, param::Params};
10
11#[derive(Debug)]
13pub enum Error {
14 IoError(io::Error),
17 SqlxError(sqlx::Error),
20}
21
22impl fmt::Display for Error {
23 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 match self {
25 Self::IoError(err) => err.fmt(f),
26 Self::SqlxError(err) => err.fmt(f),
27 }
28 }
29}
30
31impl std::error::Error for Error {}
32
33impl From<io::Error> for Error {
34 fn from(err: io::Error) -> Self {
35 Self::IoError(err)
36 }
37}
38
39impl From<sqlx::Error> for Error {
40 fn from(err: sqlx::Error) -> Self {
41 Self::SqlxError(err)
42 }
43}
44
45pub async fn prepare(params: &Params) -> Result<db::SqlitePool, Error> {
47 let mut path = create_data_dir(params)?;
48 path.push("node.db");
49 debug!("db: {:?}", path);
50 Ok(db::SqlitePool::connect_with(
51 sqlx::sqlite::SqliteConnectOptions::new()
52 .filename(path)
53 .create_if_missing(true),
54 )
55 .await?)
56}