Skip to main content

koibumi_common_sync/
node.rs

1//! Functions to prepare database connection for node.
2
3use std::{fmt, io, path::PathBuf};
4
5use log::debug;
6
7use crate::{config::create_data_dir, param::Params};
8
9/// An error which can be returned when operating on the node database.
10#[derive(Debug)]
11pub enum Error {
12    /// A standard I/O error was caught during operation on the node database.
13    /// The actual error caught is returned as a payload of this variant.
14    IoError(io::Error),
15    /// A Rusqlite error was caught during operation on the node database.
16    /// The actual error caught is returned as a payload of this variant.
17    RusqliteError(rusqlite::Error),
18}
19
20impl fmt::Display for Error {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            Self::IoError(err) => err.fmt(f),
24            Self::RusqliteError(err) => err.fmt(f),
25        }
26    }
27}
28
29impl std::error::Error for Error {}
30
31impl From<io::Error> for Error {
32    fn from(err: io::Error) -> Self {
33        Self::IoError(err)
34    }
35}
36
37impl From<rusqlite::Error> for Error {
38    fn from(err: rusqlite::Error) -> Self {
39        Self::RusqliteError(err)
40    }
41}
42
43/// Returns a database path.
44pub fn prepare(params: &Params) -> Result<PathBuf, Error> {
45    let mut path = create_data_dir(params)?;
46    path.push("node.db");
47    debug!("db: {:?}", path);
48    Ok(path)
49}