1use serde::{Deserialize, Serialize};
4use serde_json::Value as JsonValue;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct Point {
14 pub id: u64,
16
17 pub vector: Vec<f32>,
19
20 #[serde(default)]
22 pub payload: Option<JsonValue>,
23}
24
25impl Point {
26 #[must_use]
34 pub fn new(id: u64, vector: Vec<f32>, payload: Option<JsonValue>) -> Self {
35 Self {
36 id,
37 vector,
38 payload,
39 }
40 }
41
42 #[must_use]
44 pub fn without_payload(id: u64, vector: Vec<f32>) -> Self {
45 Self::new(id, vector, None)
46 }
47
48 #[must_use]
50 pub fn dimension(&self) -> usize {
51 self.vector.len()
52 }
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct SearchResult {
58 pub point: Point,
60
61 pub score: f32,
63}
64
65impl SearchResult {
66 #[must_use]
68 pub const fn new(point: Point, score: f32) -> Self {
69 Self { point, score }
70 }
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76 use serde_json::json;
77
78 #[test]
79 fn test_point_creation() {
80 let point = Point::new(1, vec![0.1, 0.2, 0.3], Some(json!({"title": "Test"})));
81
82 assert_eq!(point.id, 1);
83 assert_eq!(point.dimension(), 3);
84 assert!(point.payload.is_some());
85 }
86
87 #[test]
88 fn test_point_without_payload() {
89 let point = Point::without_payload(1, vec![0.1, 0.2, 0.3]);
90
91 assert_eq!(point.id, 1);
92 assert!(point.payload.is_none());
93 }
94
95 #[test]
96 fn test_point_serialization() {
97 let point = Point::new(1, vec![0.1, 0.2], Some(json!({"key": "value"})));
98 let json = serde_json::to_string(&point).unwrap();
99 let deserialized: Point = serde_json::from_str(&json).unwrap();
100
101 assert_eq!(point.id, deserialized.id);
102 assert_eq!(point.vector, deserialized.vector);
103 }
104}