capitol 0.3.0

Parse United States Congress legislative document citations
Documentation
use std::fmt::Display;

use crate::constants::CURRENT_CONGRESS;
use crate::error::Error;
use crate::utils::Result;

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Congress(pub usize);

impl Congress {
    pub(crate) fn parse(input: usize) -> Result<Self> {
        if input <= *CURRENT_CONGRESS {
            Ok(Congress(input))
        } else {
            Err(Error::InvalidCongress)
        }
    }

    pub(crate) 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)]
pub 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 {
    pub(crate) fn parse(input: &str) -> Self {
        if input.starts_with('h') {
            Self::House
        } else {
            Self::Senate
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum MeasureType {
    Bill,
    Resolution,
    ConcurrentResolution,
    JointResolution,
}

impl Display for MeasureType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Bill => "bill",
                Self::Resolution => "resolution",
                Self::ConcurrentResolution => "concurrent-resolution",
                Self::JointResolution => "joint-resolution",
            }
        )
    }
}

#[derive(Debug, PartialEq)]
pub enum CommitteeDocumentType {
    Print,
    Report,
}