"""SMILES string manipulation utilities.

This module provides functions for canonicalizing SMILES strings and
converting them to RDKit molecule objects.
"""

from typing import Optional
from rdkit import Chem


def canonicalize_smiles(smiles: str) -> Optional[str]:
    """Canonicalize a SMILES string.

    Converts a SMILES string to its canonical form using RDKit. Invalid
    SMILES strings return None.

    Args:
        smiles: Input SMILES string

    Returns:
        Canonical SMILES string, or None if invalid
    """
    try:
        mol = Chem.MolFromSmiles(smiles)
        if mol is None:
            return None
        return Chem.MolToSmiles(mol, canonical=True)
    except Exception:
        return None


def mol_from_smiles(smiles: str) -> Optional[Chem.Mol]:
    """Convert SMILES string to RDKit Mol object.

    Args:
        smiles: Input SMILES string

    Returns:
        RDKit Mol object, or None if invalid
    """
    try:
        return Chem.MolFromSmiles(smiles)
    except Exception:
        return None
