Skip to content

Incremental engine for long horizon agents 🌟 Star if you like it!

1.5 KB
Raw Blame
#![allow(
    clippy::clone_on_copy,
    reason = "pyo3's `from_py_object` impl clones the pyclass, out of reach of an item-level allow"
)]

use crate::prelude::*;

use pyo3::types::PyBytes;

/// Python wrapper for `utils::fingerprint::Fingerprint`.
///
/// Exposed to Python as `cocoindex._internal.core.Fingerprint`.
///
/// `Hash` + `Eq` are derived so `PyFingerprint` can be used as a key in Rust
/// `HashMap`s — this enables pyo3 to auto-extract Python `dict[Fingerprint, V]`
/// into `HashMap<PyFingerprint, V>` at the Rust↔Python boundary. Python's
/// `__hash__` / `__eq__` are auto-generated by pyo3 from these traits via the
/// `hash` / `eq` class attributes.
#[pyclass(name = "Fingerprint", frozen, eq, hash, from_py_object)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct PyFingerprint(pub utils::fingerprint::Fingerprint);

#[pymethods]
impl PyFingerprint {
    /// Return the raw 16-byte digest.
    pub fn as_bytes<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
        PyBytes::new(py, self.0.as_slice())
    }

    /// Base64 encoding used by CocoIndex for human-readable fingerprints.
    #[allow(clippy::wrong_self_convention, reason = "pymethods take `&self`")]
    pub fn to_base64(&self) -> String {
        self.0.to_base64()
    }

    pub fn __bytes__<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
        self.as_bytes(py)
    }

    pub fn __str__(&self) -> String {
        self.0.to_string()
    }

    pub fn __repr__(&self) -> String {
        format!("Fingerprint({})", self.0)
    }
}