Skip to main content

systemprompt_models/analytics/
traffic_class.rs

1//! The traffic class a session is counted under: the vocabulary of
2//! `session_traffic_rollup_hourly.traffic_class`.
3//!
4//! Copyright (c) systemprompt.io — Business Source License 1.1.
5//! See <https://systemprompt.io> for licensing details.
6
7use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
10#[serde(rename_all = "snake_case")]
11pub enum TrafficClass {
12    Human,
13    Ghost,
14    Bot,
15}
16
17impl TrafficClass {
18    pub const ALL: [Self; 3] = [Self::Human, Self::Ghost, Self::Bot];
19
20    #[must_use]
21    pub const fn as_str(self) -> &'static str {
22        match self {
23            Self::Human => "human",
24            Self::Ghost => "ghost",
25            Self::Bot => "bot",
26        }
27    }
28
29    #[must_use]
30    pub fn parse(value: &str) -> Option<Self> {
31        Self::ALL.into_iter().find(|class| class.as_str() == value)
32    }
33}
34
35impl std::fmt::Display for TrafficClass {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        f.write_str(self.as_str())
38    }
39}