Skip to main content

systemprompt_identifiers/
client.rs

1//! OAuth client identifier with classifier helpers.
2
3crate::define_id!(ClientId);
4
5impl ClientId {
6    pub fn client_type(&self) -> ClientType {
7        if self.0.starts_with("https://") {
8            ClientType::Cimd
9        } else if self.0.starts_with("sp_") {
10            ClientType::FirstParty
11        } else if self.0.starts_with("client_") {
12            ClientType::ThirdParty
13        } else if self.0.starts_with("sys_") {
14            ClientType::System
15        } else {
16            ClientType::Unknown
17        }
18    }
19
20    pub fn is_dcr(&self) -> bool {
21        matches!(
22            self.client_type(),
23            ClientType::FirstParty | ClientType::ThirdParty
24        )
25    }
26
27    pub fn is_cimd(&self) -> bool {
28        self.0.starts_with("https://")
29    }
30
31    pub fn is_system(&self) -> bool {
32        self.0.starts_with("sys_")
33    }
34
35    pub fn web() -> Self {
36        Self("sp_web".to_owned())
37    }
38
39    pub fn cli() -> Self {
40        Self("sp_cli".to_owned())
41    }
42
43    pub fn mobile_ios() -> Self {
44        Self("sp_mobile_ios".to_owned())
45    }
46
47    pub fn mobile_android() -> Self {
48        Self("sp_mobile_android".to_owned())
49    }
50
51    pub fn desktop() -> Self {
52        Self("sp_desktop".to_owned())
53    }
54
55    pub fn bridge() -> Self {
56        Self("sp_bridge".to_owned())
57    }
58
59    pub fn sync() -> Self {
60        Self("sys_sync".to_owned())
61    }
62
63    pub fn system(service_name: &str) -> Self {
64        Self(format!("sys_{service_name}"))
65    }
66}
67
68#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
69#[serde(rename_all = "lowercase")]
70pub enum ClientType {
71    Cimd,
72    FirstParty,
73    ThirdParty,
74    System,
75    Unknown,
76}
77
78impl ClientType {
79    pub const fn as_str(self) -> &'static str {
80        match self {
81            Self::Cimd => "cimd",
82            Self::FirstParty => "firstparty",
83            Self::ThirdParty => "thirdparty",
84            Self::System => "system",
85            Self::Unknown => "unknown",
86        }
87    }
88}
89
90impl std::fmt::Display for ClientType {
91    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92        write!(f, "{}", self.as_str())
93    }
94}