anda_db_schema 0.4.9

Anda DB schema library for Rust.
Documentation
//! The [`Resource`] type — a reusable schema describing a binary asset or
//! external reference attached to a document.
use serde::{Deserialize, Serialize};

use crate::{
    AndaDBSchema, ByteArrayB64, ByteBufB64, FieldEntry, FieldKey, FieldType, FieldTyped, Json, Map,
    Schema, SchemaError,
};

/// Represents a resource for AI Agents.
///
/// A `Resource` is a generic descriptor for any external piece of data an
/// agent may need to reference: an inline blob, a remote file, a URL, etc.
/// Every field except `_id`, `tags` and `name` is optional, so the same
/// type can describe both lightweight references (`uri` only) and fully
/// inlined assets (`blob` plus `mime_type` and `hash`).
///
/// The struct derives [`AndaDBSchema`] and [`FieldTyped`], so it can be
/// embedded as a sub-document in any other Anda DB schema by simply using
/// `Resource` (or `Option<Resource>`) as a field type.
#[derive(Debug, Default, Clone, Serialize, Deserialize, FieldTyped, PartialEq, AndaDBSchema)]
pub struct Resource {
    /// The unique identifier for this resource in the Anda DB collection.
    pub _id: u64,

    /// A list of tags that identifies the type of this resource.
    /// It is recommended to use the primary type of the file MIME type and the file extension as tags, for example:
    /// "text", "image", "audio", "video", "txt", "md", "png", etc.
    pub tags: Vec<String>,

    /// A human-readable name for this resource.
    pub name: String,

    /// A description of what this resource represents.
    /// This can be used by clients to improve the LLM's understanding of available resources.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// The URI of this resource.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uri: Option<String>,

    /// MIME type, https://developer.mozilla.org/zh-CN/docs/Web/HTTP/MIME_types/Common_types
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mime_type: Option<String>,

    /// The binary data of this resource.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub blob: Option<ByteBufB64>,

    /// The size of the resource in bytes.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub size: Option<u64>,

    /// The SHA3-256 hash of the resource.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[unique]
    pub hash: Option<ByteArrayB64<32>>,

    /// Metadata associated with this resource.
    /// This can include additional information such as creation date, author, etc.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<Map<String, Json>>,
}

#[cfg(test)]
mod tests {}