miden_node_utils/
config.rs1use std::{
2 fmt::{Display, Formatter},
3 io,
4 net::{SocketAddr, ToSocketAddrs},
5 path::Path,
6 vec,
7};
8
9use figment::{
10 providers::{Format, Toml},
11 Figment,
12};
13use serde::{Deserialize, Serialize};
14
15pub const DEFAULT_NODE_RPC_PORT: u16 = 57291;
16pub const DEFAULT_BLOCK_PRODUCER_PORT: u16 = 48046;
17pub const DEFAULT_STORE_PORT: u16 = 28943;
18pub const DEFAULT_FAUCET_SERVER_PORT: u16 = 8080;
19
20#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Serialize, Deserialize, Default)]
21pub enum Protocol {
22 #[default]
23 Http,
24 Https,
25}
26#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Serialize, Deserialize)]
28pub struct Endpoint {
29 pub host: String,
31 pub port: u16,
33 #[serde(default)]
35 pub protocol: Protocol,
36}
37
38impl Endpoint {
39 pub fn localhost(port: u16) -> Self {
40 Endpoint {
41 host: "localhost".to_string(),
42 port,
43 protocol: Protocol::default(),
44 }
45 }
46}
47
48impl ToSocketAddrs for Endpoint {
49 type Iter = vec::IntoIter<SocketAddr>;
50 fn to_socket_addrs(&self) -> io::Result<Self::Iter> {
51 (self.host.as_ref(), self.port).to_socket_addrs()
52 }
53}
54
55impl Display for Endpoint {
56 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
57 let Endpoint { protocol, host, port } = self;
58 f.write_fmt(format_args!("{protocol}://{host}:{port}"))
59 }
60}
61
62impl Display for Protocol {
63 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
64 match self {
65 Protocol::Http => f.write_str("http"),
66 Protocol::Https => f.write_str("https"),
67 }
68 }
69}
70
71pub fn load_config<T: for<'a> Deserialize<'a>>(
78 config_file: impl AsRef<Path>,
79) -> figment::Result<T> {
80 Figment::from(Toml::file(config_file.as_ref())).extract()
81}