Skip to main content

tempo/
follow.rs

1//! Follow mode configuration for syncing from an upstream node.
2
3use std::{convert::Infallible, str::FromStr};
4use tempo_chainspec::spec::TempoChainSpec;
5
6/// The upstream a follower syncs from, as parsed from `--follow`.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub(crate) enum FollowMode {
9    /// Use the default follow URL for the selected chain.
10    Auto,
11    /// Follow an explicit upstream websocket URL.
12    Url(String),
13}
14
15impl FollowMode {
16    /// Resolves the mode to a concrete upstream URL.
17    ///
18    /// [`Auto`](Self::Auto) resolves to the chain's default follow URL, which may be absent for
19    /// chains without one; an explicit [`Url`](Self::Url) is always returned as-is.
20    pub(crate) fn resolve_url(&self, chain_spec: &TempoChainSpec) -> Option<String> {
21        match self {
22            Self::Auto => chain_spec.default_follow_url().map(str::to_string),
23            Self::Url(url) => Some(url.clone()),
24        }
25    }
26}
27
28impl FromStr for FollowMode {
29    type Err = Infallible;
30
31    fn from_str(s: &str) -> Result<Self, Self::Err> {
32        Ok(if s == "auto" {
33            Self::Auto
34        } else {
35            Self::Url(s.to_string())
36        })
37    }
38}