use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub type VectorId = String;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct VectorMetadata {
pub data: HashMap<String, MetadataValue>,
}
impl VectorMetadata {
pub fn new() -> Self {
Self {
data: HashMap::new(),
}
}
pub fn from_pairs<I, K, V>(pairs: I) -> Self
where
I: IntoIterator<Item = (K, V)>,
K: Into<String>,
V: Into<MetadataValue>,
{
Self {
data: pairs
.into_iter()
.map(|(k, v)| (k.into(), v.into()))
.collect(),
}
}
pub fn insert<K: Into<String>, V: Into<MetadataValue>>(&mut self, key: K, value: V) {
self.data.insert(key.into(), value.into());
}
pub fn get(&self, key: &str) -> Option<&MetadataValue> {
self.data.get(key)
}
pub fn get_string(&self, key: &str) -> Option<&str> {
match self.data.get(key)? {
MetadataValue::String(s) => Some(s),
_ => None,
}
}
pub fn get_int(&self, key: &str) -> Option<i64> {
match self.data.get(key)? {
MetadataValue::Int(i) => Some(*i),
_ => None,
}
}
pub fn get_float(&self, key: &str) -> Option<f64> {
match self.data.get(key)? {
MetadataValue::Float(f) => Some(*f),
_ => None,
}
}
pub fn get_bool(&self, key: &str) -> Option<bool> {
match self.data.get(key)? {
MetadataValue::Bool(b) => Some(*b),
_ => None,
}
}
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
pub fn len(&self) -> usize {
self.data.len()
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum MetadataValue {
String(String),
Int(i64),
Float(f64),
Bool(bool),
List(Vec<MetadataValue>),
}
impl From<String> for MetadataValue {
fn from(s: String) -> Self {
MetadataValue::String(s)
}
}
impl From<&str> for MetadataValue {
fn from(s: &str) -> Self {
MetadataValue::String(s.to_string())
}
}
impl From<i64> for MetadataValue {
fn from(i: i64) -> Self {
MetadataValue::Int(i)
}
}
impl From<i32> for MetadataValue {
fn from(i: i32) -> Self {
MetadataValue::Int(i as i64)
}
}
impl From<f64> for MetadataValue {
fn from(f: f64) -> Self {
MetadataValue::Float(f)
}
}
impl From<f32> for MetadataValue {
fn from(f: f32) -> Self {
MetadataValue::Float(f as f64)
}
}
impl From<bool> for MetadataValue {
fn from(b: bool) -> Self {
MetadataValue::Bool(b)
}
}
impl<T: Into<MetadataValue>> From<Vec<T>> for MetadataValue {
fn from(v: Vec<T>) -> Self {
MetadataValue::List(v.into_iter().map(Into::into).collect())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
pub id: VectorId,
pub score: f32,
pub metadata: Option<VectorMetadata>,
}
impl SearchResult {
pub fn new(id: VectorId, score: f32, metadata: Option<VectorMetadata>) -> Self {
Self {
id,
score,
metadata,
}
}
}
#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct StoredVector {
pub id: VectorId,
pub internal_id: usize,
pub vector: Vec<f32>,
pub metadata: Option<VectorMetadata>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_metadata_basic() {
let mut meta = VectorMetadata::new();
meta.insert("title", "Test Document");
meta.insert("score", 0.95f64);
meta.insert("count", 42i64);
meta.insert("active", true);
assert_eq!(meta.get_string("title"), Some("Test Document"));
assert_eq!(meta.get_float("score"), Some(0.95));
assert_eq!(meta.get_int("count"), Some(42));
assert_eq!(meta.get_bool("active"), Some(true));
}
#[test]
fn test_metadata_from_pairs() {
let meta = VectorMetadata::from_pairs([
("key1", MetadataValue::String("value1".to_string())),
("key2", MetadataValue::Int(123)),
]);
assert_eq!(meta.len(), 2);
assert_eq!(meta.get_string("key1"), Some("value1"));
assert_eq!(meta.get_int("key2"), Some(123));
}
#[test]
fn test_search_result() {
let result = SearchResult::new(
"doc1".to_string(),
0.95,
Some(VectorMetadata::from_pairs([(
"title",
MetadataValue::String("Test".to_string()),
)])),
);
assert_eq!(result.id, "doc1");
assert_eq!(result.score, 0.95);
assert!(result.metadata.is_some());
let bare = SearchResult::new("doc2".to_string(), 0.5, None);
assert!(bare.metadata.is_none());
}
#[test]
fn test_metadata_get_and_empty() {
let empty = VectorMetadata::new();
assert!(empty.is_empty());
assert_eq!(empty.len(), 0);
assert!(empty.get("missing").is_none());
let mut meta = VectorMetadata::new();
meta.insert("tag", "alpha");
assert!(!meta.is_empty());
assert_eq!(meta.len(), 1);
assert_eq!(
meta.get("tag"),
Some(&MetadataValue::String("alpha".to_string()))
);
assert!(meta.get("missing").is_none());
}
#[test]
fn test_metadata_typed_getters_wrong_type() {
let mut meta = VectorMetadata::new();
meta.insert("name", "hello");
meta.insert("count", 7i64);
meta.insert("ratio", 0.5f64);
meta.insert("active", false);
assert_eq!(meta.get_string("name"), Some("hello"));
assert_eq!(meta.get_string("count"), None);
assert_eq!(meta.get_int("count"), Some(7));
assert_eq!(meta.get_int("name"), None);
assert_eq!(meta.get_float("ratio"), Some(0.5));
assert_eq!(meta.get_float("count"), None);
assert_eq!(meta.get_bool("active"), Some(false));
assert_eq!(meta.get_bool("ratio"), None);
}
#[test]
fn test_metadata_value_from_impls() {
let from_string: MetadataValue = MetadataValue::from(String::from("text"));
let from_str: MetadataValue = "text".into();
let from_i32: MetadataValue = 42i32.into();
let from_i64: MetadataValue = 99i64.into();
let from_f32: MetadataValue = 1.5f32.into();
let from_f64: MetadataValue = 2.5f64.into();
let from_bool: MetadataValue = true.into();
let from_list: MetadataValue = vec!["a", "b"].into();
assert_eq!(from_string, MetadataValue::String("text".to_string()));
assert_eq!(from_str, MetadataValue::String("text".to_string()));
assert_eq!(from_i32, MetadataValue::Int(42));
assert_eq!(from_i64, MetadataValue::Int(99));
assert_eq!(from_f32, MetadataValue::Float(1.5));
assert_eq!(from_f64, MetadataValue::Float(2.5));
assert_eq!(from_bool, MetadataValue::Bool(true));
assert_eq!(
from_list,
MetadataValue::List(vec![
MetadataValue::String("a".to_string()),
MetadataValue::String("b".to_string()),
])
);
}
#[test]
fn test_stored_vector_roundtrip() {
let stored = StoredVector {
id: "vec-1".to_string(),
internal_id: 0,
vector: vec![1.0, 2.0, 3.0],
metadata: Some(VectorMetadata::from_pairs([("k", "v")])),
};
let json = serde_json::to_string(&stored).unwrap();
let decoded: StoredVector = serde_json::from_str(&json).unwrap();
assert_eq!(decoded.id, "vec-1");
assert_eq!(decoded.internal_id, 0);
assert_eq!(decoded.vector, vec![1.0, 2.0, 3.0]);
assert_eq!(
decoded.metadata.as_ref().unwrap().get_string("k"),
Some("v")
);
}
}