use std::fmt;
use crate::ids::{ContentKey, Digest128};
use crate::le::{u16_at, u32_at};
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct ModelIdentity {
pub name: String,
pub revision: String,
pub dimensions: u32,
pub normalized: bool,
pub text_template: u32,
pub chunk_template: u32,
}
const MAX_IDENTITY_STRING: usize = 4096;
impl ModelIdentity {
#[must_use]
pub fn encode(&self) -> Vec<u8> {
assert!(
self.name.len() <= MAX_IDENTITY_STRING && self.revision.len() <= MAX_IDENTITY_STRING,
"model name and revision must be at most {MAX_IDENTITY_STRING} bytes"
);
let mut out = Vec::with_capacity(17 + self.name.len() + self.revision.len());
out.extend_from_slice(&self.dimensions.to_le_bytes());
out.push(u8::from(self.normalized));
out.extend_from_slice(&self.text_template.to_le_bytes());
out.extend_from_slice(&self.chunk_template.to_le_bytes());
for text in [&self.name, &self.revision] {
let len = u16::try_from(text.len()).expect("checked above");
out.extend_from_slice(&len.to_le_bytes());
out.extend_from_slice(text.as_bytes());
}
out
}
#[must_use]
pub fn decode(bytes: &[u8]) -> Option<Self> {
if bytes.len() < 13 {
return None;
}
let normalized = match bytes[4] {
0 => false,
1 => true,
_ => return None,
};
let mut at = 13;
let mut strings = Vec::with_capacity(2);
for _ in 0..2 {
if bytes.len() < at + 2 {
return None;
}
let len = usize::from(u16_at(bytes, at));
at += 2;
if len > MAX_IDENTITY_STRING || bytes.len() < at + len {
return None;
}
strings.push(String::from_utf8(bytes[at..at + len].to_vec()).ok()?);
at += len;
}
if at != bytes.len() {
return None;
}
let revision = strings.pop()?;
let name = strings.pop()?;
Some(Self {
name,
revision,
dimensions: u32_at(bytes, 0),
normalized,
text_template: u32_at(bytes, 5),
chunk_template: u32_at(bytes, 9),
})
}
#[must_use]
pub fn id(&self) -> ModelId {
let mut hasher = blake3::Hasher::new_derive_key("aft-store 2026-11 vector model identity");
hasher.update(&self.encode());
ModelId(Digest128::from_hash(&hasher.finalize()))
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ModelId(Digest128);
impl ModelId {
#[must_use]
pub const fn digest(&self) -> Digest128 {
self.0
}
}
impl fmt::Display for ModelId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl fmt::Debug for ModelId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ModelId({})", self.0)
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TextDigest(Digest128);
impl TextDigest {
#[must_use]
pub fn of(bytes: &[u8]) -> Self {
Self(Digest128::of(bytes))
}
#[must_use]
pub const fn from_digest(digest: Digest128) -> Self {
Self(digest)
}
#[must_use]
pub const fn digest(&self) -> Digest128 {
self.0
}
}
impl fmt::Display for TextDigest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl fmt::Debug for TextDigest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "TextDigest({})", self.0)
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct ChunkMapKey {
content: ContentKey,
path: Digest128,
}
impl ChunkMapKey {
pub(crate) const LEN: usize = 2 * Digest128::LEN;
#[must_use]
pub fn new(content: ContentKey, path: &[u8]) -> Self {
let mut hasher = blake3::Hasher::new_derive_key("aft-store 2026-11 chunk map path");
hasher.update(path);
Self {
content,
path: Digest128::from_hash(&hasher.finalize()),
}
}
#[must_use]
pub const fn content(&self) -> ContentKey {
self.content
}
pub(crate) fn to_bytes(self) -> [u8; Self::LEN] {
let mut out = [0; Self::LEN];
out[..Digest128::LEN].copy_from_slice(self.content.digest().as_bytes());
out[Digest128::LEN..].copy_from_slice(self.path.as_bytes());
out
}
pub(crate) fn from_bytes(bytes: &[u8]) -> Option<Self> {
if bytes.len() != Self::LEN {
return None;
}
Some(Self {
content: ContentKey::from_digest(Digest128::from_bytes(crate::le::array(bytes, 0))),
path: Digest128::from_bytes(crate::le::array(bytes, Digest128::LEN)),
})
}
}
crate::heap_bytes_none!(ModelId, TextDigest, ChunkMapKey);
crate::heap_bytes_of_fields!(ModelIdentity {
name,
revision,
dimensions,
normalized,
text_template,
chunk_template,
});