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
use crate::network::NetworkConfig;
use libp2p::identity::Keypair;
use sled::{Error, Tree};
use std::path::Path;
use std::time::Duration;

pub struct Config {
    pub tree: Tree,
    pub timeout: Duration,
    pub network: NetworkConfig,
}

impl Config {
    pub fn new(tree: Tree, keypair: Keypair) -> Self {
        Self {
            tree,
            timeout: Duration::from_millis(20000),
            network: NetworkConfig::new(keypair),
        }
    }

    pub fn from_tree(tree: Tree) -> Self {
        Self::new(tree, Keypair::generate_ed25519())
    }

    pub fn from_path<T: AsRef<Path>>(path: T) -> Result<Self, Error> {
        let db = sled::open(path)?;
        let tree = db.open_tree("ipfs_tree")?;
        Ok(Self::from_tree(tree))
    }
}