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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
pub use holochain_keystore::paths::*;
use std::path::PathBuf;

/// Subdirectory of the data directory where the conductor stores its
/// databases.
pub const DATABASES_DIRECTORY: &str = "databases";

/// Subdirectory of the data directory where the conductor stores its
/// compiled wasm.
pub const WASM_DIRECTORY: &str = "wasm";

/// Name of the file that conductor config is written to.
pub const CONDUCTOR_CONFIG: &str = "conductor-config.yaml";

/// Newtype to make sure we never accidentaly use or not use the config path.
/// Intentionally has no default value.
#[derive(
    shrinkwraprs::Shrinkwrap,
    derive_more::From,
    Debug,
    PartialEq,
    serde::Serialize,
    serde::Deserialize,
    Clone,
)]
pub struct ConfigRootPath(PathBuf);

impl ConfigRootPath {
    /// Create a new config root path from a data path.
    /// This is useful for when you want to use the same path for both.
    pub fn is_also_data_root_path(&self) -> DataRootPath {
        self.0.clone().into()
    }
}

/// Newtype to make sure we never accidentaly use or not use the config file
/// path.
/// Intentionally has no default value.
#[derive(
    shrinkwraprs::Shrinkwrap,
    derive_more::From,
    Debug,
    PartialEq,
    serde::Serialize,
    serde::Deserialize,
    Clone,
)]
pub struct ConfigFilePath(PathBuf);

impl From<ConfigRootPath> for ConfigFilePath {
    fn from(config_path: ConfigRootPath) -> Self {
        Self::from(config_path.0.join(CONDUCTOR_CONFIG))
    }
}

/// Newtype to make sure we never accidentaly use or not use the data path.
/// Intentionally has no default value.
#[derive(
    shrinkwraprs::Shrinkwrap,
    derive_more::From,
    Debug,
    PartialEq,
    serde::Serialize,
    serde::Deserialize,
    Clone,
)]
pub struct DataRootPath(PathBuf);

impl TryFrom<DataRootPath> for KeystorePath {
    type Error = std::io::Error;
    fn try_from(data_root_path: DataRootPath) -> Result<Self, Self::Error> {
        let path = data_root_path.0.join(KEYSTORE_DIRECTORY);
        if let Ok(false) = path.try_exists() {
            std::fs::create_dir_all(path.clone())?;
        }
        Ok(Self::from(path))
    }
}

/// Newtype to make sure we never accidentaly use or not use the databases path.
/// Intentionally has no default value.
#[derive(
    shrinkwraprs::Shrinkwrap,
    derive_more::From,
    Debug,
    PartialEq,
    serde::Serialize,
    serde::Deserialize,
    Clone,
)]
pub struct DatabasesRootPath(PathBuf);

impl TryFrom<DataRootPath> for DatabasesRootPath {
    type Error = std::io::Error;
    fn try_from(data_path: DataRootPath) -> Result<Self, Self::Error> {
        let path = data_path.0.join(DATABASES_DIRECTORY);
        if let Ok(false) = path.try_exists() {
            std::fs::create_dir_all(path.clone())?;
        }
        Ok(Self::from(path))
    }
}

/// Newtype to make sure we never accidentaly use or not use the wasm path.
/// Intentionally has no default value.
#[derive(
    shrinkwraprs::Shrinkwrap,
    derive_more::From,
    Debug,
    PartialEq,
    serde::Serialize,
    serde::Deserialize,
    Clone,
)]
pub struct WasmRootPath(PathBuf);

impl TryFrom<DataRootPath> for WasmRootPath {
    type Error = std::io::Error;
    fn try_from(data_path: DataRootPath) -> Result<Self, Self::Error> {
        let path = data_path.0.join(WASM_DIRECTORY);
        if let Ok(false) = path.try_exists() {
            std::fs::create_dir_all(path.clone())?;
        }
        Ok(Self::from(path))
    }
}