1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#[cfg(feature = "fp-bindgen")]
use fp_bindgen::prelude::Serializable;
use serde::{Deserialize, Serialize};

/// Defines a field that allows integer numbers to be entered.
#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::providers")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct IntegerField {
    /// Name of the field as it will be included in the encoded query or config
    /// object.
    pub name: String,

    /// Suggested label to display along the field.
    pub label: String,

    /// Optional maximum value to be entered.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max: Option<i32>,

    /// Optional minimal value to be entered.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub min: Option<i32>,

    /// Suggested placeholder to display when there is no value.
    pub placeholder: String,

    /// Whether a value is required.
    pub required: bool,

    /// Specifies the granularity that any specified numbers must adhere to.
    ///
    /// If omitted, `step` defaults to "1", meaning only integers are allowed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub step: Option<i32>,
}

impl IntegerField {
    /// Creates a new integer field with all default values.
    pub fn new() -> Self {
        Default::default()
    }

    /// Marks the field as required.
    pub fn required(self) -> Self {
        Self {
            required: true,
            ..self
        }
    }

    /// Convenience method for setting `min` and `max` together.
    pub fn with_bounds(self, min: i32, max: i32) -> Self {
        Self {
            max: Some(max),
            min: Some(min),
            ..self
        }
    }

    pub fn with_label(self, label: impl Into<String>) -> Self {
        Self {
            label: label.into(),
            ..self
        }
    }

    pub fn with_max(self, max: i32) -> Self {
        Self {
            max: Some(max),
            ..self
        }
    }

    pub fn with_min(self, min: i32) -> Self {
        Self {
            min: Some(min),
            ..self
        }
    }

    pub fn with_name(self, name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            ..self
        }
    }

    pub fn with_step(self, step: i32) -> Self {
        Self {
            step: Some(step),
            ..self
        }
    }
}