-
-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathcommon_args.rs
More file actions
66 lines (57 loc) · 1.89 KB
/
common_args.rs
File metadata and controls
66 lines (57 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use crate::utils::value_parsers;
use anyhow::{anyhow, Result};
use clap::Args;
use sentry::metrics::MetricStr;
use std::str::FromStr;
/// Arguments for send-metric subcommands using float as value type and no default value.
#[derive(Args)]
pub(super) struct FloatValueMetricArgs {
#[command(flatten)]
pub(super) common: CommonMetricArgs,
#[arg(short, long, help = "Metric value, any finite 64 bit float.")]
pub(super) value: f64,
}
/// Common arguments for all send-metric subcommands.
#[derive(Args)]
pub(super) struct CommonMetricArgs {
#[arg(short, long)]
#[arg(help = "The name of the metric, identifying it in Sentry.")]
pub(super) name: MetricName,
#[arg(short, long)]
#[arg(
help = "Any custom unit. You can have multiple metrics with the same name but different \
units."
)]
pub(super) unit: Option<String>,
#[arg(short, long, value_delimiter=',', value_name = "KEY:VALUE", num_args = 1..)]
#[arg(value_parser=value_parsers::kv_parser)]
#[arg(
help = "Metric tags as key:value pairs. Tags allow you to add dimensions to your metrics \
and can be filtered or grouped by in Sentry."
)]
pub(super) tags: Vec<(String, String)>,
}
#[derive(Clone)]
pub(super) struct MetricName(String);
impl FromStr for MetricName {
type Err = anyhow::Error;
/// Metric name must start with an alphabetic character.
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.chars()
.next()
.ok_or_else(|| anyhow!("metric name cannot be empty"))?
.is_ascii_alphabetic()
{
Ok(MetricName(s.to_owned()))
} else {
Err(anyhow!(
"metric name must start with an alphabetic character"
))
}
}
}
impl From<MetricName> for MetricStr {
fn from(name: MetricName) -> MetricStr {
MetricStr::from(name.0)
}
}