1pub mod get_valid_badges;
9
10
11#[allow(unused_imports)]
12use alloc::collections::BTreeMap;
13
14#[allow(unused_imports)]
15use core::marker::PhantomData;
16use jacquard_common::CowStr;
17
18#[allow(unused_imports)]
19use jacquard_common::deps::codegen::unicode_segmentation::UnicodeSegmentation;
20use jacquard_common::types::string::Did;
21use jacquard_derive::{IntoStatic, lexicon};
22use jacquard_lexicon::lexicon::LexiconDoc;
23use jacquard_lexicon::schema::LexiconSchema;
24
25#[allow(unused_imports)]
26use jacquard_lexicon::validation::{ConstraintError, ValidationPath};
27use serde::{Serialize, Deserialize};
28#[lexicon]
31#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
32#[serde(rename_all = "camelCase")]
33pub struct BadgeView<'a> {
34 #[serde(borrow)]
35 pub badge_type: BadgeViewBadgeType<'a>,
36 #[serde(borrow)]
38 pub issuer: Did<'a>,
39 #[serde(borrow)]
41 pub recipient: Did<'a>,
42 #[serde(skip_serializing_if = "Option::is_none")]
44 #[serde(borrow)]
45 pub signature: Option<CowStr<'a>>,
46}
47
48
49#[derive(Debug, Clone, PartialEq, Eq, Hash)]
50pub enum BadgeViewBadgeType<'a> {
51 Mod,
52 Streamer,
53 Other(CowStr<'a>),
54}
55
56impl<'a> BadgeViewBadgeType<'a> {
57 pub fn as_str(&self) -> &str {
58 match self {
59 Self::Mod => "place.stream.badge.defs#mod",
60 Self::Streamer => "place.stream.badge.defs#streamer",
61 Self::Other(s) => s.as_ref(),
62 }
63 }
64}
65
66impl<'a> From<&'a str> for BadgeViewBadgeType<'a> {
67 fn from(s: &'a str) -> Self {
68 match s {
69 "place.stream.badge.defs#mod" => Self::Mod,
70 "place.stream.badge.defs#streamer" => Self::Streamer,
71 _ => Self::Other(CowStr::from(s)),
72 }
73 }
74}
75
76impl<'a> From<String> for BadgeViewBadgeType<'a> {
77 fn from(s: String) -> Self {
78 match s.as_str() {
79 "place.stream.badge.defs#mod" => Self::Mod,
80 "place.stream.badge.defs#streamer" => Self::Streamer,
81 _ => Self::Other(CowStr::from(s)),
82 }
83 }
84}
85
86impl<'a> core::fmt::Display for BadgeViewBadgeType<'a> {
87 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
88 write!(f, "{}", self.as_str())
89 }
90}
91
92impl<'a> AsRef<str> for BadgeViewBadgeType<'a> {
93 fn as_ref(&self) -> &str {
94 self.as_str()
95 }
96}
97
98impl<'a> serde::Serialize for BadgeViewBadgeType<'a> {
99 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
100 where
101 S: serde::Serializer,
102 {
103 serializer.serialize_str(self.as_str())
104 }
105}
106
107impl<'de, 'a> serde::Deserialize<'de> for BadgeViewBadgeType<'a>
108where
109 'de: 'a,
110{
111 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
112 where
113 D: serde::Deserializer<'de>,
114 {
115 let s = <&'de str>::deserialize(deserializer)?;
116 Ok(Self::from(s))
117 }
118}
119
120impl<'a> Default for BadgeViewBadgeType<'a> {
121 fn default() -> Self {
122 Self::Other(Default::default())
123 }
124}
125
126impl jacquard_common::IntoStatic for BadgeViewBadgeType<'_> {
127 type Output = BadgeViewBadgeType<'static>;
128 fn into_static(self) -> Self::Output {
129 match self {
130 BadgeViewBadgeType::Mod => BadgeViewBadgeType::Mod,
131 BadgeViewBadgeType::Streamer => BadgeViewBadgeType::Streamer,
132 BadgeViewBadgeType::Other(v) => BadgeViewBadgeType::Other(v.into_static()),
133 }
134 }
135}
136
137#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Hash)]
140pub struct Mod;
141impl core::fmt::Display for Mod {
142 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
143 write!(f, "mod")
144 }
145}
146
147#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Hash)]
150pub struct Streamer;
151impl core::fmt::Display for Streamer {
152 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
153 write!(f, "streamer")
154 }
155}
156
157#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Hash)]
160pub struct Vip;
161impl core::fmt::Display for Vip {
162 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
163 write!(f, "vip")
164 }
165}
166
167impl<'a> LexiconSchema for BadgeView<'a> {
168 fn nsid() -> &'static str {
169 "place.stream.badge.defs"
170 }
171 fn def_name() -> &'static str {
172 "badgeView"
173 }
174 fn lexicon_doc() -> LexiconDoc<'static> {
175 lexicon_doc_place_stream_badge_defs()
176 }
177 fn validate(&self) -> Result<(), ConstraintError> {
178 Ok(())
179 }
180}
181
182pub mod badge_view_state {
183
184 pub use crate::builder_types::{Set, Unset, IsSet, IsUnset};
185 #[allow(unused)]
186 use ::core::marker::PhantomData;
187 mod sealed {
188 pub trait Sealed {}
189 }
190 pub trait State: sealed::Sealed {
192 type Issuer;
193 type Recipient;
194 type BadgeType;
195 }
196 pub struct Empty(());
198 impl sealed::Sealed for Empty {}
199 impl State for Empty {
200 type Issuer = Unset;
201 type Recipient = Unset;
202 type BadgeType = Unset;
203 }
204 pub struct SetIssuer<S: State = Empty>(PhantomData<fn() -> S>);
206 impl<S: State> sealed::Sealed for SetIssuer<S> {}
207 impl<S: State> State for SetIssuer<S> {
208 type Issuer = Set<members::issuer>;
209 type Recipient = S::Recipient;
210 type BadgeType = S::BadgeType;
211 }
212 pub struct SetRecipient<S: State = Empty>(PhantomData<fn() -> S>);
214 impl<S: State> sealed::Sealed for SetRecipient<S> {}
215 impl<S: State> State for SetRecipient<S> {
216 type Issuer = S::Issuer;
217 type Recipient = Set<members::recipient>;
218 type BadgeType = S::BadgeType;
219 }
220 pub struct SetBadgeType<S: State = Empty>(PhantomData<fn() -> S>);
222 impl<S: State> sealed::Sealed for SetBadgeType<S> {}
223 impl<S: State> State for SetBadgeType<S> {
224 type Issuer = S::Issuer;
225 type Recipient = S::Recipient;
226 type BadgeType = Set<members::badge_type>;
227 }
228 #[allow(non_camel_case_types)]
230 pub mod members {
231 pub struct issuer(());
233 pub struct recipient(());
235 pub struct badge_type(());
237 }
238}
239
240pub struct BadgeViewBuilder<'a, S: badge_view_state::State> {
242 _state: PhantomData<fn() -> S>,
243 _fields: (
244 Option<BadgeViewBadgeType<'a>>,
245 Option<Did<'a>>,
246 Option<Did<'a>>,
247 Option<CowStr<'a>>,
248 ),
249 _lifetime: PhantomData<&'a ()>,
250}
251
252impl<'a> BadgeView<'a> {
253 pub fn new() -> BadgeViewBuilder<'a, badge_view_state::Empty> {
255 BadgeViewBuilder::new()
256 }
257}
258
259impl<'a> BadgeViewBuilder<'a, badge_view_state::Empty> {
260 pub fn new() -> Self {
262 BadgeViewBuilder {
263 _state: PhantomData,
264 _fields: (None, None, None, None),
265 _lifetime: PhantomData,
266 }
267 }
268}
269
270impl<'a, S> BadgeViewBuilder<'a, S>
271where
272 S: badge_view_state::State,
273 S::BadgeType: badge_view_state::IsUnset,
274{
275 pub fn badge_type(
277 mut self,
278 value: impl Into<BadgeViewBadgeType<'a>>,
279 ) -> BadgeViewBuilder<'a, badge_view_state::SetBadgeType<S>> {
280 self._fields.0 = Option::Some(value.into());
281 BadgeViewBuilder {
282 _state: PhantomData,
283 _fields: self._fields,
284 _lifetime: PhantomData,
285 }
286 }
287}
288
289impl<'a, S> BadgeViewBuilder<'a, S>
290where
291 S: badge_view_state::State,
292 S::Issuer: badge_view_state::IsUnset,
293{
294 pub fn issuer(
296 mut self,
297 value: impl Into<Did<'a>>,
298 ) -> BadgeViewBuilder<'a, badge_view_state::SetIssuer<S>> {
299 self._fields.1 = Option::Some(value.into());
300 BadgeViewBuilder {
301 _state: PhantomData,
302 _fields: self._fields,
303 _lifetime: PhantomData,
304 }
305 }
306}
307
308impl<'a, S> BadgeViewBuilder<'a, S>
309where
310 S: badge_view_state::State,
311 S::Recipient: badge_view_state::IsUnset,
312{
313 pub fn recipient(
315 mut self,
316 value: impl Into<Did<'a>>,
317 ) -> BadgeViewBuilder<'a, badge_view_state::SetRecipient<S>> {
318 self._fields.2 = Option::Some(value.into());
319 BadgeViewBuilder {
320 _state: PhantomData,
321 _fields: self._fields,
322 _lifetime: PhantomData,
323 }
324 }
325}
326
327impl<'a, S: badge_view_state::State> BadgeViewBuilder<'a, S> {
328 pub fn signature(mut self, value: impl Into<Option<CowStr<'a>>>) -> Self {
330 self._fields.3 = value.into();
331 self
332 }
333 pub fn maybe_signature(mut self, value: Option<CowStr<'a>>) -> Self {
335 self._fields.3 = value;
336 self
337 }
338}
339
340impl<'a, S> BadgeViewBuilder<'a, S>
341where
342 S: badge_view_state::State,
343 S::Issuer: badge_view_state::IsSet,
344 S::Recipient: badge_view_state::IsSet,
345 S::BadgeType: badge_view_state::IsSet,
346{
347 pub fn build(self) -> BadgeView<'a> {
349 BadgeView {
350 badge_type: self._fields.0.unwrap(),
351 issuer: self._fields.1.unwrap(),
352 recipient: self._fields.2.unwrap(),
353 signature: self._fields.3,
354 extra_data: Default::default(),
355 }
356 }
357 pub fn build_with_data(
359 self,
360 extra_data: BTreeMap<
361 jacquard_common::deps::smol_str::SmolStr,
362 jacquard_common::types::value::Data<'a>,
363 >,
364 ) -> BadgeView<'a> {
365 BadgeView {
366 badge_type: self._fields.0.unwrap(),
367 issuer: self._fields.1.unwrap(),
368 recipient: self._fields.2.unwrap(),
369 signature: self._fields.3,
370 extra_data: Some(extra_data),
371 }
372 }
373}
374
375fn lexicon_doc_place_stream_badge_defs() -> LexiconDoc<'static> {
376 #[allow(unused_imports)]
377 use jacquard_common::{CowStr, deps::smol_str::SmolStr, types::blob::MimeType};
378 use jacquard_lexicon::lexicon::*;
379 use alloc::collections::BTreeMap;
380 LexiconDoc {
381 lexicon: Lexicon::Lexicon1,
382 id: CowStr::new_static("place.stream.badge.defs"),
383 defs: {
384 let mut map = BTreeMap::new();
385 map.insert(
386 SmolStr::new_static("badgeView"),
387 LexUserType::Object(LexObject {
388 description: Some(
389 CowStr::new_static(
390 "View of a badge record, with fields resolved for display. If the DID in issuer is not the current streamplace node, the signature field shall be required.",
391 ),
392 ),
393 required: Some(
394 vec![
395 SmolStr::new_static("badgeType"),
396 SmolStr::new_static("issuer"),
397 SmolStr::new_static("recipient")
398 ],
399 ),
400 properties: {
401 #[allow(unused_mut)]
402 let mut map = BTreeMap::new();
403 map.insert(
404 SmolStr::new_static("badgeType"),
405 LexObjectProperty::String(LexString { ..Default::default() }),
406 );
407 map.insert(
408 SmolStr::new_static("issuer"),
409 LexObjectProperty::String(LexString {
410 description: Some(
411 CowStr::new_static("DID of the badge issuer."),
412 ),
413 format: Some(LexStringFormat::Did),
414 ..Default::default()
415 }),
416 );
417 map.insert(
418 SmolStr::new_static("recipient"),
419 LexObjectProperty::String(LexString {
420 description: Some(
421 CowStr::new_static("DID of the badge recipient."),
422 ),
423 format: Some(LexStringFormat::Did),
424 ..Default::default()
425 }),
426 );
427 map.insert(
428 SmolStr::new_static("signature"),
429 LexObjectProperty::String(LexString {
430 description: Some(
431 CowStr::new_static(
432 "TODO: Cryptographic signature of the badge (of a place.stream.key).",
433 ),
434 ),
435 ..Default::default()
436 }),
437 );
438 map
439 },
440 ..Default::default()
441 }),
442 );
443 map.insert(
444 SmolStr::new_static("mod"),
445 LexUserType::Token(LexToken { ..Default::default() }),
446 );
447 map.insert(
448 SmolStr::new_static("streamer"),
449 LexUserType::Token(LexToken { ..Default::default() }),
450 );
451 map.insert(
452 SmolStr::new_static("vip"),
453 LexUserType::Token(LexToken { ..Default::default() }),
454 );
455 map
456 },
457 ..Default::default()
458 }
459}