twilight_model/channel/message/component/
text_input.rs

1use serde_repr::{Deserialize_repr, Serialize_repr};
2
3/// Pop-up [`Component`] that renders on modals.
4///
5/// [`Component`]: super::Component
6#[derive(Clone, Debug, Eq, Hash, PartialEq)]
7pub struct TextInput {
8    /// Optional id for the text input.
9    pub id: Option<i32>,
10    /// User defined identifier for the input text.
11    pub custom_id: String,
12    /// Text appearing over the input field.
13    #[deprecated = "Deprecated by Discord in favor of label and description on the Label component."]
14    pub label: Option<String>,
15    /// The maximum length of the text.
16    pub max_length: Option<u16>,
17    /// The minimum length of the text.
18    ///
19    /// Defaults to `0`.
20    pub min_length: Option<u16>,
21    /// Placeholder for the text input.
22    pub placeholder: Option<String>,
23    /// Whether the user is required to input a text.
24    ///
25    /// Defaults to `true`.
26    pub required: Option<bool>,
27    /// Style variant of the input text.
28    pub style: TextInputStyle,
29    /// Pre-filled value for input text.
30    pub value: Option<String>,
31}
32
33/// Style of an [`TextInput`].
34#[derive(Clone, Copy, Debug, Deserialize_repr, Eq, Hash, PartialEq, Serialize_repr)]
35#[non_exhaustive]
36#[repr(u8)]
37pub enum TextInputStyle {
38    /// Intended for short single-line text.
39    Short = 1,
40    /// Intended for much longer inputs.
41    Paragraph = 2,
42}
43
44#[cfg(test)]
45#[allow(deprecated)]
46mod tests {
47    use super::*;
48    use serde::{Deserialize, Serialize};
49    use serde_test::Token;
50    use static_assertions::{assert_fields, assert_impl_all, const_assert_eq};
51    use std::{fmt::Debug, hash::Hash};
52
53    assert_fields!(
54        TextInput: custom_id,
55        label,
56        style,
57        placeholder,
58        min_length,
59        max_length,
60        value
61    );
62    assert_impl_all!(TextInput: Clone, Debug, Eq, Hash, PartialEq, Send, Sync);
63
64    assert_impl_all!(
65        TextInputStyle: Clone,
66        Copy,
67        Debug,
68        Deserialize<'static>,
69        Eq,
70        Hash,
71        PartialEq,
72        Send,
73        Serialize,
74        Sync
75    );
76    const_assert_eq!(1, TextInputStyle::Short as u8);
77    const_assert_eq!(2, TextInputStyle::Paragraph as u8);
78
79    #[test]
80    fn text_input_style() {
81        serde_test::assert_tokens(&TextInputStyle::Short, &[Token::U8(1)]);
82        serde_test::assert_tokens(&TextInputStyle::Paragraph, &[Token::U8(2)]);
83    }
84}