reth_node_ethstats/
credentials.rs

1use crate::error::EthStatsError;
2use std::str::FromStr;
3
4/// Credentials for connecting to an `EthStats` server
5///
6/// Contains the node identifier, authentication secret, and server host
7/// information needed to establish a connection with the `EthStats` service.
8#[derive(Debug, Clone)]
9pub(crate) struct EthstatsCredentials {
10    /// Unique identifier for this node in the `EthStats` network
11    pub node_id: String,
12    /// Authentication secret for the `EthStats` server
13    pub secret: String,
14    /// Host address of the `EthStats` server
15    pub host: String,
16}
17
18impl FromStr for EthstatsCredentials {
19    type Err = EthStatsError;
20
21    /// Parse credentials from a string in the format "`node_id:secret@host`"
22    ///
23    /// # Arguments
24    /// * `s` - String containing credentials in the format "`node_id:secret@host`"
25    ///
26    /// # Returns
27    /// * `Ok(EthstatsCredentials)` - Successfully parsed credentials
28    /// * `Err(EthStatsError::InvalidUrl)` - Invalid format or missing separators
29    fn from_str(s: &str) -> Result<Self, Self::Err> {
30        let parts: Vec<&str> = s.split('@').collect();
31        if parts.len() != 2 {
32            return Err(EthStatsError::InvalidUrl("Missing '@' separator".to_string()));
33        }
34        let creds = parts[0];
35        let host = parts[1].to_string();
36        let creds_parts: Vec<&str> = creds.split(':').collect();
37        if creds_parts.len() != 2 {
38            return Err(EthStatsError::InvalidUrl(
39                "Missing ':' separator in credentials".to_string(),
40            ));
41        }
42        let node_id = creds_parts[0].to_string();
43        let secret = creds_parts[1].to_string();
44
45        Ok(Self { node_id, secret, host })
46    }
47}