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
use activitystreams_vocabulary::{create_object, field_access};
use crate::HexColor;
mod field_type;
mod field_value;
pub use field_type::FieldType;
pub use field_value::{FieldValue, FieldValueItem};
create_object! {
/// Represents a custom ticket field within a [Workflow](crate::Workflow).
///
/// # Example (with flat `FieldValue`)
///
/// ```rust
/// use activityforge::{Field, FieldType, HexColor, context};
/// use activitystreams_vocabulary::Name;
///
/// # fn main() {
/// let name = Name::try_from("workflow custom field").unwrap();
/// let field_color = HexColor::new()
/// .with_red(0xaa)
/// .with_green(0xbb)
/// .with_blue(0xcc);
/// let field_type = FieldType::Text;
/// let value = "some field value";
///
/// let json_str = format!(
/// r#"{{
/// "@context": [
/// "https://www.w3.org/ns/activitystreams",
/// "https://forgefed.org/ns"
/// ],
/// "type": "Field",
/// "name": "{name}",
/// "fieldColor": "{field_color}",
/// "fieldType": "{field_type}",
/// "fieldValue": "{value}"
/// }}"#
/// );
///
/// let context = context::forgefed_context();
///
/// let field_value: serde_json::Value = value.to_string().into();
///
/// let field = Field::new()
/// .with_context_property(context)
/// .with_name(name)
/// .with_field_color(field_color)
/// .with_field_type(field_type)
/// .with_field_value(field_value);
///
/// assert_eq!(serde_json::to_string_pretty(&field).unwrap(), json_str);
/// assert_eq!(
/// serde_json::from_str::<Field>(json_str.as_str()).unwrap(),
/// field
/// );
/// # }
/// ```
///
/// # Example (with object `FieldValue`)
///
/// ```rust
/// use activityforge::{Field, FieldType, FieldValue, HexColor, context};
/// use activitystreams_vocabulary::Name;
///
/// # fn main() {
/// let name = Name::try_from("workflow custom field").unwrap();
/// let field_color = HexColor::new()
/// .with_red(0xaa)
/// .with_green(0xbb)
/// .with_blue(0xcc);
/// let field_type = FieldType::Text;
/// let value = "some field value";
///
/// let json_str = format!(
/// r#"{{
/// "@context": [
/// "https://www.w3.org/ns/activitystreams",
/// "https://forgefed.org/ns"
/// ],
/// "type": "Field",
/// "name": "{name}",
/// "fieldColor": "{field_color}",
/// "fieldType": "{field_type}",
/// "fieldValue": {{
/// "type": "FieldValue",
/// "fieldValue": "{value}"
/// }}
/// }}"#
/// );
///
/// let context = context::forgefed_context();
///
/// let field_value = FieldValue::new_inner()
/// .with_field_value(value.to_string());
///
/// let field = Field::new()
/// .with_context_property(context)
/// .with_name(name)
/// .with_field_color(field_color)
/// .with_field_type(field_type)
/// .with_field_value(field_value);
///
/// assert_eq!(serde_json::to_string_pretty(&field).unwrap(), json_str);
/// assert_eq!(
/// serde_json::from_str::<Field>(json_str.as_str()).unwrap(),
/// field
/// );
/// # }
/// ```
Field: crate::ObjectType::Field {
field_color: Option<HexColor>,
field_type: Option<FieldType>,
field_value: Option<FieldValueItem>,
}
}
field_access! {
Field {
field_color: option { HexColor },
field_type: option { FieldType },
}
}
field_access! {
Field {
field_value: option_ref { FieldValueItem },
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::context;
use activitystreams_vocabulary::Name;
#[test]
fn test_field() {
let name = Name::try_from("workflow custom field").unwrap();
let field_color = HexColor::new()
.with_red(0xaa)
.with_green(0xbb)
.with_blue(0xcc);
let field_type = FieldType::Text;
let value = "some field value";
let json_str = format!(
r#"{{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://forgefed.org/ns"
],
"type": "Field",
"name": "{name}",
"fieldColor": "{field_color}",
"fieldType": "{field_type}",
"fieldValue": "{value}"
}}"#
);
let context = context::forgefed_context();
let field_value: serde_json::Value = value.to_string().into();
let field = Field::new()
.with_context_property(context)
.with_name(name)
.with_field_color(field_color)
.with_field_type(field_type)
.with_field_value(field_value);
assert_eq!(serde_json::to_string_pretty(&field).unwrap(), json_str);
assert_eq!(
serde_json::from_str::<Field>(json_str.as_str()).unwrap(),
field
);
}
}