capitol 0.2.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
#![warn(missing_docs)]
//! Parse citations of United States Congress legislative documents and convert them to
//! Congress.gov URLs.
//!
//! Calling `parse` on a string like `"118hr815"`, or calling `Citation::parse("118hr815")`
//! returns a struct representing the document's citation. Calling `to_url` on such a struct
//! returns `"https://www.congress.gov/bill/118th-congress/house-bill/815"`
//!
//! Legislative citations generally follow the form `<CONGRESS><CONGRESSIONAL_OBJECT_TYPE><NUMBER>`.
//! Measures (bills) can specify a version of the text of the bill with a two-to-three letter
//! string at the end of the citation.

mod constants;
mod error;

use std::fmt::Display;
use std::str::FromStr;

use crate::constants::{BASE_URL, BILL_VERSIONS, CURRENT_CONGRESS};
use crate::error::Error;

type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, PartialEq)]
struct Version(String);

#[derive(Debug, Default, PartialEq)]
struct CiteBytes {
    congress: Vec<u8>,
    chamber: u8,
    object_type: Vec<u8>,
    number: Vec<u8>,
    ver: Option<Vec<u8>>,
}

#[derive(Debug, PartialEq)]
struct Congress(u64);

impl Congress {
    fn parse(input: &[u8]) -> Result<Self> {
        match String::from_utf8(input.to_vec()) {
            Ok(s) => {
                let congress = s.parse::<u64>()?;
                if congress <= *CURRENT_CONGRESS {
                    Ok(Congress(congress))
                } else {
                    Err(Error::InvalidCongress)
                }
            }
            Err(e) => Err(Error::FromUtf8(e)),
        }
    }

    fn as_ordinal(&self) -> String {
        let mut ordinal = self.to_string();
        if ordinal.ends_with('1') {
            ordinal.push_str("st");
        } else if ordinal.ends_with('2') {
            ordinal.push_str("nd");
        } else if ordinal.ends_with('3') {
            ordinal.push_str("rd");
        } else {
            ordinal.push_str("th");
        }
        ordinal
    }
}

impl Display for Congress {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[derive(Debug, PartialEq)]
enum Chamber {
    House,
    Senate,
}

impl Display for Chamber {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::House => "house",
                Self::Senate => "senate",
            }
        )
    }
}

impl Chamber {
    fn parse(input: u8) -> Self {
        if input.eq_ignore_ascii_case(&b'h') {
            Self::House
        } else {
            Self::Senate
        }
    }
}

#[derive(Debug, PartialEq)]
enum CongObjectType {
    SenateBill,
    HouseBill,
    SenateResolution,
    HouseResolution,
    SenateConcurrentResolution,
    HouseConcurrentResolution,
    SenateJointResolution,
    HouseJointResolution,
    HouseReport,
    SenateReport,
}

impl CongObjectType {
    fn parse(input: &[u8], chamber: &Chamber) -> Result<Self> {
        match input.to_ascii_lowercase().as_slice() {
            b"" | b"r" if *chamber == Chamber::House => Ok(Self::HouseBill),
            b"" if *chamber == Chamber::Senate => Ok(Self::SenateBill),
            b"res" if *chamber == Chamber::House => Ok(Self::HouseResolution),
            b"res" if *chamber == Chamber::Senate => Ok(Self::SenateResolution),
            b"conres" if *chamber == Chamber::House => Ok(Self::HouseConcurrentResolution),
            b"conres" if *chamber == Chamber::Senate => Ok(Self::SenateConcurrentResolution),
            b"jres" if *chamber == Chamber::House => Ok(Self::HouseJointResolution),
            b"jres" if *chamber == Chamber::Senate => Ok(Self::SenateJointResolution),
            b"rpt" if *chamber == Chamber::House => Ok(Self::HouseReport),
            b"rpt" if *chamber == Chamber::Senate => Ok(Self::SenateReport),
            _ => Err(Error::UnknownCongObjectType),
        }
    }
}

impl Display for CongObjectType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::HouseBill | Self::SenateBill => "bill",
                Self::HouseResolution | Self::SenateResolution => "resolution",
                Self::HouseConcurrentResolution | Self::SenateConcurrentResolution =>
                    "concurrent-resolution",
                Self::HouseJointResolution | Self::SenateJointResolution => "joint-resolution",
                Self::HouseReport | Self::SenateReport => "report",
            }
        )
    }
}

/// Represents a legislative Citation.
///
/// A `Citation` consists of a Congress, a Chamber, a Congressional object type, a number, and
/// optionally for bills, a Version.
#[derive(Debug, PartialEq)]
pub struct Citation {
    congress: Congress,
    chamber: Chamber,
    object_type: CongObjectType,
    number: usize,
    ver: Option<Version>,
}

