Skip to main content

squib_api/schemas/
metrics.rs

1//! `/metrics` PUT body.
2
3use serde::{Deserialize, Serialize};
4
5use super::common::SafePath;
6
7/// Raw `/metrics` PUT body off the wire.
8#[derive(Debug, Clone, Deserialize)]
9#[serde(deny_unknown_fields)]
10pub struct RawMetricsConfig {
11    /// Path to a regular file or named pipe.
12    pub metrics_path: String,
13}
14
15/// Validated `/metrics` PUT body.
16#[derive(Debug, Clone, Serialize)]
17#[non_exhaustive]
18pub struct MetricsConfig {
19    /// Validated metrics destination.
20    pub metrics_path: SafePath,
21}
22
23impl TryFrom<RawMetricsConfig> for MetricsConfig {
24    type Error = String;
25
26    fn try_from(raw: RawMetricsConfig) -> Result<Self, Self::Error> {
27        Ok(Self {
28            metrics_path: SafePath::new(raw.metrics_path)
29                .map_err(|e| format!("Invalid metrics_path: {e}"))?,
30        })
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn test_should_accept_minimal_metrics_config() {
40        let cfg = MetricsConfig::try_from(RawMetricsConfig {
41            metrics_path: "/tmp/squib.metrics".into(),
42        })
43        .unwrap();
44        assert_eq!(cfg.metrics_path.as_path().as_os_str(), "/tmp/squib.metrics");
45    }
46
47    #[test]
48    fn test_should_reject_empty_metrics_path() {
49        assert!(
50            MetricsConfig::try_from(RawMetricsConfig {
51                metrics_path: String::new(),
52            })
53            .is_err()
54        );
55    }
56}