turbopuffer_client/
response.rs

1use serde::Deserialize;
2use serde_json::Value;
3use std::{
4  collections::HashMap,
5  fmt,
6};
7
8#[derive(Debug, Deserialize)]
9pub enum Status {
10  // Necessary because turbopuffer api does not consistently send the same
11  // capitalization for "ok". Notably between upsert and delete endpoints.
12  #[serde(alias = "ok", alias = "OK")]
13  Ok,
14}
15
16#[derive(Debug, Deserialize)]
17pub struct UpsertResponse {
18  pub status: Status,
19}
20
21#[derive(Debug, Deserialize)]
22pub struct DeleteResponse {
23  pub status: Status,
24}
25
26#[derive(Debug, Deserialize)]
27pub struct QueryResponse {
28  pub vectors: Vec<ResponseVector>,
29}
30
31// Required because Ids may be strings or numbers.
32// TODO: Consider serializing/deseralizing to only strings to remove this.
33// TODO: Consider simply using serde_json::Value.
34#[derive(Debug, Deserialize)]
35#[serde(untagged)]
36pub enum Id {
37  String(String),
38  Int(i32),
39}
40
41impl fmt::Display for Id {
42  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43    match &self {
44      Id::String(s) => write!(f, "{}", s),
45      Id::Int(i) => write!(f, "{}", i),
46    }
47  }
48}
49
50#[derive(Debug, Deserialize)]
51pub struct ResponseVector {
52  pub dist: f32,
53  pub id: Id,
54  // Responses only contain vectors when "include_vectors" is true.
55  pub vector: Option<Vec<f32>>,
56  pub attributes: Option<HashMap<String, Value>>,
57}