polyc-embeddings 0.1.3

Provider-agnostic embedding trait for polychrome (local-first tool search).
Documentation
//! `EmbeddingError` marker trait and a reference implementation.
//!
//! Every `EmbeddingProvider::Error` associated type must satisfy the
//! [`EmbeddingError`] bound, equivalent to
//! `std::error::Error + Send + Sync + 'static` but named so it is grep-able and
//! can grow cross-backend extension methods without breaking changes. Mirrors
//! `polyc_llm::error::LlmError`.

/// Marker trait that every `EmbeddingProvider::Error` must satisfy.
///
/// Equivalent to `std::error::Error + Send + Sync + 'static`, written as its
/// own trait so it is searchable, a single place to add cross-backend
/// extension methods later, and a sticky name in error messages.
pub trait EmbeddingError: std::error::Error + Send + Sync + 'static {}

/// Blanket impl: any type that meets the bound is an `EmbeddingError`.
impl<T> EmbeddingError for T where T: std::error::Error + Send + Sync + 'static {}

/// Reference implementation: the shape of error a real backend would ship.
/// Concrete backend crates define their own.
#[derive(Debug, thiserror::Error)]
pub enum DummyError {
    /// The model could not be loaded (missing/corrupt weights).
    #[error("model load failed: {0}")]
    Load(String),

    /// Embedding a batch failed (e.g. tokenisation).
    #[error("embed failed: {0}")]
    Embed(String),

    /// Anything else, escape hatch.
    #[error("other: {0}")]
    Other(String),
}

#[cfg(test)]
mod tests {
    use super::{DummyError, EmbeddingError};

    fn require_embedding_error<E: EmbeddingError>() {}
    fn assert_send_sync<T: Send + Sync + 'static>() {}

    #[test]
    fn display_load() {
        let e = DummyError::Load("no safetensors".to_owned());
        assert_eq!(format!("{e}"), "model load failed: no safetensors");
    }

    #[test]
    fn dummy_error_satisfies_embedding_error() {
        require_embedding_error::<DummyError>();
    }

    #[test]
    fn dummy_error_is_send_sync_static() {
        assert_send_sync::<DummyError>();
    }
}