capitol 0.3.0

Parse United States Congress legislative document citations
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
use crate::constants::{CDG_BASE_URL, GOVINFO_BASE_URL};
use crate::error::Error;
use crate::legislation::{Chamber, CommitteeDocumentType, Congress, MeasureType};
use crate::parser::CitationParser;
use crate::utils::{DisplayOption, Result};
use std::str::FromStr;

/// Represents a Citation for a legislative document.
///
/// A `Citation` can represent a Measure (bill or resolution), a (public) `Law`, a `Statute`,
/// or a `CommitteeDocument` (congressional report or committee print).
#[derive(Debug, PartialEq)]
pub enum Citation {
    /// A Measure represents a bill or resolution.
    Measure {
        /// The Congress in which the measure was introduced.
        congress: Option<Congress>,
        /// The Congressional chamber of introduction.
        chamber: Chamber,
        /// The type of the Measure; `Bill`, `Resolution`, `ConcurrentResolution`, or `JointResolution`
        measure_type: MeasureType,
        /// The bill number
        number: usize,
        /// An optional suffix representing the text version of the measure.
        version: Option<String>,
    },
    /// A `Law` represents public slip laws.
    Law {
        /// The Congress in which the measure was introduced.
        congress: Option<Congress>,
        /// The law number.
        number: usize,
    },
    /// A `Statute` represents a federal statute at large.
    Statute {
        /// The statute volume number.
        volume: usize,
        /// The statute page number.
        page: usize,
    },
    /// A `CommitteeDocument` represents a committee publication.
    CommitteeDocument {
        /// The Congress during which the document was published.
        congress: Option<Congress>,
        /// The chamber of the committee.
        chamber: Chamber,
        /// A congressional report or committee print.
        document_type: CommitteeDocumentType,
        /// The number of the document.
        number: usize,
    },
}

impl Citation {
    /// Parse a legislative citation.
    ///
    /// The method first breaks up the citation into its constituent parts, then parses each of the
    /// parts, validating that the given Congress does not exceed the current Congress.
    ///
    /// Example
    ///
    /// ```rust
    /// use capitol::Citation;
    ///
    /// let citation = Citation::parse("118hr815");
    /// ```
    ///
    /// # Errors
    ///
    /// Will result in an error if the Congress part of the citation is invalid (greater than the
    /// current Congress), if the Congressional object type is unrecognized, if an integer can't be
    /// parsed from the document number, or if the document is a bill and has an unrecognized
    /// version type.
    pub fn parse(input: &str) -> Result<Self> {
        CitationParser::parse(input)
    }

    #[must_use]
    /// Render a URL for the document represented by the citation.
    /// Measures, committee documents, and laws go to Congress.gov.
    /// Statutes go to Govinfo.
    ///
    /// Example
    ///
    /// ```rust
    /// use capitol::Citation;
    ///
    /// let citation = Citation::parse("118hr815").unwrap();
    /// let url = citation.to_url();
    /// assert_eq!("https://www.congress.gov/bill/118th-congress/house-bill/815", url.unwrap());
    /// ```
    pub fn to_url(&self) -> Option<String> {
        match self {
            Self::Measure {
                congress,
                chamber,
                measure_type,
                number,
                version,
            } => {
                if let Some(congress) = congress {
                    let collection = "bill";
                    let congress = congress.as_ordinal();
                    let mut url = format!(
                    "{CDG_BASE_URL}/{collection}/{congress}-congress/{chamber}-{measure_type}/{number}"
                );

                    if let Some(ver) = version {
                        url.push_str("/text/");
                        url.push_str(ver);
                    }
                    Some(url)
                } else {
                    None
                }
            }
            Self::CommitteeDocument {
                congress,
                chamber,
                document_type,
                number,
            } => {
                if let Some(congress) = congress {
                    let congress = congress.as_ordinal();
                    let collection = match document_type {
                        CommitteeDocumentType::Report => "congressional-report",
                        CommitteeDocumentType::Print => "committee-print",
                    };
                    let short = if collection == "congressional-report" {
                        "report"
                    } else {
                        collection
                    };
                    let url = format!(
                    "{CDG_BASE_URL}/{collection}/{congress}-congress/{chamber}-{short}/{number}"
                );
                    Some(url)
                } else {
                    None
                }
            }
            Self::Statute { volume, page } => Some(format!(
                "{GOVINFO_BASE_URL}/app/details/STATUTE-{volume}/STATUTE-{volume}-Pg{page}"
            )),
            Self::Law { congress, number } => {
                if let Some(Congress(congress)) = congress {
                    Some(format!(
                    "{CDG_BASE_URL}/{congress}/plaws/publ{number}/PLAW-{congress}publ{number}.pdf"
                ))
                } else {
                    None
                }
            }
        }
    }

