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