stelae 0.6.6

A collection of tools in Rust and Python for preserving, authenticating, and accessing laws in perpetuity.
Documentation
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use chrono::NaiveDate;
use serde::Serialize;

use super::format_date;
use crate::server::api::versions::response::Version;

/// Messages for the versions endpoint.
#[derive(Serialize, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Historical {
    /// Message for an outdated publication.
    pub publication: Option<String>,
    /// Message for an outdated version.
    pub version: Option<String>,
    /// Message for a comparison between two versions.
    pub comparison: Option<String>,
}

/// Returns historical messages for the versions endpoint.
/// The historical messages currently include:
/// - A message for an outdated publication.
/// - A message for an outdated version.
/// - A message for a comparison between two versions.
#[must_use]
pub fn historical(
    versions: &[Version],
    current_publication_name: &str,
    active_publication_name: &str,
    version_date: &Option<String>,
    compare_to_date: &Option<String>,
    publication_exists: bool,
) -> Historical {
    let current_version: &str = versions
        .first()
        .map(|lmv| lmv.date.as_str())
        .unwrap_or_default();

    let publication = publication_message(
        active_publication_name,
        current_publication_name,
        current_version,
        publication_exists,
    );
    let version = version_date.as_ref().and_then(|found_version_date| {
        version_message(
            current_version,
            found_version_date,
            versions,
            compare_to_date.as_ref(),
        )
    });
    let comparison = compare_to_date.as_ref().and_then(|found_compare_to_date| {
        version_date.as_ref().map(|found_version_date| {
            comparison_message(
                found_compare_to_date,
                found_version_date,
                current_version,
                versions,
            )
        })
    });
    Historical {
        publication,
        version,
        comparison,
    }
}

/// Returns a historical message for an outdated publication.
fn publication_message(
    active_publication_name: &str,
    current_publication_name: &str,
    current_version: &str,
    publication_exists: bool,
) -> Option<String> {
    if active_publication_name == current_publication_name || active_publication_name == "current" {
        return None;
    }
    if !publication_exists {
        return Some(
            "You have selected an invalid publication. Please select a valid one.".to_owned(),
        );
    }
    Some(publication_message_template(current_version))
}

/// Formats the response for an outdated publication.
fn publication_message_template(date: &str) -> String {
    format!(
        "You are viewing a historical publication that was last updated on {current_date} and is no longer being updated.",
        current_date = format_date(date)
    )
}

/// Returns a historical message for an outdated version.
/// Version is outdated if `version_date` is in the past.
fn version_message(
    current_version: &str,
    version_date: &str,
    versions: &[Version],
    compare_to_date: Option<&String>,
) -> Option<String> {
    let is_current_version = {
        let current_date =
            NaiveDate::parse_from_str(current_version, "%Y-%m-%d").unwrap_or_default();
        let Ok(parsed_version_date) = NaiveDate::parse_from_str(version_date, "%Y-%m-%d") else {
            return None;
        };
        current_date <= parsed_version_date
    };
    if compare_to_date.is_some() || is_current_version {
        return None;
    }
    let version_date_idx = versions
        .iter()
        .position(|ver| ver.date.as_str() == version_date);
    let (start_date, end_date) = version_date_idx.map_or_else(
        || {
            let end_date = versions
                .iter()
                .filter(|ver| ver.date.as_str() > version_date)
                .map(|ver| ver.date.as_str())
                .min();
            let found_idx = versions
                .iter()
                .position(|ver| ver.date.as_str() == end_date.unwrap_or_default())
                .unwrap_or_default();
            let start_date = versions
                .get(found_idx + 1)
                .map_or_else(|| versions.last(), Some)
                .map(|ver| ver.date.as_str());
            (start_date.unwrap_or_default(), end_date.unwrap_or_default())
        },
        |idx| {
            let start_date = version_date;
            let end_date = versions
                .get(idx - 1)
                .map_or_else(|| versions.first(), Some)
                .map(|ver| ver.date.as_str())
                .unwrap_or_default();
            (start_date, end_date)
        },
    );
    Some(version_message_template(version_date, start_date, end_date))
}

/// Formats the response for an outdated version.
fn version_message_template(version_date: &str, start_date: &str, end_date: &str) -> String {
    format!(
        "You are viewing this document as it appeared on {version_date}. This version was valid between {start_date} and {end_date}.",
        version_date = format_date(version_date),
        start_date = format_date(start_date),
        end_date = format_date(end_date)
    )
}

