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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use crate::{prelude::*, util::*};
use serde::{Deserialize, Serialize};
/// Represents a Time Zone in Opsview.
///
/// These are not used directly in Opsview, or configured via the Opsview API, but are used to
/// represent the `TimeZone` of a [`super::TimePeriod`] object.
#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
pub struct TimeZone {
// TODO: Add validation of this field.
/// The name of the `TimeZone`.
pub name: String,
/// Unique reference string unique to this time zone.
#[serde(rename = "ref")]
pub ref_: String,
}
/// Enables the creation of a `TimeZone` instance from a JSON representation.
/// Typically used when parsing JSON data from the Opsview API.
impl CreateFromJson for TimeZone {}
impl ConfigObject for TimeZone {
type Builder = TimeZoneBuilder;
/// Returns a builder for constructing a `TimeZone` object.
///
/// # Returns
/// A `TimeZoneBuilder` object.
fn builder() -> Self::Builder {
TimeZoneBuilder::new()
}
/// Returns the unique name of the `TimeZone` object.
///
/// This name is used to identify the `TimeZone` when building the `HashMap` for an
/// [`ConfigObjectMap`].
///
/// # Returns
/// A string representing the unique name of the `TimeZone`.
fn unique_name(&self) -> String {
self.ref_.clone()
}
fn minimal(name: &str) -> Result<Self, OpsviewConfigError> {
Ok(Self {
name: name.to_string(),
..Default::default()
})
}
}
/// Builder for `TimeZone` objects, used to simplify the creation of new instances.
///
/// # Example
/// ```rust
/// use opsview::config::TimeZone;
/// use opsview::prelude::*;
///
/// let timezone = TimeZone::builder()
/// .name("SYSTEM")
/// .ref_("/rest/config/timezone/1")
/// .build()
/// .unwrap();
///
/// assert_eq!(timezone.name, "SYSTEM".to_string());
/// assert_eq!(timezone.ref_, "/rest/config/timezone/1".to_string());
/// ```
#[derive(Clone, Debug, Default)]
pub struct TimeZoneBuilder {
name: Option<String>,
ref_: Option<String>,
}
impl Builder for TimeZoneBuilder {
type ConfigObject = TimeZone;
/// Creates a new `TimeZoneBuilder` instance with default values.
///
/// # Returns
/// A TimeZoneBuilder instance.
fn new() -> Self {
Self::default()
}
/// Sets the name field.
///
/// # Arguments
/// * `name` - The name of the `TimeZone`.
fn name(mut self, name: &str) -> Self {
self.name = Some(name.to_string());
self
}
/// Builds a new `TimeZone` instance using the `TimeZoneBuilder`.
///
/// # Returns
/// A `TimeZone` instance.
///
/// # Errors
/// If the name field is not set, an error will be returned.
fn build(self) -> Result<Self::ConfigObject, OpsviewConfigError> {
let name = require_field(&self.name, "name")?;
let ref_ = require_field(&self.ref_, "ref_")?;
Ok(TimeZone { name, ref_ })
}
}
impl TimeZoneBuilder {
/// Clears the name field.
pub fn clear_name(mut self) -> Self {
self.name = None;
self
}
/// Clears the ref_ field.
pub fn clear_ref_(mut self) -> Self {
self.ref_ = None;
self
}
/// Sets the ref_ field.
///
/// # Arguments
/// * `ref_` - The reference string for the `TimeZone`.
pub fn ref_(mut self, ref_: &str) -> Self {
self.ref_ = Some(ref_.to_string());
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn test_timezone_default() {
let timezone = TimeZone::default();
assert_eq!(timezone.name, "".to_string());
assert_eq!(timezone.ref_, "".to_string());
}
#[test]
fn test_timezone_minimal() {
let timezone =
TimeZone::minimal("SYSTEM").expect("Failed to create TimeZone with name 'SYSTEM'");
assert_eq!(timezone.name, "SYSTEM".to_string());
assert_eq!(timezone.ref_, "".to_string());
}
#[test]
fn test_timezone_unique_name() {
let timezone = TimeZone::minimal("SYSTEM");
assert_eq!(timezone.unwrap().unique_name(), "".to_string());
}
#[test]
fn test_timezone_builder() {
let timezone = TimeZone::builder()
.name("SYSTEM")
.ref_("/rest/config/timezone/1")
.build()
.unwrap();
assert_eq!(timezone.name, "SYSTEM".to_string());
assert_eq!(timezone.ref_, "/rest/config/timezone/1".to_string());
}
#[test]
fn test_timezone_builder_missing_name() {
let timezone = TimeZone::builder().ref_("/rest/config/timezone/1").build();
assert!(timezone.is_err());
assert_eq!(
timezone.unwrap_err().to_string(),
"Mandatory field 'name' cannot be empty"
);
}
#[test]
fn test_timezone_builder_missing_ref() {
let timezone = TimeZone::builder().name("SYSTEM").build();
assert!(timezone.is_err());
assert_eq!(
timezone.unwrap_err().to_string(),
"Mandatory field 'ref_' cannot be empty"
);
}
}