Skip to content

Commit

Permalink
Added serde feature - #6
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Oct 17, 2023
1 parent 1ae278f commit cf31e9a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ jobs:
- run: cargo test --features postgres
- run: cargo test --features sqlx
- run: cargo test --features diesel
- run: cargo test --features serde
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 0.3.0 (unreleased)

- Added `serde` feature
- Removed `postgres` from default features
- Reduced dependencies
- Updated Rust edition to 2021
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ bytes = { version = "1", optional = true }
postgres = { version = "0.19", default-features = false, optional = true }
diesel = { version = "2", default-features = false, features = ["postgres"], optional = true }
sqlx = { version = ">= 0.5, < 0.8", default-features = false, features = ["postgres"], optional = true }
serde = { version = "1", features = ["derive"], optional = true }

[dev-dependencies]
diesel = { version = "2", features = ["32-column-tables"] }
sqlx = { version = "0", default-features = false, features = ["runtime-async-std-native-tls"] }
async-std = { version = "1", features = ["attributes"] }
serde_json = "1"

[features]
postgres = ["dep:postgres", "dep:bytes"]
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ CREATE INDEX my_index ON items USING hnsw (embedding vector_l2_ops)

Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance

## Serialization [unreleased]

Use the `serde` feature to enable serialization

## Reference

Convert a vector to a `Vec<f32>`
Expand Down
20 changes: 20 additions & 0 deletions src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ use std::convert::TryInto;
#[cfg(feature = "diesel")]
use crate::diesel_ext::VectorType;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// A vector.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "diesel", derive(FromSqlRow, AsExpression))]
#[cfg_attr(feature = "diesel", diesel(sql_type = VectorType))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Vector(pub(crate) Vec<f32>);

impl From<Vec<f32>> for Vector {
Expand Down Expand Up @@ -70,4 +74,20 @@ mod tests {
let vec = Vector::from(vec![1.0, 2.0, 3.0]);
assert_eq!(vec.to_vec(), vec![1.0, 2.0, 3.0]);
}

#[cfg(feature = "serde")]
#[test]
fn test_serialize() {
let vec = Vector::from(vec![1.0, 2.0, 3.0]);
let json = serde_json::to_string(&vec).unwrap();
assert_eq!(json, "[1.0,2.0,3.0]");
}

#[cfg(feature = "serde")]
#[test]
fn test_deserialize() {
let json = "[1.0,2.0,3.0]";
let vec: Vector = serde_json::from_str(json).unwrap();
assert_eq!(vec, Vector::from(vec![1.0, 2.0, 3.0]));
}
}

0 comments on commit cf31e9a

Please sign in to comment.