fiberplane_models/providers/schema/fields/
checkbox_field.rs

1#[cfg(feature = "fp-bindgen")]
2use fp_bindgen::prelude::Serializable;
3use serde::{Deserialize, Serialize};
4
5/// Defines a field that produces a boolean value.
6///
7/// For JSON/YAML encoding, the value will be represented as a native boolean.
8/// In the case of "application/x-www-form-urlencoded", it will be represented
9/// by the value defined in the `value` field, which will be either present or
10/// not, similar to the encoding of HTML forms.
11#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
12#[cfg_attr(
13    feature = "fp-bindgen",
14    derive(Serializable),
15    fp(rust_module = "fiberplane_models::providers")
16)]
17#[non_exhaustive]
18#[serde(rename_all = "camelCase")]
19pub struct CheckboxField {
20    /// Whether the checkbox should be initially checked if no query data is
21    /// present.
22    pub checked: bool,
23
24    /// Suggested label to display along the checkbox.
25    pub label: String,
26
27    /// Name of the field as it will be included in the encoded query or config
28    /// object.
29    pub name: String,
30
31    /// Whether the checkbox must be checked.
32    ///
33    /// This allows for the use case of implementing Terms of Service checkboxes
34    /// in config forms.
35    pub required: bool,
36
37    /// Value of the field as it will be included in the encoded query. Note
38    /// that only checked checkboxes will be included.
39    ///
40    /// If the data is encoded using either JSON or YAML, the checkbox state is
41    /// encoded as a boolean and this value will not be used.
42    pub value: String,
43}
44
45impl CheckboxField {
46    /// Creates a new checkbox with all default values.
47    pub fn new() -> Self {
48        Default::default()
49    }
50
51    /// Marks the checkbox as being checked by default.
52    pub fn checked_by_default(self) -> Self {
53        Self {
54            checked: true,
55            ..self
56        }
57    }
58
59    /// Marks the field as required.
60    pub fn required(self) -> Self {
61        Self {
62            required: true,
63            ..self
64        }
65    }
66
67    pub fn with_label(self, label: &str) -> Self {
68        Self {
69            label: label.to_owned(),
70            ..self
71        }
72    }
73
74    pub fn with_name(self, name: &str) -> Self {
75        Self {
76            name: name.to_owned(),
77            ..self
78        }
79    }
80
81    pub fn with_value(self, value: &str) -> Self {
82        Self {
83            value: value.to_owned(),
84            ..self
85        }
86    }
87}