    /// Returns the version of document if it is a measure, None, otherwise.
    ///
    /// Example
    ///
    /// ```rust
    /// use capitol::Citation;
    ///
    /// let citation = Citation::parse("118hr815ih").unwrap();
    /// let version = citation.version();
    /// assert_eq!(Some("ih"), version.as_deref());
    /// ```
    pub fn version(&self) -> Option<String> {
        match self {
            Self::Measure { version, .. } => version.as_ref().map(String::from),
            _ => None,
        }
    }

    #[must_use]
    /// Returns a normalized string representation of the citation, suitable for use
    /// when searching Congress.gov. If `show_version` is `true`, the `Citation` refers
    /// to a `Measure`, and a version is included in the citation the text version suffix
    /// is included in the output. Otherwise it is ignored.
    ///
    /// Example
    ///
    /// ```rust
    /// use capitol::Citation;
    ///
    /// let citation = Citation::parse("118.H.Con.Res.815.IH").unwrap();
    ///
    /// let normalized = citation.normalize(false);
    /// assert_eq!("118hconres815", normalized);
    ///
    /// let normalized = citation.normalize(true);
    /// assert_eq!("118hconres815ih", normalized);
    /// ```
    pub fn normalize(&self, show_version: bool) -> String {
        match &self {
            Self::Measure {
                chamber,
                congress,
                measure_type,
                number,
                version,
            } => {
                let object = match (chamber, measure_type) {
                    (Chamber::House, MeasureType::Bill) => "hr",
                    (Chamber::House, MeasureType::Resolution) => "hres",
                    (Chamber::House, MeasureType::ConcurrentResolution) => "hconres",
                    (Chamber::House, MeasureType::JointResolution) => "hjres",
                    (Chamber::Senate, MeasureType::Bill) => "s",
                    (Chamber::Senate, MeasureType::Resolution) => "sres",
                    (Chamber::Senate, MeasureType::ConcurrentResolution) => "sconres",
                    (Chamber::Senate, MeasureType::JointResolution) => "sjres",
                };
                let mut cite = format!("{}{object}{number}", DisplayOption(*congress));
                if show_version {
                    let version = DisplayOption(version.to_owned()).to_string();
                    cite.push_str(&version);
                }
                cite
            }
            Self::Law { congress, number } => {
                format!("{}publ{number}", DisplayOption(*congress))
            }
            Self::Statute { volume, page } => format!("{volume}stat{page}"),
            Self::CommitteeDocument {
                congress,
                chamber,
                document_type,
                number,
            } => {
                let object = match (chamber, document_type) {
                    (Chamber::House, CommitteeDocumentType::Report) => "hrpt",
                    (Chamber::Senate, CommitteeDocumentType::Report) => "srpt",
                    (Chamber::House, CommitteeDocumentType::Print) => "hprt",
                    (Chamber::Senate, CommitteeDocumentType::Print) => "sprt",
                };
                format!("{}{object}{number}", DisplayOption(*congress))
            }
        }
    }
}