/// Returns a historical message for a comparison between two versions.
fn comparison_message(
    compare_to_date: &str,
    version_date: &str,
    current_date: &str,
    versions: &[Version],
) -> String {
    let (compare_start_date, compare_end_date) = if version_date > compare_to_date {
        (compare_to_date, version_date)
    } else {
        (version_date, compare_to_date)
    };
    let start_idx = Version::find_index_or_closest(versions, compare_start_date);
    let end_idx = Version::find_index_or_closest(versions, compare_end_date);
    let num_of_changes = start_idx - end_idx;
    let start_date = format_date(compare_start_date);
    let end_date = if compare_end_date == current_date {
        None
    } else {
        Some(format_date(compare_end_date))
    };
    messages_between_template(num_of_changes, &start_date, end_date)
}

/// Formats and returns a message for the number of changes between two dates.
fn messages_between_template(
    num_of_changes: usize,
    start_date: &str,
    end_date: Option<String>,
) -> String {
    let changes = match num_of_changes {
        0 => "no updates".to_owned(),
        1 => "1 update".to_owned(),
        _ => format!("{num_of_changes} updates"),
    };

    end_date.map_or_else(
        || format!("There have been <strong>{changes}</strong> since {start_date}."),
        |found_end_date| {
            format!(
                "There have been <strong>{changes}</strong> between {start_date} and {found_end_date}."
            )
        },
    )
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    use std::cmp::Reverse;
    use std::collections::BTreeMap;

    use super::super::Publication;

    fn publication_to_versions() -> BTreeMap<Reverse<String>, Publication> {
        let test_data = json!({
            "2023-12-30": {
                "active": false,
                "date": "2023-12-30",
                "display": "2023-12-30",
                "name": "2023-12-30",
                "revoked": false,
                "versions": [
                    {"date": "2023-12-30", "display": "2023-12-30", "version": 0},
                    {"date": "2023-12-11", "display": "2023-12-11", "version": 0},
                    {"date": "2023-11-02", "display": "2023-11-02", "version": 0},
                    {"date": "2023-10-22", "display": "2023-10-22", "version": 0},
                    {"date": "2023-08-12", "display": "2023-08-12", "version": 0},
                    {"date": "2023-08-10", "display": "2023-08-10", "version": 0},
                    {"date": "2023-06-04", "display": "2023-06-04", "version": 0},
                    {"date": "2023-01-01", "display": "2023-01-01", "version": 0}
                ]
            },
            "2023-10-22": {
                "active": false,
                "date": "2023-10-22",
                "display": "2023-10-22",
                "name": "2023-10-22",
                "revoked": false,
                "versions": [
                    {"date": "2023-10-22", "display": "2023-10-22", "version": 0},
                    {"date": "2023-08-12", "display": "2023-08-12", "version": 0},
                    {"date": "2023-08-10", "display": "2023-08-10", "version": 0},
                    {"date": "2023-06-04", "display": "2023-06-04", "version": 0},
                    {"date": "2023-01-01", "display": "2023-01-01", "version": 0}
                ]
            }
        });
        let map: BTreeMap<Reverse<String>, Publication> =
            serde_json::from_value(test_data).unwrap();
        map
    }

    fn current_publication_name() -> String {
        "2023-12-30".to_string()
    }

    #[test]
    fn test_historical_when_current_publication_expect_no_historical_messages() {
        let active_publication_name = "2023-12-30".to_string();
        let current_publication_name = current_publication_name();
        let publication_to_versions = publication_to_versions();
        let versions = &publication_to_versions
            .get(&Reverse(active_publication_name.clone()))
            .unwrap()
            .versions;
        let version_date: Option<String> = None;
        let compare_to_date: Option<String> = None;

        let cut = historical;

        let actual = cut(
            &versions,
            &current_publication_name,
            &active_publication_name,
            &version_date,
            &compare_to_date,
            true,
        );
        let expected = Historical {
            publication: None,
            version: None,
            comparison: None,
        };

        assert_eq!(actual, expected);
    }

    #[test]
    fn test_historical_when_outdated_publication_expect_publication_message_with_last_update() {
        let test_cases = vec![
            None,
            Some("2023-10-22".to_string()),
            Some("2024-06-06".to_string()),
        ];

        for version_date in test_cases {
            let active_publication_name = "2023-10-22".to_string();
            let current_publication_name = current_publication_name();
            let publication_to_versions = publication_to_versions();
            let versions = &publication_to_versions
                .get(&Reverse(active_publication_name.clone()))
                .unwrap()
                .versions;
            let compare_to_date: Option<String> = None;

            let cut = historical;

            let actual = cut(
                &versions,
                &current_publication_name,
                &active_publication_name,
                &version_date,
                &compare_to_date,
                true,
            );
            let expected = Historical {
                publication: Some(publication_message_template(&versions[0].date)),
                version: None,
                comparison: None,
            };

            assert_eq!(actual, expected);
        }
    }

    #[test]
    fn test_historical_when_outdated_publication_and_outdated_date_expect_publication_and_version_message_with_last_update(
    ) {
        let test_cases = vec![
            ("2023-01-01", "2023-01-01", "2023-06-04"), // first date
            ("2023-08-12", "2023-08-12", "2023-10-22"), // middle date
            ("2023-02-02", "2023-01-01", "2023-06-04"), // non-existing date
        ];

        for (version_date, start_date, end_date) in test_cases {
            let active_publication_name = "2023-10-22".to_string();
            let current_publication_name = current_publication_name();
            let publication_to_versions = publication_to_versions();
            let versions = &publication_to_versions
                .get(&Reverse(active_publication_name.clone()))
                .unwrap()
                .versions;
            let compare_to_date: Option<String> = None;

            let cut = historical;

            let actual = cut(
                &versions,
                &current_publication_name,
                &active_publication_name,
                &Some(version_date.to_string()),
                &compare_to_date,
                true,
            );
            let expected = Historical {
                publication: Some(publication_message_template(&versions[0].date)),
                version: Some(version_message_template(version_date, start_date, end_date)),
                comparison: None,
            };

            assert_eq!(actual, expected);
        }
    }

    #[test]
    fn test_historical_when_comparing_with_latest_date_expect_historical_message_with_comparison_date(
    ) {
        let test_cases = vec![
            ("2023-10-22", "no updates"),
            ("2023-08-12", "1 update"),
            ("2023-08-10", "2 updates"),
            ("2023-07-01", "3 updates"), // non-existing date
            ("2023-01-01", "4 updates"),
            ("2020-01-01", "5 updates"), // non-existing date before creation date
        ];

        for (version_date, changes) in test_cases {
            let active_publication_name = "2023-10-22".to_string();
            let current_publication_name = current_publication_name();
            let publication_to_versions = publication_to_versions();
            let versions = &publication_to_versions
                .get(&Reverse(active_publication_name.clone()))
                .unwrap()
                .versions;
            let compare_to_date = Some("2023-10-22".to_string());
            let start_date = version_date;

            let cut = historical;

            let actual = cut(
                &versions,
                &current_publication_name,
                &active_publication_name,
                &Some(version_date.to_string()),
                &compare_to_date,
                true,
            );

            let expected_comparison_message = messages_between_template(
                match changes {
                    "no updates" => 0,
                    "1 update" => 1,
                    "2 updates" => 2,
                    "3 updates" => 3,
                    "4 updates" => 4,
                    "5 updates" => 5,
                    _ => 0,
                },
                &format_date(start_date),
                None,
            );

            let expected = Historical {
                publication: Some(publication_message_template(&versions[0].date)),
                version: None,
                comparison: Some(expected_comparison_message),
            };

            assert_eq!(actual, expected);
        }
    }

    #[test]
    fn test_historical_messages_when_comparing_with_non_latest_date_expect_historical_message() {
        let test_cases = vec![
            ("2023-12-11", "2023-12-11", "no updates"),
            ("2023-10-22", "2023-11-02", "1 update"),
            ("2023-10-22", "2023-12-11", "2 updates"),
            ("2023-07-01", "2023-12-11", "5 updates"), // non-existing start date
            ("2023-06-04", "2023-09-11", "2 updates"), // non-existing end date
            ("2023-07-01", "2023-09-11", "2 updates"), // non-existing start and end date
            ("2020-01-01", "2023-06-04", "2 updates"), // non-existing start date before creation date
            ("2020-01-01", "2020-06-04", "no updates"), // non-existing start and end date before creation date
            ("2020-01-01", "2024-06-04", "8 updates"),  // end date in the future
            ("2023-07-01", "2024-06-04", "6 updates"), // non-existing start date and end date in the future
        ];

        for (version_date, compare_to_date, changes) in test_cases {
            let active_publication_name = "2023-12-30".to_string();
            let current_publication_name = current_publication_name();
            let publication_to_versions = publication_to_versions();
            let versions = &publication_to_versions
                .get(&Reverse(active_publication_name.clone()))
                .unwrap()
                .versions;

            let cut = historical;

            let actual = cut(
                &versions,
                &current_publication_name,
                &active_publication_name,
                &Some(version_date.to_string()),
                &Some(compare_to_date.to_string()),
                true,
            );

            let expected_comparison_message = messages_between_template(
                match changes {
                    "no updates" => 0,
                    "1 update" => 1,
                    "2 updates" => 2,
                    "3 updates" => 3,
                    "4 updates" => 4,
                    "5 updates" => 5,
                    "6 updates" => 6,
                    "7 updates" => 7,
                    "8 updates" => 8,
                    _ => 0,
                },
                &format_date(version_date),
                Some(format_date(compare_to_date)),
            );

            let expected = Historical {
                publication: None,
                version: None,
                comparison: Some(expected_comparison_message),
            };

            assert_eq!(actual, expected);
        }
    }
}