miden_node_utils/
config.rs

1use 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/// The `(host, port)` pair for the server's listening socket.
27#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Serialize, Deserialize)]
28pub struct Endpoint {
29    /// Host used by the store.
30    pub host: String,
31    /// Port number used by the store.
32    pub port: u16,
33    /// Protocol type: http or https.
34    #[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
71/// Loads the user configuration.
72///
73/// This function will look for the configuration file at the provided path. If the path is
74/// relative, searches in parent directories all the way to the root as well.
75///
76/// The above configuration options are indented to support easy of packaging and deployment.
77pub 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}