Skip to main content

fiberplane_models/providers/schema/fields/
text_field.rs

1#[cfg(feature = "fp-bindgen")]
2use fp_bindgen::prelude::Serializable;
3use serde::{Deserialize, Serialize};
4
5/// Defines a free-form text entry field.
6///
7/// This is commonly used for filter text and query entry.
8#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
9#[cfg_attr(
10    feature = "fp-bindgen",
11    derive(Serializable),
12    fp(rust_module = "fiberplane_models::providers")
13)]
14#[non_exhaustive]
15#[serde(rename_all = "camelCase")]
16pub struct TextField {
17    /// Suggested label to display along the form field.
18    pub label: String,
19
20    /// Whether multi-line input is useful for this provider.
21    pub multiline: bool,
22
23    /// Whether multiple values may be inserted.
24    pub multiple: bool,
25
26    /// Name of the field as it will be included in the encoded query or config
27    /// object.
28    pub name: String,
29
30    /// Suggested placeholder to display when there is no value.
31    pub placeholder: String,
32
33    /// An optional list of fields that should be filled in before allowing the
34    /// user to fill in this field. This forces a certain ordering in the data
35    /// entry, which enables richer auto-suggestions, since the filled in
36    /// prerequisite fields can provide additional context.
37    pub prerequisites: Vec<String>,
38
39    /// Whether a value is required.
40    pub required: bool,
41
42    /// Whether the provider supports auto-suggestions for this field.
43    pub supports_suggestions: bool,
44}
45
46impl TextField {
47    /// Creates a new text field with all default values.
48    pub fn new() -> Self {
49        Default::default()
50    }
51
52    /// Marks the field as allowing multi-line entry.
53    pub fn multiline(self) -> Self {
54        Self {
55            multiline: true,
56            ..self
57        }
58    }
59
60    /// Marks the field as allowing multiple values.
61    pub fn multiple(self) -> Self {
62        Self {
63            multiple: true,
64            ..self
65        }
66    }
67
68    /// Marks the field as required.
69    pub fn required(self) -> Self {
70        Self {
71            required: true,
72            ..self
73        }
74    }
75
76    pub fn with_label(self, label: impl Into<String>) -> Self {
77        Self {
78            label: label.into(),
79            ..self
80        }
81    }
82
83    pub fn with_name(self, name: impl Into<String>) -> Self {
84        Self {
85            name: name.into(),
86            ..self
87        }
88    }
89
90    pub fn with_placeholder(self, placeholder: impl Into<String>) -> Self {
91        Self {
92            placeholder: placeholder.into(),
93            ..self
94        }
95    }
96
97    pub fn with_prerequisites(self, prerequisites: impl Into<Vec<String>>) -> Self {
98        Self {
99            prerequisites: prerequisites.into(),
100            ..self
101        }
102    }
103
104    /// Marks the field as supporting auto-suggestions.
105    pub fn with_suggestions(self) -> Self {
106        Self {
107            supports_suggestions: true,
108            ..self
109        }
110    }
111}