use serde::{Deserialize, Serialize};
use super::NeuralCommit;
pub const SCHEMA_VERSION: u32 = 1;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ObjectType {
NeuralCommit,
Blob,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ObjectEnvelope<T> {
pub object_type: ObjectType,
pub schema_version: u32,
pub created_at: i64,
pub data: T,
}
impl<T> ObjectEnvelope<T> {
pub fn new(object_type: ObjectType, data: T) -> Self {
Self {
object_type,
schema_version: SCHEMA_VERSION,
created_at: chrono::Utc::now().timestamp(),
data,
}
}
}
pub type WrappedNeuralCommit = ObjectEnvelope<NeuralCommit>;
impl WrappedNeuralCommit {
pub fn wrap(commit: NeuralCommit) -> Self {
ObjectEnvelope::new(ObjectType::NeuralCommit, commit)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlobContent {
pub content_type: String,
pub content: String,
}
impl BlobContent {
pub fn new(content_type: impl Into<String>, content: impl Into<String>) -> Self {
Self {
content_type: content_type.into(),
content: content.into(),
}
}
pub fn roadmap(content: impl Into<String>) -> Self {
Self::new("roadmap", content)
}
pub fn trace(content: impl Into<String>) -> Self {
Self::new("trace", content)
}
}
pub type WrappedBlob = ObjectEnvelope<BlobContent>;
impl WrappedBlob {
pub fn wrap(blob: BlobContent) -> Self {
ObjectEnvelope::new(ObjectType::Blob, blob)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_object_envelope() {
let blob = BlobContent::roadmap("Build the best AI tool");
let wrapped = WrappedBlob::wrap(blob);
assert_eq!(wrapped.object_type, ObjectType::Blob);
assert_eq!(wrapped.schema_version, SCHEMA_VERSION);
assert_eq!(wrapped.data.content_type, "roadmap");
}
#[test]
fn test_blob_content_types() {
let roadmap = BlobContent::roadmap("Goals here");
assert_eq!(roadmap.content_type, "roadmap");
let trace = BlobContent::trace("Trace data");
assert_eq!(trace.content_type, "trace");
}
#[test]
fn test_serialization() {
let blob = BlobContent::roadmap("Test");
let wrapped = WrappedBlob::wrap(blob);
let json = serde_json::to_string(&wrapped).unwrap();
assert!(json.contains("\"object_type\":\"blob\""));
assert!(json.contains("\"schema_version\":1"));
}
}