torrust_metrics/metric/
description.rs1use derive_more::Display;
2use serde::{Deserialize, Serialize};
3
4use crate::prometheus::PrometheusSerializable;
5
6#[derive(Debug, Display, Clone, Eq, PartialEq, Default, Deserialize, Serialize, Hash, Ord, PartialOrd)]
7pub struct MetricDescription(String);
8
9impl MetricDescription {
10 #[must_use]
11 pub fn new(name: &str) -> Self {
12 Self(name.to_owned())
13 }
14}
15
16impl From<&str> for MetricDescription {
17 fn from(value: &str) -> Self {
18 Self::new(value)
19 }
20}
21
22impl From<String> for MetricDescription {
23 fn from(value: String) -> Self {
24 Self(value)
25 }
26}
27
28impl PrometheusSerializable for MetricDescription {
29 fn to_prometheus(&self) -> String {
30 self.0.clone()
31 }
32}
33#[cfg(test)]
34mod tests {
35 use super::*;
36
37 #[test]
38 fn it_should_be_created_from_a_string_reference() {
39 let metric = MetricDescription::new("Metric description");
40 assert_eq!(metric.0, "Metric description");
41 }
42
43 #[test]
44 fn it_serializes_to_prometheus() {
45 let label_value = MetricDescription::new("name");
46 assert_eq!(label_value.to_prometheus(), "name");
47 }
48
49 #[test]
50 fn it_should_be_displayed() {
51 let metric = MetricDescription::new("Metric description");
52 assert_eq!(metric.to_string(), "Metric description");
53 }
54
55 #[test]
56 fn it_should_be_converted_from_str() {
57 let metric: MetricDescription = "Metric description".into();
58 assert_eq!(metric, MetricDescription::new("Metric description"));
59 }
60
61 #[test]
62 fn it_should_be_converted_from_string() {
63 let metric: MetricDescription = String::from("Metric description").into();
64 assert_eq!(metric, MetricDescription::new("Metric description"));
65 }
66}