1use serde::{Deserialize, Serialize};
2use uuid::Uuid;
3use crate::vector::Vector;
4use crate::multivector::MultiVector;
5
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
8#[serde(untagged)]
9pub enum VectorData {
10 Single(Vector),
12 Multi(MultiVector),
14}
15
16impl VectorData {
17 pub fn dim(&self) -> usize {
19 match self {
20 VectorData::Single(v) => v.dim(),
21 VectorData::Multi(mv) => mv.dim(),
22 }
23 }
24
25 pub fn is_multi(&self) -> bool {
27 matches!(self, VectorData::Multi(_))
28 }
29
30 pub fn as_single(&self) -> Vector {
33 match self {
34 VectorData::Single(v) => v.clone(),
35 VectorData::Multi(mv) => mv.to_single_vector(),
36 }
37 }
38
39 pub fn as_slice(&self) -> &[f32] {
41 match self {
42 VectorData::Single(v) => v.as_slice(),
43 VectorData::Multi(mv) => mv.vectors().first().map(|v| v.as_slice()).unwrap_or(&[]),
44 }
45 }
46}
47
48impl From<Vector> for VectorData {
49 fn from(v: Vector) -> Self {
50 VectorData::Single(v)
51 }
52}
53
54impl From<MultiVector> for VectorData {
55 fn from(mv: MultiVector) -> Self {
56 VectorData::Multi(mv)
57 }
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct Point {
63 pub id: PointId,
64 #[serde(alias = "vectors")]
66 pub vector: Vector,
67 #[serde(skip_serializing_if = "Option::is_none")]
69 pub multivector: Option<MultiVector>,
70 pub payload: Option<serde_json::Value>,
71}
72
73#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
74#[serde(untagged)]
75pub enum PointId {
76 String(String),
77 Uuid(Uuid),
78 Integer(u64),
79}
80
81impl std::fmt::Display for PointId {
82 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83 match self {
84 PointId::String(s) => write!(f, "{}", s),
85 PointId::Uuid(u) => write!(f, "{}", u),
86 PointId::Integer(i) => write!(f, "{}", i),
87 }
88 }
89}
90
91impl From<String> for PointId {
92 fn from(s: String) -> Self {
93 PointId::String(s)
94 }
95}
96
97impl From<u64> for PointId {
98 fn from(i: u64) -> Self {
99 PointId::Integer(i)
100 }
101}
102
103impl From<Uuid> for PointId {
104 fn from(u: Uuid) -> Self {
105 PointId::Uuid(u)
106 }
107}
108
109impl Point {
110 #[inline]
112 #[must_use]
113 pub fn new(id: PointId, vector: Vector, payload: Option<serde_json::Value>) -> Self {
114 Self {
115 id,
116 vector,
117 multivector: None,
118 payload,
119 }
120 }
121
122 #[inline]
124 #[must_use]
125 pub fn new_multi(id: PointId, multivector: MultiVector, payload: Option<serde_json::Value>) -> Self {
126 let vector = multivector.to_single_vector();
128 Self {
129 id,
130 vector,
131 multivector: Some(multivector),
132 payload,
133 }
134 }
135
136 #[inline]
138 pub fn has_multivector(&self) -> bool {
139 self.multivector.is_some()
140 }
141
142 #[inline]
144 pub fn get_multivector(&self) -> Option<&MultiVector> {
145 self.multivector.as_ref()
146 }
147
148 #[inline]
149 #[must_use]
150 pub fn with_payload(mut self, payload: serde_json::Value) -> Self {
151 self.payload = Some(payload);
152 self
153 }
154
155 #[inline]
156 #[must_use]
157 pub fn with_multivector(mut self, multivector: MultiVector) -> Self {
158 self.multivector = Some(multivector);
159 self
160 }
161}
162