risingwave_license/
key.rs1use std::convert::Infallible;
16use std::str::FromStr;
17
18use serde::{Deserialize, Serialize};
19
20pub(crate) const TEST_PAID_LICENSE_KEY_CONTENT: &str = "eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.\
33 eyJzdWIiOiJydy10ZXN0IiwidGllciI6InBhaWQiLCJpc3MiOiJ0ZXN0LnJpc2luZ3dhdmUuY29tIiwiZXhwIjo5OTk5OTk5OTk5fQ.\
34 c6Gmb6xh3dBDYX_4cOnHUbwRXJbUCM7W3mrJA77nLC5FkoOLpGstzvQ7qfnPVBu412MFtKRDvh-Lk8JwG7pVa0WLw16DeHTtVHxZukMTZ1Q_ciZ1xKeUx_pwUldkVzv6c9j99gNqPSyTjzOXTdKlidBRLer2zP0v3Lf-ZxnMG0tEcIbTinTb3BNCtAQ8bwBSRP-X48cVTWafjaZxv_zGiJT28uV3bR6jwrorjVB4VGvqhsJi6Fd074XOmUlnOleoAtyzKvjmGC5_FvnL0ztIe_I0z_pyCMfWpyJ_J4C7rCP1aVWUImyoowLmVDA-IKjclzOW5Fvi0wjXsc6OckOc_A";
35
36pub(crate) const PROD_PAID_4_CORE_LICENSE_KEY_CONTENT: &str = "eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.\
52 eyJzdWIiOiJydy1kZWZhdWx0LXBhaWQtNC1jb3JlIiwiaXNzIjoicHJvZC5yaXNpbmd3YXZlLmNvbSIsInRpZXIiOiJwYWlkIiwiZXhwIjoyMTQ3NDcxOTk5LCJpYXQiOjE3Mzc3MDQxMjQsImNwdV9jb3JlX2xpbWl0Ijo0fQ.\
53 BvCClH6vb_TH-UHKLK76nSP0RfuJDF8ay0WHBpaJFWTVt_phcl9claWPWWk6KTpj_5eJi-TWTDzThE2JKsHjRk9Uo48MtZcOUBZsGsc_NUyShRjd1DS9LmzzI6ouwEWO5BfMFxQ4ZuJFRcQP7_EtC5vHVGILXCThOE--Cj1YLz5rC4mi6WMNdgfWAmnJh6FtfruHvqQEqq8m23CuosS8XHG5DMOIwdmP9jCHYFtJQaYNOQVQW90vHp69Uqmcv8lZD57rUvrQYFGyekERg2JWlMWar2z2vyiN4u73Qje7MJ3EB9pkXE0wvAfJ3bPpATgKd96SxCJL1kYPeCJkVdFPQg";
54
55#[derive(Clone, Copy, Deserialize)]
64#[serde(transparent)]
65pub struct LicenseKey<T = String>(pub(crate) T);
66
67pub type LicenseKeyRef<'a> = LicenseKey<&'a str>;
69
70impl<T: From<&'static str>> Default for LicenseKey<T> {
71 fn default() -> Self {
72 Self(
73 if cfg!(debug_assertions) {
74 TEST_PAID_LICENSE_KEY_CONTENT
75 } else {
76 PROD_PAID_4_CORE_LICENSE_KEY_CONTENT
77 }
78 .into(),
79 )
80 }
81}
82
83impl<T> From<T> for LicenseKey<T> {
84 fn from(t: T) -> Self {
85 Self(t)
86 }
87}
88
89impl<A, B> PartialEq<LicenseKey<B>> for LicenseKey<A>
90where
91 A: AsRef<str>,
92 B: AsRef<str>,
93{
94 fn eq(&self, other: &LicenseKey<B>) -> bool {
95 self.0.as_ref() == other.0.as_ref()
96 }
97}
98
99impl<T: AsRef<str>> Eq for LicenseKey<T> {}
100
101impl<T: AsRef<str>> LicenseKey<T> {
102 fn redact_str(&self) -> &str {
103 let s = self.0.as_ref();
104 if s.is_empty() {
105 ""
106 } else if self == &LicenseKeyRef::default() {
107 "<default>"
108 } else {
109 "<redacted>"
110 }
111 }
112}
113
114impl<T: AsRef<str>> std::fmt::Debug for LicenseKey<T> {
115 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
116 write!(f, "{}", self.redact_str())
117 }
118}
119
120impl<T: AsRef<str>> std::fmt::Display for LicenseKey<T> {
121 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
122 std::fmt::Debug::fmt(self, f)
123 }
124}
125
126impl<T: AsRef<str>> Serialize for LicenseKey<T> {
127 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
128 where
129 S: serde::Serializer,
130 {
131 self.redact_str().serialize(serializer)
132 }
133}
134
135impl FromStr for LicenseKey {
136 type Err = Infallible;
137
138 fn from_str(s: &str) -> Result<Self, Self::Err> {
139 Ok(Self(s.to_owned()))
140 }
141}
142
143impl<T: AsRef<str>> From<LicenseKey<T>> for String {
144 fn from(val: LicenseKey<T>) -> Self {
145 val.0.as_ref().to_owned()
146 }
147}
148
149impl<'a> From<LicenseKeyRef<'a>> for LicenseKey {
150 fn from(t: LicenseKeyRef<'a>) -> Self {
151 Self(t.0.to_owned())
152 }
153}
154
155impl LicenseKey {
156 pub fn empty() -> Self {
158 Self(String::new())
159 }
160
161 pub fn as_ref(&self) -> LicenseKeyRef<'_> {
163 LicenseKey(self.0.as_ref())
164 }
165}