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