cas_codes

Crates.iocas_codes
lib.rscas_codes
version0.1.0
created_at2025-11-11 09:18:45.354199+00
updated_at2025-11-11 09:18:45.354199+00
descriptionA Rust library for parsing, validating, and working with Chemical Abstracts Service (CAS) Registry Numbers
homepagehttps://github.com/earth-metabolome-initiative/emi-monorepo/tree/main/web/web_common/cas_codes
repositoryhttps://github.com/earth-metabolome-initiative/emi-monorepo
max_upload_size
id1926902
size72,533
Luca Cappelletti (LucaCappelletti94)

documentation

https://docs.rs/cas_codes

README

CAS Codes

Crates.io Documentation PGRX Build Clippy Test

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

[dependencies]
cas_codes = "0.1.0"

# With optional features
cas_codes = { version = "0.1.0", features = ["serde", "pgrx"] }

Usage

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 Extension

The 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.

Using the Extension

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)
);

Diesel ORM Integration

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,
    }
}

License

MIT Licensed.

Commit count: 0

cargo fmt