impl FromStr for Citation {
    type Err = Error;
    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        Self::parse(s)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_parse_no_ver_house_bill() {
        let input = "118hr8070";
        let expected = Citation::Measure {
            congress: Some(Congress(118)),
            chamber: Chamber::House,
            measure_type: MeasureType::Bill,
            number: 8070,
            version: None,
        };
        let result = input.parse();
        assert_eq!(expected, result.unwrap());
    }

    #[test]
    fn test_parse_house_report() {
        let input = "118hrpt529";
        let expected = Citation::CommitteeDocument {
            congress: Some(Congress(118)),
            chamber: Chamber::House,
            document_type: CommitteeDocumentType::Report,
            number: 529,
        };
        let result = input.parse();
        assert_eq!(expected, result.unwrap());
    }

    #[test]
    fn test_parse_house_print() {
        let input = "119hprt58246";
        let expected = Citation::CommitteeDocument {
            congress: Some(Congress(119)),
            chamber: Chamber::House,
            document_type: CommitteeDocumentType::Print,
            number: 58246,
        };
        let result = input.parse();
        assert_eq!(expected, result.unwrap());
    }

    #[test]
    fn test_parse_senate_print() {
        let input = "119sprt57246";
        let expected = Citation::CommitteeDocument {
            congress: Some(Congress(119)),
            chamber: Chamber::Senate,
            document_type: CommitteeDocumentType::Print,
            number: 57246,
        };
        let result = input.parse();
        assert_eq!(expected, result.unwrap());
    }

    #[test]
    fn test_parse_senate_report() {
        let input = "118srpt17";
        let expected = Citation::CommitteeDocument {
            congress: Some(Congress(118)),
            chamber: Chamber::Senate,
            document_type: CommitteeDocumentType::Report,
            number: 17,
        };
        let result = input.parse();
        assert_eq!(expected, result.unwrap());
    }

    #[test]
    fn test_house_bill_to_url() {
        let input = "118hr529";
        let expected =
            Some("https://www.congress.gov/bill/118th-congress/house-bill/529".to_string());
        let citation = input.parse::<Citation>().unwrap();
        let result = citation.to_url();
        assert_eq!(expected, result);
    }

    #[test]
    fn test_house_bill_with_ver_to_url() {
        let input = "118hr529ih";
        let expected =
            Some("https://www.congress.gov/bill/118th-congress/house-bill/529/text/ih".to_string());
        let citation = input.parse::<Citation>().unwrap();
        let result = citation.to_url();
        assert_eq!(expected, result);
    }

    #[test]
    fn test_house_report_to_url() {
        let input = "118hrpt529";
        let expected = Some(
            "https://www.congress.gov/congressional-report/118th-congress/house-report/529"
                .to_string(),
        );
        let citation = input.parse::<Citation>().unwrap();
        let result = citation.to_url();
        assert_eq!(expected, result);
    }

    #[test]
    fn test_house_print_to_url() {
        let input = "119hprt58246";
        let expected = Some(
            "https://www.congress.gov/committee-print/119th-congress/house-committee-print/58246"
                .to_string(),
        );
        let citation = input.parse::<Citation>().unwrap();
        let result = citation.to_url();
        assert_eq!(expected, result);
    }

    #[test]
    fn test_senate_print_to_url() {
        let input = "119sprt57246";
        let expected = Some(
            "https://www.congress.gov/committee-print/119th-congress/senate-committee-print/57246"
                .to_string(),
        );
        let citation = input.parse::<Citation>().unwrap();
        let result = citation.to_url();
        assert_eq!(expected, result);
    }

    #[test]
    fn test_get_version() {
        let input = "118hr529ih";
        let expected = Some("ih");
        let citation = input.parse::<Citation>().unwrap();
        let result = citation.version();
        assert_eq!(expected, result.as_deref());
    }

