systemprompt_identifiers/auth/
cloud_token.rs1use crate::{DbValue, ToDbValue};
4use serde::{Deserialize, Serialize};
5use std::fmt;
6
7#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
8#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
9#[cfg_attr(feature = "sqlx", sqlx(transparent))]
10#[serde(transparent)]
11pub struct CloudAuthToken(String);
12
13impl CloudAuthToken {
14 pub fn new(token: impl Into<String>) -> Self {
15 Self(token.into())
16 }
17
18 pub fn as_str(&self) -> &str {
19 &self.0
20 }
21
22 #[must_use]
23 pub fn redacted(&self) -> String {
24 let len = self.0.len();
25 if len <= 16 {
26 "*".repeat(len.min(8))
27 } else {
28 format!("{}...{}", &self.0[..8], &self.0[len - 4..])
29 }
30 }
31}
32
33impl fmt::Display for CloudAuthToken {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 write!(f, "{}", self.redacted())
36 }
37}
38
39impl From<String> for CloudAuthToken {
40 fn from(s: String) -> Self {
41 Self(s)
42 }
43}
44
45impl From<&str> for CloudAuthToken {
46 fn from(s: &str) -> Self {
47 Self(s.to_string())
48 }
49}
50
51impl AsRef<str> for CloudAuthToken {
52 fn as_ref(&self) -> &str {
53 &self.0
54 }
55}
56
57impl ToDbValue for CloudAuthToken {
58 fn to_db_value(&self) -> DbValue {
59 DbValue::String(self.0.clone())
60 }
61}
62
63impl ToDbValue for &CloudAuthToken {
64 fn to_db_value(&self) -> DbValue {
65 DbValue::String(self.0.clone())
66 }
67}