impl Citation {
    fn tokenize(input: &str) -> CiteBytes {
        let mut iter = input.as_bytes().iter().peekable();

        // initialize containers for various parts of the citation
        let mut congress_bytes: Vec<u8> = Vec::with_capacity(3);
        let mut type_bytes: Vec<u8> = Vec::with_capacity(7);
        let mut number_bytes: Vec<u8> = Vec::new();
        let mut ver_bytes: Vec<u8> = Vec::new();

        // initialize parts container
        let mut parts = CiteBytes::default();

        while let Some(&ch) = iter.next_if(|&&ch| ch > b'0' && ch <= b'9') {
            congress_bytes.push(ch);
        }

        parts.congress.clone_from(&congress_bytes);

        if let Some(&ch) = iter.next_if(|&&ch| ch == b'h' || ch == b'H' || ch == b's' || ch == b'S')
        {
            parts.chamber = ch;
        }

        while let Some(&ch) = iter.next_if(|&&ch| ch.is_ascii_alphabetic()) {
            type_bytes.push(ch);
        }

        parts.object_type = type_bytes;

        while let Some(&ch) = iter.next_if(|&&ch| ch.is_ascii_digit()) {
            number_bytes.push(ch);
        }

        parts.number = number_bytes;

        while let Some(&ch) = iter.next_if(|&&ch| ch.is_ascii_alphabetic()) {
            ver_bytes.push(ch);
        }

        if ver_bytes.is_empty() {
            parts.ver = None;
        } else {
            parts.ver = Some(ver_bytes);
        }

        parts
    }

    /// 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> {
        let bytes = Self::tokenize(input);
        let congress = Congress::parse(&bytes.congress)?;
        let chamber = Chamber::parse(bytes.chamber);
        let object_type = CongObjectType::parse(&bytes.object_type, &chamber)?;
        let number = String::from_utf8(bytes.number)?.parse::<usize>()?;
        let ver = if let Some(v) = bytes.ver {
            if BILL_VERSIONS.contains(&v.as_slice()) {
                let text = String::from_utf8(v)?;
                Some(Version(text))
            } else {
                return Err(Error::InvalidBillVersion);
            }
        } else {
            None
        };

        Ok(Citation {
            congress,
            chamber,
            object_type,
            number,
            ver,
        })
    }

    /// Converts a `Citation` to a URL on Congress.gov.
    ///
    /// Example
    ///
    /// ```rust
    /// use capitol::Citation;
    ///
    /// let url = "118hr815".parse::<Citation>().unwrap().to_url();
    /// ```
    pub fn to_url(&self) -> String {
        let collection = match self.object_type {
            CongObjectType::HouseReport | CongObjectType::SenateReport => "congressional-report",
            _ => "bill",
        };
        let mut base = format!(
            "{BASE_URL}/{collection}/{}-congress/{}-{}/{}",
            self.congress.as_ordinal(),
            self.chamber,
            self.object_type,
            self.number
        );

        if let Some(ver) = &self.ver {
            base.push_str("/text/");
            base.push_str(&ver.0);
        }

        base
    }
}

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_tokenize_no_ver_house_bill() {
        let mut input = "118hr8070";
        let expected = CiteBytes {
            congress: b"118".to_vec(),
            chamber: b'h',
            object_type: b"r".to_vec(),
            number: b"8070".to_vec(),
            ver: None,
        };
        let result = Citation::tokenize(&mut input);
        assert_eq!(expected, result);
    }

    #[test]
    fn test_parse_no_ver_house_bill() {
        let input = "118hr8070";
        let expected = Citation {
            congress: Congress(118),
            chamber: Chamber::House,
            object_type: CongObjectType::HouseBill,
            number: 8070,
            ver: None,
        };
        let result = input.parse();
        assert_eq!(expected, result.unwrap());
    }

    #[test]
    fn test_parse_house_bill() {
        let input = "118hrpt529";
        let expected = Citation {
            congress: Congress(118),
            chamber: Chamber::House,
            object_type: CongObjectType::HouseReport,
            number: 529,
            ver: None,
        };
        let result = input.parse();
        assert_eq!(expected, result.unwrap());
    }

    #[test]
    fn test_parse_senate_bill() {
        let input = "118srpt17";
        let expected = Citation {
            congress: Congress(118),
            chamber: Chamber::Senate,
            object_type: CongObjectType::SenateReport,
            number: 17,
            ver: None,
        };
        let result = input.parse();
        assert_eq!(expected, result.unwrap());
    }

    #[test]
    fn test_tokenize_no_ver_senate_bill() {
        let mut input = "118s5";
        let expected = CiteBytes {
            congress: b"118".to_vec(),
            chamber: b's',
            object_type: Vec::new(),
            number: b"5".to_vec(),
            ver: None,
        };
        let result = Citation::tokenize(&mut input);
        assert_eq!(expected, result);
    }

    #[test]
    fn test_tokenize_with_ver_house_bill() {
        let mut input = "118hr555ih";
        let expected = CiteBytes {
            congress: b"118".to_vec(),
            chamber: b'h',
            object_type: b"r".to_vec(),
            number: b"555".to_vec(),
            ver: Some(b"ih".to_vec()),
        };
        let result = Citation::tokenize(&mut input);
        assert_eq!(expected, result);
    }

    #[test]
    fn test_tokenize_with_ver_senate_bill() {
        let mut input = "118s17is";
        let expected = CiteBytes {
            congress: b"118".to_vec(),
            chamber: b's',
            object_type: Vec::new(),
            number: b"17".to_vec(),
            ver: Some(b"is".to_vec()),
        };
        let result = Citation::tokenize(&mut input);
        assert_eq!(expected, result);
    }

    #[test]
    fn test_house_bill_to_url() {
        let input = "118hr529";
        let expected = "https://www.congress.gov/bill/118th-congress/house-bill/529";
        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 = "https://www.congress.gov/bill/118th-congress/house-bill/529/text/ih";
        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 =
            "https://www.congress.gov/congressional-report/118th-congress/house-report/529";
        let citation = input.parse::<Citation>().unwrap();
        let result = citation.to_url();
        assert_eq!(expected, result);
    }
}