use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use super::{normalize_source_ref, CanonicalisedSource};
use crate::memory::chunks::{Metadata, SourceKind};
fn default_provider() -> String {
"unknown".to_string()
}
fn now_utc() -> DateTime<Utc> {
Utc::now()
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DocumentInput {
#[serde(default = "default_provider")]
pub provider: String,
pub title: String,
pub body: String,
#[serde(
default = "now_utc",
deserialize_with = "super::deserialize_flexible_timestamp"
)]
pub modified_at: DateTime<Utc>,
#[serde(default)]
pub source_ref: Option<String>,
}
pub fn canonicalise(
source_id: &str,
owner: &str,
tags: &[String],
doc: DocumentInput,
path_scope: Option<String>,
) -> Result<Option<CanonicalisedSource>, String> {
if doc.body.trim().is_empty() && doc.title.trim().is_empty() {
return Ok(None);
}
let mut md = String::new();
md.push_str(doc.body.trim());
md.push('\n');
Ok(Some(CanonicalisedSource {
markdown: md,
metadata: Metadata {
source_kind: SourceKind::Document,
source_id: source_id.to_string(),
owner: owner.to_string(),
timestamp: doc.modified_at,
time_range: (doc.modified_at, doc.modified_at),
tags: tags.to_vec(),
source_ref: normalize_source_ref(doc.source_ref),
path_scope,
},
}))
}
#[cfg(test)]
#[path = "document_tests.rs"]
mod tests;