#![doc = include_str!("../README.md")]
#[cfg(feature = "pgrx")]
::pgrx::pg_module_magic!();
mod digits;
mod display;
mod from;
mod from_str;
mod serde;
mod try_from;
pub use digits::Digits;
pub mod errors;
pub mod utils;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "diesel", derive(diesel_pgrx::DieselPGRX))]
#[cfg_attr(feature = "diesel", derive(diesel::AsExpression, diesel::FromSqlRow))]
#[
cfg_attr(feature = "diesel", diesel(sql_type = crate::diesel_impls::CAS))]
#[cfg_attr(
feature = "pgrx",
derive(pgrx::PostgresType, pgrx::PostgresEq, pgrx::PostgresOrd, pgrx::PostgresHash)
)]
#[cfg_attr(feature = "pgrx", pg_binary_protocol)]
pub struct CAS(u32);
impl CAS {
pub fn new(first: u32, second: u8, third: u8) -> Result<Self, crate::errors::Error> {
Self::try_from((first, second, third))
}
#[must_use]
pub fn check_digit(self) -> u8 {
u8::try_from(self.0 % 10).unwrap()
}
#[must_use]
pub fn first(self) -> u32 {
self.0 / 1000
}
#[must_use]
pub fn second(self) -> u8 {
u8::try_from((self.0 / 10) % 100).unwrap()
}
#[must_use]
pub fn digits(self) -> Digits {
Digits::from(self)
}
#[must_use]
pub fn as_u32(self) -> u32 {
self.0
}
#[must_use]
pub fn is_valid(s: &str) -> bool {
Self::try_from(s).is_ok()
}
}