Skip to main content

fiberplane_models/providers/schema/fields/
select_field.rs

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