miden_validator/
data_directory.rs1use std::ops::Not;
2use std::path::{Path, PathBuf};
3
4#[derive(Clone)]
8pub enum DataDirectory {
9 Server { data: PathBuf },
11 Bootstrap {
13 genesis_block: PathBuf,
14 accounts: PathBuf,
15 data: PathBuf,
16 },
17}
18
19impl DataDirectory {
20 pub fn load_server(data: PathBuf) -> std::io::Result<Self> {
22 verify_is_dir(&data)?;
23 Ok(Self::Server { data })
24 }
25
26 pub fn load_bootstrap(
28 genesis_block: PathBuf,
29 accounts: PathBuf,
30 data: PathBuf,
31 ) -> std::io::Result<Self> {
32 for dir in [&genesis_block, &accounts, &data] {
33 verify_is_dir(dir)?;
34 }
35 Ok(Self::Bootstrap { genesis_block, accounts, data })
36 }
37
38 pub fn database_path(&self) -> PathBuf {
39 self.data().join("validator.sqlite3")
40 }
41
42 pub fn block_store_dir(&self) -> PathBuf {
43 self.data().join("blocks")
44 }
45
46 pub fn genesis_block_path(&self) -> Option<PathBuf> {
47 match self {
48 Self::Bootstrap { genesis_block, .. } => Some(genesis_block.join("genesis.dat")),
49 Self::Server { .. } => None,
50 }
51 }
52
53 pub fn accounts_dir(&self) -> Option<&Path> {
54 match self {
55 Self::Bootstrap { accounts, .. } => Some(accounts),
56 Self::Server { .. } => None,
57 }
58 }
59
60 pub fn display(&self) -> std::path::Display<'_> {
61 self.data().display()
62 }
63
64 fn data(&self) -> &PathBuf {
65 match self {
66 Self::Server { data } | Self::Bootstrap { data, .. } => data,
67 }
68 }
69}
70
71fn verify_is_dir(path: &PathBuf) -> std::io::Result<()> {
72 if fs_err::metadata(path)?.is_dir().not() {
73 return Err(std::io::ErrorKind::NotConnected.into());
74 }
75 Ok(())
76}