    #[test]
    fn test_no_congress_to_url_returns_none() {
        let input = "hr529";
        let expected = None;
        let citation = input.parse::<Citation>().unwrap();
        let result = citation.to_url();
        assert_eq!(expected, result);
    }

    #[test]
    fn test_uppercase_input() {
        let input = "118HR529IH";
        let expected =
            Some("https://www.congress.gov/bill/118th-congress/house-bill/529/text/ih".to_string());
        let citation = input.parse::<Citation>().unwrap();
        let result = citation.to_url();
        assert_eq!(expected, result);
    }

    #[test]
    fn test_parse_house_bill_with_dots() {
        let input = "118h.r.529";
        let expected = Citation::Measure {
            congress: Some(Congress(118)),
            chamber: Chamber::House,
            measure_type: MeasureType::Bill,
            number: 529,
            version: None,
        };
        let result = input.parse();
        assert_eq!(expected, result.unwrap());
    }

    #[test]
    fn test_parse_house_bill_with_version_no_congress() {
        let input = "hr529ih";
        let expected = Citation::Measure {
            congress: None,
            chamber: Chamber::House,
            measure_type: MeasureType::Bill,
            number: 529,
            version: Some("ih".to_string()),
        };
        let result = input.parse();
        assert_eq!(expected, result.unwrap());
    }

    #[test]
    fn test_print_citation_without_version() {
        let input = "118hr529ih";
        let citation: Citation = input.parse().unwrap();
        let expected = "118hr529";
        let result = citation.normalize(false);
        assert_eq!(expected, result);
    }

    #[test]
    fn test_print_no_congress_citation_without_version() {
        let input = "H.R.529.IH";
        let citation: Citation = input.parse().unwrap();
        let expected = "hr529".to_string();
        let result = citation.normalize(false);
        assert_eq!(expected, result);
    }

    #[test]
    fn test_normalize_public_law() {
        let input = "Public Law No: 119-68";
        let citation: Citation = input.parse().unwrap();
        let expected = "119publ68";
        let result = citation.normalize(true);
        assert_eq!(expected, result);
    }

    #[test]
    fn test_normalize_statute() {
        let input = "86Stat1326";
        let citation: Citation = input.parse().unwrap();
        let expected = "86stat1326";
        let result = citation.normalize(true);
        assert_eq!(expected, result);
    }

    #[test]
    fn test_normalize_house_report() {
        let input = "119.H.Rpt.23";
        let citation: Citation = input.parse().unwrap();
        let expected = "119hrpt23";
        let result = citation.normalize(true);
        assert_eq!(expected, result);
    }

    #[test]
    fn test_normalize_senate_print() {
        let input = "119.S.Prt.23";
        let citation: Citation = input.parse().unwrap();
        let expected = "119sprt23";
        let result = citation.normalize(true);
        assert_eq!(expected, result);
    }

    #[test]
    fn test_statute_to_url() {
        let input = "135stat2454";
        let citation: Citation = input.parse().unwrap();
        let expected =
            Some("https://www.govinfo.gov/app/details/STATUTE-135/STATUTE-135-Pg2454".to_string());
        let result = citation.to_url();
        assert_eq!(expected, result);
    }

    #[test]
    fn test_law_to_url() {
        let input = "119pl68";
        let citation: Citation = input.parse().unwrap();
        let expected =
            Some("https://www.congress.gov/119/plaws/publ68/PLAW-119publ68.pdf".to_string());
        let result = citation.to_url();
        assert_eq!(expected, result);
        let input = "119publ68";
        let citation: Citation = input.parse().unwrap();
        let result = citation.to_url();
        assert_eq!(expected, result);
    }

    #[test]
    fn test_citations_with_spaces() {
        let input = "135 Stat. 2454";
        let citation: Citation = input.parse().unwrap();
        let expected = "135stat2454";
        let result = citation.normalize(false);
        assert_eq!(expected, result);
    }
}