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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use activitystreams_vocabulary::{create_object, field_access};
use crate::DiffSide;
create_object! {
/// A reference to a part of a merge request diff being reviewed.
///
/// # Example (code quote single line)
///
/// ```rust
/// use activityforge::{CodeQuote, DiffSide, context};
///
/// # fn main() {
/// let cq_file = "src/main.rs";
/// let cq_hunk = r#"fn main() {
/// println!("hello forgeverse!");
/// }"#;
/// let cq_hunk_json = serde_json::to_string(&cq_hunk).unwrap();
/// let cq_line = 1u64;
/// let cq_side = DiffSide::Old;
/// let cq_outdated = false;
///
/// let json_str = format!(
/// r#"{{
/// "@context": [
/// "https://www.w3.org/ns/activitystreams",
/// "https://forgefed.org/ns"
/// ],
/// "type": "CodeQuote",
/// "cqFile": "{cq_file}",
/// "cqHunk": {cq_hunk_json},
/// "cqLine": {cq_line},
/// "cqSide": "{cq_side}",
/// "cqOutdated": {cq_outdated}
/// }}"#
/// );
///
/// let context = context::forgefed_context();
///
/// let code_quote = CodeQuote::new()
/// .with_context_property(context)
/// .with_cq_file(cq_file)
/// .with_cq_hunk(cq_hunk)
/// .with_cq_line(cq_line)
/// .with_cq_side(cq_side)
/// .with_cq_outdated(cq_outdated);
///
/// assert_eq!(serde_json::to_string_pretty(&code_quote).unwrap(), json_str);
/// assert_eq!(
/// serde_json::from_str::<CodeQuote>(json_str.as_str()).unwrap(),
/// code_quote
/// );
/// # }
/// ```
///
/// # Example (code quote line range)
///
/// ```rust
/// use activityforge::{CodeQuote, DiffSide, context};
///
/// # fn main() {
/// let cq_file = "src/main.rs";
/// let cq_hunk = r#"fn main() {
/// println!("hello forgeverse!");
/// }"#;
/// let cq_hunk_json = serde_json::to_string(&cq_hunk).unwrap();
/// let cq_start = 1u64;
/// let cq_start_side = DiffSide::New;
/// let cq_end = 3u64;
/// let cq_end_side = DiffSide::New;
/// let cq_outdated = false;
///
/// let json_str = format!(
/// r#"{{
/// "@context": [
/// "https://www.w3.org/ns/activitystreams",
/// "https://forgefed.org/ns"
/// ],
/// "type": "CodeQuote",
/// "cqFile": "{cq_file}",
/// "cqHunk": {cq_hunk_json},
/// "cqStart": {cq_start},
/// "cqStartSide": "{cq_start_side}",
/// "cqEnd": {cq_end},
/// "cqEndSide": "{cq_end_side}",
/// "cqOutdated": {cq_outdated}
/// }}"#
/// );
///
/// let context = context::forgefed_context();
///
/// let code_quote = CodeQuote::new()
/// .with_context_property(context)
/// .with_cq_file(cq_file)
/// .with_cq_hunk(cq_hunk)
/// .with_cq_start(cq_start)
/// .with_cq_start_side(cq_start_side)
/// .with_cq_end(cq_end)
/// .with_cq_end_side(cq_end_side)
/// .with_cq_outdated(cq_outdated);
///
/// assert_eq!(serde_json::to_string_pretty(&code_quote).unwrap(), json_str);
/// assert_eq!(
/// serde_json::from_str::<CodeQuote>(json_str.as_str()).unwrap(),
/// code_quote
/// );
/// # }
/// ```
CodeQuote: crate::ObjectType::CodeQuote {
#[serde(skip_serializing_if = "Option::is_none")]
cq_file: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
cq_hunk: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
cq_line: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
cq_side: Option<DiffSide>,
#[serde(skip_serializing_if = "Option::is_none")]
cq_start: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
cq_start_side: Option<DiffSide>,
#[serde(skip_serializing_if = "Option::is_none")]
cq_end: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
cq_end_side: Option<DiffSide>,
#[serde(skip_serializing_if = "Option::is_none")]
cq_outdated: Option<bool>,
}
}
field_access! {
CodeQuote {
/// Path, relative to repo root, of the file whose changes are being quoted.
cq_file: option_deref { &str, String },
/// A diff hunk from the merge request’s diff.
cq_hunk: option_deref { &str, String },
}
}
field_access! {
CodeQuote {
/// The line of code being commented.
///
/// It may refer to the old or new version of the file, depending on `cqSide`.
cq_line: option { u64 },
/// To which version of the file `cqLine` refers.
cq_side: option { DiffSide },
/// The first line in a range of lines of code being commented.
///
/// It may refer to the old or new version of the file, depending on `cqStartSide`.
cq_start: option { u64 },
/// To which version of the file `cqStart` refers.
cq_start_side: option { DiffSide },
/// The last line in a range of lines of code being commented.
///
/// It may refer to the old or new version of the file, depending on `cqEndSide`.
cq_end: option { u64 },
/// To which version of the file `cqEnd` refers.
cq_end_side: option { DiffSide },
/// Whether these line(s) have been changed by a later version of the merge request.
cq_outdated: option { bool },
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::context;
#[test]
fn test_code_quote_single() {
let cq_file = "src/main.rs";
let cq_hunk = r#"fn main() {
println!("hello forgeverse!");
}"#;
let cq_hunk_json = serde_json::to_string(&cq_hunk).unwrap();
let cq_line = 1u64;
let cq_side = DiffSide::Old;
let cq_outdated = false;
let json_str = format!(
r#"{{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://forgefed.org/ns"
],
"type": "CodeQuote",
"cqFile": "{cq_file}",
"cqHunk": {cq_hunk_json},
"cqLine": {cq_line},
"cqSide": "{cq_side}",
"cqOutdated": {cq_outdated}
}}"#
);
let context = context::forgefed_context();
let code_quote = CodeQuote::new()
.with_context_property(context)
.with_cq_file(cq_file)
.with_cq_hunk(cq_hunk)
.with_cq_line(cq_line)
.with_cq_side(cq_side)
.with_cq_outdated(cq_outdated);
assert_eq!(serde_json::to_string_pretty(&code_quote).unwrap(), json_str);
assert_eq!(
serde_json::from_str::<CodeQuote>(json_str.as_str()).unwrap(),
code_quote
);
}
#[test]
fn test_code_quote_range() {
let cq_file = "src/main.rs";
let cq_hunk = r#"fn main() {
println!("hello forgeverse!");
}"#;
let cq_hunk_json = serde_json::to_string(&cq_hunk).unwrap();
let cq_start = 1u64;
let cq_start_side = DiffSide::New;
let cq_end = 3u64;
let cq_end_side = DiffSide::New;
let cq_outdated = false;
let json_str = format!(
r#"{{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://forgefed.org/ns"
],
"type": "CodeQuote",
"cqFile": "{cq_file}",
"cqHunk": {cq_hunk_json},
"cqStart": {cq_start},
"cqStartSide": "{cq_start_side}",
"cqEnd": {cq_end},
"cqEndSide": "{cq_end_side}",
"cqOutdated": {cq_outdated}
}}"#
);
let context = context::forgefed_context();
let code_quote = CodeQuote::new()
.with_context_property(context)
.with_cq_file(cq_file)
.with_cq_hunk(cq_hunk)
.with_cq_start(cq_start)
.with_cq_start_side(cq_start_side)
.with_cq_end(cq_end)
.with_cq_end_side(cq_end_side)
.with_cq_outdated(cq_outdated);
assert_eq!(serde_json::to_string_pretty(&code_quote).unwrap(), json_str);
assert_eq!(
serde_json::from_str::<CodeQuote>(json_str.as_str()).unwrap(),
code_quote
);
}
}