jupiter_rs/
builder.rs

1use std::sync::Arc;
2
3use crate::init_logging;
4use crate::platform::Platform;
5
6const JUPITER_VERSION: &'static str = "DEVELOPMENT-SNAPSHOT";
7const JUPITER_REVISION: &'static str = "NO-REVISION";
8
9pub struct Builder {
10    versions: Versions,
11    setup_logging: bool,
12    enable_signals: bool,
13    ping_command: bool,
14    core_commands: bool,
15    setup_config: bool,
16    setup_commands: bool,
17    setup_server: bool,
18}
19
20pub struct Versions {
21    pub platform_version: &'static str,
22    pub platform_revision: &'static str,
23    pub product_name: &'static str,
24    pub product_version: &'static str,
25    pub product_revision: &'static str,
26}
27
28impl Builder {
29    pub fn new(
30        product_name: &'static str,
31        product_version: &'static str,
32        product_revision: &'static str,
33    ) -> Self {
34        Builder {
35            versions: Versions {
36                platform_version: JUPITER_VERSION,
37                platform_revision: JUPITER_REVISION,
38                product_name,
39                product_version,
40                product_revision,
41            },
42            setup_logging: false,
43            enable_signals: false,
44            ping_command: false,
45            core_commands: false,
46            setup_config: false,
47            setup_commands: false,
48            setup_server: false,
49        }
50    }
51
52    pub fn enable_all(mut self) -> Self {
53        self.setup_logging = true;
54        self.enable_signals = true;
55        self.ping_command = true;
56        self.core_commands = true;
57        self.setup_config = true;
58        self.setup_commands = true;
59        self.setup_server = true;
60
61        self
62    }
63
64    pub fn enable_logging(mut self) -> Self {
65        self.setup_logging = true;
66        self
67    }
68    pub fn disable_logging(mut self) -> Self {
69        self.setup_logging = false;
70        self
71    }
72    pub fn enable_signals(mut self) -> Self {
73        self.enable_signals = true;
74        self
75    }
76    pub fn disable_signals(mut self) -> Self {
77        self.enable_signals = false;
78        self
79    }
80    pub fn enable_ping_command(mut self) -> Self {
81        self.ping_command = true;
82        self
83    }
84    pub fn disable_ping_command(mut self) -> Self {
85        self.ping_command = false;
86        self
87    }
88    pub fn enable_core_commands(mut self) -> Self {
89        self.core_commands = true;
90        self
91    }
92    pub fn disable_core_commands(mut self) -> Self {
93        self.core_commands = false;
94        self
95    }
96    pub fn enable_config(mut self) -> Self {
97        self.setup_config = true;
98        self
99    }
100    pub fn disable_config(mut self) -> Self {
101        self.setup_config = false;
102        self
103    }
104    pub fn enable_commands(mut self) -> Self {
105        self.setup_commands = true;
106        self
107    }
108    pub fn disable_commands(mut self) -> Self {
109        self.setup_commands = false;
110        self
111    }
112    pub fn enable_server(mut self) -> Self {
113        self.setup_server = true;
114        self
115    }
116    pub fn disable_server(mut self) -> Self {
117        self.setup_server = false;
118        self
119    }
120
121    pub fn build(self) -> Arc<Platform> {
122        let platform = Platform::new();
123
124        if self.setup_logging {
125            init_logging();
126        }
127
128        log::info!(
129            "||. JUPITER (v {} - rev {}) running on {} core(s) in {} CPU(s)",
130            self.versions.platform_version,
131            self.versions.platform_revision,
132            num_cpus::get(),
133            num_cpus::get_physical()
134        );
135
136        log::info!(
137            "--> {} (v {} - rev {}) is being initialized...",
138            self.versions.product_name,
139            self.versions.product_version,
140            self.versions.product_revision
141        );
142
143        platform.register::<Versions>(Arc::new(self.versions));
144
145        if self.enable_signals {
146            crate::signals::install(platform.clone());
147        }
148
149        if self.setup_commands {
150            crate::commands::CommandDictionary::install(&platform);
151
152            if self.ping_command {
153                crate::ping::install(&platform);
154            }
155
156            if self.core_commands {}
157        }
158
159        if self.setup_config {
160            crate::config::install(platform.clone());
161        }
162
163        if self.setup_server {
164            crate::server::Server::install(&platform);
165        }
166
167        platform
168    }
169}