CAS Codes
A Rust library for parsing and validating Chemical Abstracts Service (CAS) Registry Numbers.
Features
- Parse and validate CAS numbers with checksum verification
- Zero-cost abstractions (stored as
u32) - Optional Serde,
PostgreSQL, and Diesel integration
Installation
[]
= "0.1.0"
# With optional features
= { = "0.1.0", = ["serde", "pgrx"] }
Usage
use CAS;
let cas = CAStry_from?; // Water
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
# Ok::
PostgreSQL Extension
The crate can be built as a PostgreSQL extension using PGRX:
[]
= { = "0.1.0", = ["pgrx", "pg17"] }
Note: Currently uses PostgreSQL's varlena type since PGRX doesn't yet support custom fixed-size types. Future PGRX versions will enable more efficient fixed-size storage.
Using the Extension
Step 1: Build the extension:
USER_ID= GROUP_ID=
Step 2: Copy extension files to PostgreSQL:
# Copy the shared library
# Copy the control file
# Copy the SQL file
Step 3: Enable in PostgreSQL:
CREATE EXTENSION IF NOT EXISTS cas_codes;
Step 4: Use the CAS type:
-- Create a table with CAS numbers
(
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
cas_number CAS NOT NULL UNIQUE,
purity REAL CHECK (purity > 0 AND purity <= 100)
);
Diesel ORM Integration
For use with Diesel ORM:
[]
= { = "0.1.0", = ["postgres"] }
Example Diesel table definition:
table!
License
MIT Licensed.