lnmp_embedding/
encoder.rs

1use crate::vector::Vector;
2use byteorder::{LittleEndian, WriteBytesExt};
3use std::io::Write;
4
5pub struct Encoder;
6
7impl Encoder {
8    pub fn encode(vector: &Vector) -> Result<Vec<u8>, std::io::Error> {
9        let mut buf = Vec::new();
10
11        // Spec: u16 dim, u8 dtype, u8 reserved (or similarity default?), [vector payload]
12        // The user spec said: u16 dim, u8 dtype, u8 similarity
13        // But similarity is usually a query parameter, not intrinsic to the vector storage.
14        // However, the user request says: "u16 dim, u8 dtype, u8 similarity, [vector payload]"
15        // Let's stick to the user spec for now, maybe 0x00 as default similarity if not specified?
16        // Or maybe the vector carries a preferred similarity metric?
17        // Let's assume 0x00 (Cosine) as default if not provided, but the Vector struct doesn't have it.
18        // I'll add a placeholder byte for now or maybe I should update Vector to hold it?
19        // The user spec: "u16 dim, u8 dtype, u8 similarity"
20        // I'll just write 0x01 (Cosine) as a default for now.
21
22        buf.write_u16::<LittleEndian>(vector.dim)?;
23        buf.write_u8(vector.dtype as u8)?;
24        buf.write_u8(0x01)?; // Default to Cosine for now
25        buf.write_all(&vector.data)?;
26
27        Ok(buf)
28    }
29}