Skip to main content

soil_cli/params/
telemetry_params.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
6
7use clap::Args;
8
9/// Parameters used to config telemetry.
10#[derive(Debug, Clone, Args)]
11pub struct TelemetryParams {
12	/// Disable connecting to the Substrate telemetry server.
13	///
14	/// Telemetry is on by default on global chains.
15	#[arg(long)]
16	pub no_telemetry: bool,
17
18	/// The URL of the telemetry server to connect to.
19	///
20	/// This flag can be passed multiple times as a means to specify multiple
21	/// telemetry endpoints. Verbosity levels range from 0-9, with 0 denoting
22	/// the least verbosity.
23	///
24	/// Expected format is 'URL VERBOSITY', e.g. `--telemetry-url 'wss://foo/bar 0'`.
25	#[arg(long = "telemetry-url", value_name = "URL VERBOSITY", value_parser = parse_telemetry_endpoints)]
26	pub telemetry_endpoints: Vec<(String, u8)>,
27}
28
29#[derive(Debug)]
30enum TelemetryParsingError {
31	MissingVerbosity,
32	VerbosityParsingError(std::num::ParseIntError),
33}
34
35impl std::error::Error for TelemetryParsingError {}
36
37impl std::fmt::Display for TelemetryParsingError {
38	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39		match self {
40			TelemetryParsingError::MissingVerbosity => write!(f, "Verbosity level missing"),
41			TelemetryParsingError::VerbosityParsingError(e) => write!(f, "{}", e),
42		}
43	}
44}
45
46fn parse_telemetry_endpoints(s: &str) -> Result<(String, u8), TelemetryParsingError> {
47	let pos = s.find(' ');
48	match pos {
49		None => Err(TelemetryParsingError::MissingVerbosity),
50		Some(pos_) => {
51			let url = s[..pos_].to_string();
52			let verbosity =
53				s[pos_ + 1..].parse().map_err(TelemetryParsingError::VerbosityParsingError)?;
54			Ok((url, verbosity))
55		},
56	}
57}