djs_model/application_role_connection_metadata.rs
1//! [Application role connection metadata](https://docs.discord.com/developers/resources/application-role-connection-metadata):
2//! the fields an app defines for linked roles.
3//!
4//! An app with a `role_connections_verification_url` declares up to 5 metadata records. Guilds
5//! then require values for them on a role (e.g. "account age ≥ 30 days"), and the app stores
6//! each user's values as an [`ApplicationRoleConnection`](crate::user::ApplicationRoleConnection).
7
8use std::collections::BTreeMap;
9
10use compact_str::CompactString;
11use serde::{Deserialize, Serialize};
12
13use crate::{macros::int_enum, util::Locale};
14
15/// An [application role connection metadata record](https://docs.discord.com/developers/resources/application-role-connection-metadata#application-role-connection-metadata-object):
16/// one field that guilds can compare against when granting a linked role.
17///
18/// discord.js counterpart: `ApplicationRoleConnectionMetadata`.
19#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
20#[non_exhaustive]
21pub struct ApplicationRoleConnectionMetadata {
22 /// How the user's value is compared with the guild's configured value.
23 #[serde(rename = "type")]
24 pub kind: MetadataType,
25 /// The dictionary key of the field (`a-z`, `0-9` and `_`; 1-50 characters).
26 pub key: CompactString,
27 /// The field's name (1-100 characters).
28 pub name: CompactString,
29 /// Translations of the name, keyed by locale (D-058).
30 #[serde(
31 default,
32 skip_serializing_if = "BTreeMap::is_empty",
33 deserialize_with = "crate::serde_util::null_as_default"
34 )]
35 pub name_localizations: BTreeMap<Locale, CompactString>,
36 /// The field's description (1-200 characters).
37 pub description: CompactString,
38 /// Translations of the description, keyed by locale (D-058).
39 #[serde(
40 default,
41 skip_serializing_if = "BTreeMap::is_empty",
42 deserialize_with = "crate::serde_util::null_as_default"
43 )]
44 pub description_localizations: BTreeMap<Locale, CompactString>,
45}
46
47impl ApplicationRoleConnectionMetadata {
48 /// A record without translations.
49 ///
50 /// discord.js counterpart: `ApplicationRoleConnectionMetadataEditOptions`.
51 ///
52 /// # Examples
53 ///
54 /// ```
55 /// use djs_model::application_role_connection_metadata::{
56 /// ApplicationRoleConnectionMetadata, MetadataType,
57 /// };
58 ///
59 /// let record = ApplicationRoleConnectionMetadata::new(
60 /// MetadataType::IntegerGreaterThanOrEqual,
61 /// "level",
62 /// "Level",
63 /// "The player's level",
64 /// );
65 /// assert_eq!(
66 /// serde_json::to_string(&record)?,
67 /// r#"{"type":2,"key":"level","name":"Level","description":"The player's level"}"#
68 /// );
69 /// # Ok::<(), serde_json::Error>(())
70 /// ```
71 #[must_use]
72 pub fn new(
73 kind: MetadataType,
74 key: impl Into<CompactString>,
75 name: impl Into<CompactString>,
76 description: impl Into<CompactString>,
77 ) -> Self {
78 Self {
79 kind,
80 key: key.into(),
81 name: name.into(),
82 name_localizations: BTreeMap::new(),
83 description: description.into(),
84 description_localizations: BTreeMap::new(),
85 }
86 }
87}
88
89int_enum! {
90 /// The [comparison](https://docs.discord.com/developers/resources/application-role-connection-metadata#application-role-connection-metadata-object-application-role-connection-metadata-type)
91 /// of an [`ApplicationRoleConnectionMetadata`] record.
92 ///
93 /// The user's value is stored as a string in
94 /// [`ApplicationRoleConnection::metadata`](crate::user::ApplicationRoleConnection::metadata):
95 /// an integer for the integer and boolean (`1`/`0`) comparisons, an ISO 8601 timestamp for
96 /// the date-time ones (the guild configures a number of days before today).
97 ///
98 /// discord.js counterpart: `ApplicationRoleConnectionMetadataType`.
99 #[doc(alias = "ApplicationRoleConnectionMetadataType")]
100 pub enum MetadataType: u8 {
101 /// The user's integer is less than or equal to the guild's.
102 IntegerLessThanOrEqual = 1,
103 /// The user's integer is greater than or equal to the guild's.
104 IntegerGreaterThanOrEqual = 2,
105 /// The user's integer is equal to the guild's.
106 IntegerEqual = 3,
107 /// The user's integer is not equal to the guild's.
108 IntegerNotEqual = 4,
109 /// The user's date-time is at most the guild's number of days before today.
110 DatetimeLessThanOrEqual = 5,
111 /// The user's date-time is at least the guild's number of days before today.
112 DatetimeGreaterThanOrEqual = 6,
113 /// The user's boolean is `1` (true).
114 BooleanEqual = 7,
115 /// The user's boolean is not `1` (true).
116 BooleanNotEqual = 8,
117 }
118}