| Crates.io | cas_codes |
| lib.rs | cas_codes |
| version | 0.1.0 |
| created_at | 2025-11-11 09:18:45.354199+00 |
| updated_at | 2025-11-11 09:18:45.354199+00 |
| description | A Rust library for parsing, validating, and working with Chemical Abstracts Service (CAS) Registry Numbers |
| homepage | https://github.com/earth-metabolome-initiative/emi-monorepo/tree/main/web/web_common/cas_codes |
| repository | https://github.com/earth-metabolome-initiative/emi-monorepo |
| max_upload_size | |
| id | 1926902 |
| size | 72,533 |
A Rust library for parsing and validating Chemical Abstracts Service (CAS) Registry Numbers.
u32)PostgreSQL, and Diesel integration[dependencies]
cas_codes = "0.1.0"
# With optional features
cas_codes = { version = "0.1.0", features = ["serde", "pgrx"] }
use cas_codes::CAS;
let cas = CAS::try_from("7732-18-5")?; // Water
assert_eq!(cas.first(), 7732);
assert_eq!(cas.second(), 18);
assert_eq!(cas.check_digit(), 5);
assert_eq!(cas.to_string(), "7732-18-5");
# Ok::<(), cas_codes::errors::Error>(())
PostgreSQL ExtensionThe crate can be built as a PostgreSQL extension using PGRX:
[dependencies]
cas_codes = { version = "0.1.0", features = ["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.
Step 1: Build the extension:
USER_ID=$(id -u) GROUP_ID=$(id -g) docker compose up
Step 2: Copy extension files to PostgreSQL:
# Copy the shared library
sudo cp extension/usr/lib/postgresql/17/lib/cas_codes.so /usr/lib/postgresql/17/lib/
# Copy the control file
sudo cp extension/usr/share/postgresql/17/extension/cas_codes.control /usr/share/postgresql/17/extension/
# Copy the SQL file
sudo cp extension/usr/share/postgresql/17/extension/cas_codes--0.1.0.sql /usr/share/postgresql/17/extension/
Step 3: Enable in PostgreSQL:
CREATE EXTENSION IF NOT EXISTS cas_codes;
Step 4: Use the CAS type:
-- Create a table with CAS numbers
CREATE TABLE reagents (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
cas_number CAS NOT NULL UNIQUE,
purity REAL CHECK (purity > 0 AND purity <= 100)
);
For use with Diesel ORM:
[dependencies]
cas_codes = { version = "0.1.0", features = ["postgres"] }
Example Diesel table definition:
#[cfg(feature = "diesel")]
diesel::table! {
reagents(id) {
id -> diesel::sql_types::Integer,
name -> diesel::sql_types::Text,
purity -> diesel::sql_types::Float,
cas_code -> ::cas_codes::diesel_impls::CAS,
}
}
MIT Licensed.