Skip to main content

fiberplane_models/providers/schema/fields/
integer_field.rs

1#[cfg(feature = "fp-bindgen")]
2use fp_bindgen::prelude::Serializable;
3use serde::{Deserialize, Serialize};
4
5/// Defines a field that allows integer numbers to be entered.
6#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
7#[cfg_attr(
8    feature = "fp-bindgen",
9    derive(Serializable),
10    fp(rust_module = "fiberplane_models::providers")
11)]
12#[non_exhaustive]
13#[serde(rename_all = "camelCase")]
14pub struct IntegerField {
15    /// Name of the field as it will be included in the encoded query or config
16    /// object.
17    pub name: String,
18
19    /// Suggested label to display along the field.
20    pub label: String,
21
22    /// Optional maximum value to be entered.
23    #[serde(default, skip_serializing_if = "Option::is_none")]
24    pub max: Option<i32>,
25
26    /// Optional minimal value to be entered.
27    #[serde(default, skip_serializing_if = "Option::is_none")]
28    pub min: Option<i32>,
29
30    /// Suggested placeholder to display when there is no value.
31    pub placeholder: String,
32
33    /// Whether a value is required.
34    pub required: bool,
35
36    /// Specifies the granularity that any specified numbers must adhere to.
37    ///
38    /// If omitted, `step` defaults to "1", meaning only integers are allowed.
39    #[serde(default, skip_serializing_if = "Option::is_none")]
40    pub step: Option<i32>,
41}
42
43impl IntegerField {
44    /// Creates a new integer field with all default values.
45    pub fn new() -> Self {
46        Default::default()
47    }
48
49    /// Marks the field as required.
50    pub fn required(self) -> Self {
51        Self {
52            required: true,
53            ..self
54        }
55    }
56
57    /// Convenience method for setting `min` and `max` together.
58    pub fn with_bounds(self, min: i32, max: i32) -> Self {
59        Self {
60            max: Some(max),
61            min: Some(min),
62            ..self
63        }
64    }
65
66    pub fn with_label(self, label: impl Into<String>) -> Self {
67        Self {
68            label: label.into(),
69            ..self
70        }
71    }
72
73    pub fn with_max(self, max: i32) -> Self {
74        Self {
75            max: Some(max),
76            ..self
77        }
78    }
79
80    pub fn with_min(self, min: i32) -> Self {
81        Self {
82            min: Some(min),
83            ..self
84        }
85    }
86
87    pub fn with_name(self, name: impl Into<String>) -> Self {
88        Self {
89            name: name.into(),
90            ..self
91        }
92    }
93
94    pub fn with_step(self, step: i32) -> Self {
95        Self {
96            step: Some(step),
97            ..self
98        }
99    }
100}